Create dateCompare function
Will eventually try to move away from zeit. Don't need all of it's functionality as long as SQL can format dates
This commit is contained in:
parent
4991bac9a4
commit
52fefc9ba5
1 changed files with 27 additions and 0 deletions
|
|
@ -1,8 +1,35 @@
|
|||
const std = @import("std");
|
||||
const zeit = @import("zeit");
|
||||
const Data = @import("types.zig");
|
||||
|
||||
pub fn dateFmt(allocator: std.mem.Allocator, epoch: i64) ![]const u8 {
|
||||
var date = std.ArrayList(u8).init(allocator);
|
||||
try (try zeit.instant(.{ .source = .{ .unix_timestamp = @divFloor(epoch, 1_000) } })).time().strftime(date.writer(), "%d %b %Y, %H:%M");
|
||||
return date.items;
|
||||
}
|
||||
|
||||
pub fn dateCompare(self: *[]const u8, earliest: []const u8, latest: []const u8) bool {
|
||||
const range = if (self.len == 19) 4 else 2;
|
||||
const time = std.fmt.parseInt(u32, self[0..range], 10) catch 0;
|
||||
const etime = std.fmt.parseInt(u32, earliest[0..range], 10) catch 0;
|
||||
const ltime = std.fmt.parseInt(u32, latest[0..range], 10) catch 0;
|
||||
|
||||
if (time != etime and time != ltime) {
|
||||
return (time > etime and time < ltime);
|
||||
} else {
|
||||
return dateCompare(self[range + 1 ..], earliest[range + 1 ..], latest[range + 1 ..]);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scrobbleToRow(allocator: std.mem.Allocator, scrobble: Data.Scrobble) !Data.TableRow {
|
||||
var artistlist = std.ArrayList(Data.HyperlinkData).init(allocator);
|
||||
for (scrobble.artists_track) |a| {
|
||||
try artistlist.append(Data.HyperlinkData{ .name = a, .id = 0 });
|
||||
}
|
||||
return Data.TableRow{
|
||||
.song = .{ .name = scrobble.track, .id = 0 },
|
||||
.artistlist = artistlist.items,
|
||||
.album = .{ .name = scrobble.album, .id = 0 },
|
||||
.date = try dateFmt(allocator, scrobble.date),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue