diff --git a/src/Entities.zig b/src/Entities.zig index 8f027c4..64ff18c 100644 --- a/src/Entities.zig +++ b/src/Entities.zig @@ -2,7 +2,8 @@ //! Fields based on JSON response fields const Alias = @import("Alias.zig").Alias; -const zeit = @import("zeit"); +//const zeit = @import("zeit"); +const std = @import("std"); pub const Artist = struct { id: []const u8, @@ -28,7 +29,7 @@ pub const Recording = struct { @"artist-credit": ?[]ArtistCredit = null, //artist_credit: []ArtistCredit, @"first-release-date": ?[]const u8 = null, - dt: ?i64 = null, + //dt: ?i64 = null, //@"first-release-date": ?union(enum) { date: []const u8, dt: zeit.Date } = null, //first_release_date: []const u8, releases: ?[]Release = null, @@ -37,8 +38,28 @@ pub const Recording = struct { disambiguation: ?[]const u8 = null, pub fn lessThan(context: void, a: Recording, b: Recording) bool { - _ = context; - return a.dt.? < b.dt.?; + _ = context; // Idk what this is but it's in the Zig docs + if (a.@"first-release-date") |adtstr| { + if (b.@"first-release-date") |bdtstr| { + const adtyr = std.fmt.parseInt(u32, adtstr[0..4], 10) catch 0; // Each `catch 0` is to avoid returning an error + const bdtyr = std.fmt.parseInt(u32, bdtstr[0..4], 10) catch 0; + if (adtyr != bdtyr) { + return adtyr < bdtyr; + } else if (adtstr.len > 7) { + if (bdtstr.len < 7) return true; // a provides more information + const adtmn = std.fmt.parseInt(u32, adtstr[5..7], 10) catch 0; + const bdtmn = std.fmt.parseInt(u32, bdtstr[5..7], 10) catch 0; + if (adtmn != bdtmn) { + return adtmn < bdtmn; + } else if (adtstr.len == 10) { + if (bdtstr.len < 10) return true; // a provides more information + const adtdy = std.fmt.parseInt(u32, adtstr[8..10], 10) catch 0; + const bdtdy = std.fmt.parseInt(u32, bdtstr[8..10], 10) catch 0; + return adtdy < bdtdy; + } else return false; // Either b provides more information, or they're the same date + } else return false; // Either b provides more information, or they're the same date + } else return true; // b provides no information + } else return false; // a provides no information } }; pub const Release = struct { diff --git a/src/QueryResults.zig b/src/QueryResults.zig index 7d320cb..85b8f5f 100644 --- a/src/QueryResults.zig +++ b/src/QueryResults.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const zeit = @import("zeit"); +//const zeit = @import("zeit"); const Entities = @import("Entities.zig"); const Artist = Entities.Artist; @@ -80,10 +80,10 @@ pub const Result = struct { 0 => return null, 1 => return self.recordings.?[0].id, else => { - var rcs: []Recording = self.recordings.?; - for (rcs, 0..) |rc, i| { - rcs[i].dt = (try zeit.instant(.{ .source = .{ .iso8601 = rc.@"first-release-date".? } })).unixTimestamp(); - } + const rcs: []Recording = self.recordings.?; + //for (rcs, 0..) |rc, i| { + // rcs[i].dt = (try zeit.instant(.{ .source = .{ .iso8601 = rc.@"first-release-date".? } })).unixTimestamp(); + //} std.mem.sort(Recording, rcs, {}, Recording.lessThan); for (rcs) |rc| { if (std.mem.eql(u8, tn, rc.title)) { diff --git a/src/root.zig b/src/root.zig index ece053c..de7c3f3 100644 --- a/src/root.zig +++ b/src/root.zig @@ -136,7 +136,7 @@ test "rgid_via_recording_multiple_artists_2" { //try testing.expect(false); } -test "rid" { +test "rid_aqualung" { const test_alloc = std.testing.allocator; const track: []const u8 = "Aqualung"; const album: []const u8 = "Aqualung";