const DateTime = @This(); const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const Clock = Io.Clock; const Writer = Io.Writer; const Duration = Io.Clock.Duration; const Timestamp = Io.Clock.Timestamp; const Cancelable = Io.Cancelable; const Order = std.math.Order; const CompareOperator = std.math.CompareOperator; const assert = std.debug.assert; const datetime = @import("root.zig"); const Parser = @import("Parser.zig"); const Month = datetime.Month; const Error = datetime.Error; const Span = @import("span.zig").Span; const utils = @import("utils.zig"); const t = utils.testing; const TestStruct = utils.TestStruct; const Format = datetime.Format; const Precision = datetime.Precision; const Time = datetime.Time; const Date = datetime.Date; timestamp: Timestamp, pub fn now(io: Io) DateTime { return .{ .timestamp = .now(io, .real) }; } pub fn addDuration(self: DateTime, duration: Duration) DateTime { return .{ .timestamp = self.timestamp.addDuration(duration) }; } pub fn subDuration(self: DateTime, duration: Duration) DateTime { return .{ .timestamp = self.timestamp.subDuration(duration) }; } pub fn add(self: DateTime, span: Span) DateTime { switch (span) { .ns => |ns| return .{ .timestamp = self.timestamp.addDuration(.{ .clock = .real, .raw = .{ .nanoseconds = ns, }, }), }, } } pub fn toNanoseconds(self: DateTime) i96 { return self.timestamp.raw.toNanoseconds(); } pub fn toMicroseconds(self: DateTime) i64 { return self.timestamp.raw.toMicroseconds(); } pub fn toMilliseconds(self: DateTime) i64 { return self.timestamp.raw.toMilliseconds(); } pub fn toSeconds(self: DateTime) i64 { return self.timestamp.raw.toSeconds(); } /// Sleeps until this datetime arrives. pub fn wait(self: DateTime, io: Io) Cancelable!void { return self.timestamp.wait(io); } /// Sleeps for the specified amount of time. pub fn sleep(io: Io, duration: Io.Duration) Cancelable!void { try io.sleep(duration, .real); } /// returns the duration between a past datetime and now pub fn durationToNow(self: DateTime, io: Io) Duration { return self.timestamp.untilNow(io); } /// Returns the duration between now and a future datetime pub fn durationfromNow(self: DateTime, io: Io) Duration { return self.timestamp.durationFromNow(io); } pub fn rawTimestamp(self: DateTime) Io.Timestamp { return self.timestamp.raw; } pub fn toTimestamp(self: DateTime) Timestamp { return self.timestamp; } // https://git.musl-libc.org/cgit/musl/tree/src/time/__secs_to_tm.c?h=v0.9.15 pub fn date(dt: DateTime) Date { // 2000-03-01 (mod 400 year, immediately after feb29 const leap_epoch = 946684800 + 86400 * (31 + 29); const days_per_400y = 365 * 400 + 97; const days_per_100y = 365 * 100 + 24; const days_per_4y = 365 * 4 + 1; // march-based const month_days = [_]u8{ 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29 }; const secs = dt.toSeconds() - leap_epoch; var days = @divTrunc(secs, 86400); if (@rem(secs, 86400) < 0) days -= 1; var qc_cycles = @divTrunc(days, days_per_400y); var rem_days = @rem(days, days_per_400y); if (rem_days < 0) { rem_days += days_per_400y; qc_cycles -= 1; } var c_cycles = @divTrunc(rem_days, days_per_100y); if (c_cycles == 4) c_cycles -= 1; rem_days -= c_cycles * days_per_100y; var q_cycles = @divTrunc(rem_days, days_per_4y); if (q_cycles == 25) q_cycles -= 1; rem_days -= q_cycles * days_per_4y; var rem_years = @divTrunc(rem_days, 365); if (rem_years == 4) rem_years -= 1; rem_days -= rem_years * 365; var year = rem_years + 4 * q_cycles + 100 * c_cycles + 400 * qc_cycles + 2000; var month: u8 = 0; while (month_days[month] <= rem_days) : (month += 1) rem_days -= month_days[month]; month += 2; if (month >= 12) { year += 1; month -= 12; } return .{ .year = @intCast(year), .month = @enumFromInt(month + 1), .day = @intCast(rem_days + 1), }; } pub fn unix(self: DateTime, comptime precision: Precision) switch (precision) { .seconds, .milliseconds, .microseconds => i64, .nanoseconds => i96, } { return switch (precision) { .seconds => self.toSeconds(), .milliseconds => self.toMilliseconds(), .microseconds => self.toMicroseconds(), .nanoseconds => self.toNanoseconds(), }; } pub fn time(self: DateTime) Time { const micros = @mod(self.toMicroseconds(), std.time.us_per_day); return .{ .hour = @intCast(@divTrunc(micros, std.time.us_per_hour)), .min = @intCast(@divTrunc(@rem(micros, std.time.us_per_hour), std.time.us_per_min)), .sec = @intCast(@divTrunc(@rem(micros, std.time.us_per_min), std.time.us_per_s)), .micros = @intCast(@rem(micros, std.time.us_per_s)), }; } // https://blog.reverberate.org/2020/05/12/optimizing-date-algorithms.html pub fn fromUtc(year: i32, month: u8, day: u8, hour: u8, min: u8, sec: u8, micros: u32) !DateTime { // verify valid _ = try Date.init(@intCast(year), try .fromInt(month), day); _ = try Time.init(hour, min, sec, micros); const year_base = 4800; const month_adj = @as(i64, @intCast(month)) - 3; // March-based month const carry: i64 = @intFromBool(month_adj < 0); const year_adj: i64 = year + year_base - carry; const month_days = ((month_adj + carry * 12) * 62719 + 769) >> 11; const leap_days = (year_adj >> 2) - @divTrunc(year_adj, 100) + @divTrunc(year_adj, 400); const date_micros: i64 = (year_adj * 365 + leap_days + month_days + (day - 1) - 2472632) * std.time.us_per_day; const time_micros = (@as(i64, @intCast(hour)) * std.time.us_per_hour) + (@as(i64, @intCast(min)) * std.time.us_per_min) + (@as(i64, @intCast(sec)) * std.time.us_per_s) + micros; return fromUnixTimestamp(date_micros + time_micros, .microseconds); } pub fn format(self: DateTime, writer: *Writer) !void { var buf: [28]u8 = undefined; const n = self.bufWrite(&buf); try writer.writeAll(buf[0..n]); } /// Julian Day Zero /// Returns the JD0 timestamp for the selected unit of measurement. pub fn jd0(precision: Precision) i96 { const seconds = -210866803200; return switch (precision) { .seconds => seconds, .milliseconds => seconds * std.time.ms_per_s, .microseconds => seconds * std.time.us_per_s, .nanoseconds => seconds * std.time.ns_per_s, }; } pub fn fromUnixTimestamp(value: i96, comptime precision: Precision) DateTime { const multiplier = switch (precision) { .seconds => std.time.ns_per_s, .milliseconds => std.time.ns_per_ms, .microseconds => std.time.ns_per_us, .nanoseconds => 1, }; return .{ .timestamp = .{ .clock = .real, .raw = .{ .nanoseconds = value * multiplier, }, }, }; } pub fn parse(fmt: Format, input: []const u8) !DateTime { return switch (fmt) { .rfc3339 => .parseRFC3339(input), .iso8601 => Error.NotImplemented, }; } pub fn order(lhs: DateTime, rhs: DateTime) Order { assert(lhs.timestamp.clock == rhs.timestamp.clock); return std.math.order(lhs.timestamp.raw.nanoseconds, rhs.timestamp.raw.nanoseconds); } pub fn compare(lhs: DateTime, op: CompareOperator, rhs: DateTime) bool { return lhs.timestamp.compare(op, rhs.timestamp); } pub fn jsonStringify(self: DateTime, jws: anytype) !void { var buf: [28]u8 = undefined; const n = self.bufWrite(&buf); try jws.write(buf[0..n]); } pub fn jsonParse(allocator: Allocator, source: anytype, options: anytype) !DateTime { _ = options; switch (try source.nextAlloc(allocator, .alloc_if_needed)) { inline .string, .allocated_string => |str| return parseRFC3339(str) catch return error.InvalidCharacter, else => return error.UnexpectedToken, } } fn bufWrite(self: DateTime, buf: []u8) usize { const date_n = utils.writeDate(buf, self.date()); buf[date_n] = 'T'; const time_start = date_n + 1; const time_n = utils.writeTime(buf[time_start..], self.time()); const time_stop = time_start + time_n; buf[time_stop] = 'Z'; return time_stop + 1; } fn parseRFC3339(input: []const u8) !DateTime { var parser: Parser = .init(input); const dt = try parser.rfc3339Date(); const year = dt.year; if (year < -4712 or year > 9999) return Error.OutsideJulianPeriod; // Per the spec, it can be argued thatt 't' and even ' ' should be allowed, // but certainly not encouraged. if (parser.consumeIf('T') == false) return Error.InvalidDateTime; const tm = try parser.time(); switch (parser.unconsumed()) { 0 => return Error.InvalidDateTime, 1 => if (parser.consumeIf('Z') == false) return Error.InvalidDateTime, 6 => { const suffix = parser.rest(); if (suffix[0] != '+' and suffix[0] != '-') return Error.InvalidDateTime; if (std.mem.eql(u8, suffix[1..], "00:00") == false) return Error.NonUTCNotSupported; }, else => return Error.InvalidDateTime, } return .fromUtc( dt.year, @intFromEnum(dt.month), dt.day, tm.hour, tm.min, tm.sec, tm.micros, ); } const fromUnix = fromUnixTimestamp; test "DateTime: initUTC" { // GO // for i := 0; i < 100; i++ { // us := rand.Int63n(31536000000000000) // if i%2 == 1 { // us = -us // } // date := time.UnixMicro(us).UTC() // fmt.Printf("\ttry t.expectEqual(%d, (try DateTime.fromUtc(%d, %d, %d, %d, %d, %d, %d)).micros);\n", us, date.Year(), date.Month(), date.Day(), date.Hour(), date.Minute(), date.Second(), date.Nanosecond()/1000) // } try t.expectEqual(31185488490276150, (try fromUtc(2958, 3, 25, 3, 41, 30, 276150)).toMicroseconds()); try t.expectEqual(-17564653328342207, (try fromUtc(1413, 5, 26, 9, 37, 51, 657793)).toMicroseconds()); try t.expectEqual(11204762425459393, (try fromUtc(2325, 1, 24, 18, 0, 25, 459393)).toMicroseconds()); try t.expectEqual(-11416605162739875, (try fromUtc(1608, 3, 22, 8, 47, 17, 260125)).toMicroseconds()); try t.expectEqual(4075732367920414, (try fromUtc(2099, 2, 25, 19, 52, 47, 920414)).toMicroseconds()); try t.expectEqual(-18408335598163579, (try fromUtc(1386, 8, 30, 13, 26, 41, 836421)).toMicroseconds()); try t.expectEqual(17086490946271926, (try fromUtc(2511, 6, 14, 7, 29, 6, 271926)).toMicroseconds()); try t.expectEqual(-235277150936616, (try fromUtc(1962, 7, 18, 21, 14, 9, 63384)).toMicroseconds()); try t.expectEqual(11104788804726682, (try fromUtc(2321, 11, 24, 15, 33, 24, 726682)).toMicroseconds()); try t.expectEqual(-4568937205156452, (try fromUtc(1825, 3, 20, 18, 46, 34, 843548)).toMicroseconds()); try t.expectEqual(24765673968274275, (try fromUtc(2754, 10, 17, 17, 52, 48, 274275)).toMicroseconds()); try t.expectEqual(-7121990846251510, (try fromUtc(1744, 4, 24, 13, 12, 33, 748490)).toMicroseconds()); try t.expectEqual(17226397205968456, (try fromUtc(2515, 11, 19, 14, 20, 5, 968456)).toMicroseconds()); try t.expectEqual(-6754262392339050, (try fromUtc(1755, 12, 19, 16, 0, 7, 660950)).toMicroseconds()); try t.expectEqual(16357572620714009, (try fromUtc(2488, 5, 7, 18, 10, 20, 714009)).toMicroseconds()); try t.expectEqual(-25688820176639049, (try fromUtc(1155, 12, 15, 16, 37, 3, 360951)).toMicroseconds()); try t.expectEqual(20334458172336139, (try fromUtc(2614, 5, 17, 12, 36, 12, 336139)).toMicroseconds()); try t.expectEqual(-30602962159178117, (try fromUtc(1000, 3, 26, 1, 10, 40, 821883)).toMicroseconds()); try t.expectEqual(10851036879825648, (try fromUtc(2313, 11, 9, 16, 54, 39, 825648)).toMicroseconds()); try t.expectEqual(-21853769826060317, (try fromUtc(1277, 6, 24, 20, 22, 53, 939683)).toMicroseconds()); try t.expectEqual(23747326217087461, (try fromUtc(2722, 7, 11, 7, 30, 17, 87461)).toMicroseconds()); try t.expectEqual(-6579703114708064, (try fromUtc(1761, 7, 1, 0, 41, 25, 291936)).toMicroseconds()); try t.expectEqual(14734931422924073, (try fromUtc(2436, 12, 6, 4, 30, 22, 924073)).toMicroseconds()); try t.expectEqual(-14370161672281011, (try fromUtc(1514, 8, 18, 16, 25, 27, 718989)).toMicroseconds()); try t.expectEqual(21611484560584058, (try fromUtc(2654, 11, 3, 22, 9, 20, 584058)).toMicroseconds()); try t.expectEqual(-15774514890527755, (try fromUtc(1470, 2, 15, 14, 18, 29, 472245)).toMicroseconds()); try t.expectEqual(12457884381373706, (try fromUtc(2364, 10, 10, 11, 26, 21, 373706)).toMicroseconds()); try t.expectEqual(-9291409512875127, (try fromUtc(1675, 7, 26, 12, 54, 47, 124873)).toMicroseconds()); try t.expectEqual(18766703512694310, (try fromUtc(2564, 9, 10, 5, 11, 52, 694310)).toMicroseconds()); try t.expectEqual(-10898338457124469, (try fromUtc(1624, 8, 23, 19, 45, 42, 875531)).toMicroseconds()); try t.expectEqual(27404278841361952, (try fromUtc(2838, 5, 29, 3, 40, 41, 361952)).toMicroseconds()); try t.expectEqual(-11493696741549109, (try fromUtc(1605, 10, 12, 2, 27, 38, 450891)).toMicroseconds()); try t.expectEqual(25167839321247044, (try fromUtc(2767, 7, 16, 10, 28, 41, 247044)).toMicroseconds()); try t.expectEqual(-8645720427930599, (try fromUtc(1696, 1, 10, 18, 59, 32, 69401)).toMicroseconds()); try t.expectEqual(7021225980669527, (try fromUtc(2192, 6, 29, 4, 33, 0, 669527)).toMicroseconds()); try t.expectEqual(-22567421500525473, (try fromUtc(1254, 11, 12, 23, 48, 19, 474527)).toMicroseconds()); try t.expectEqual(3592419409525180, (try fromUtc(2083, 11, 2, 22, 16, 49, 525180)).toMicroseconds()); try t.expectEqual(-24897829995733878, (try fromUtc(1181, 1, 7, 16, 6, 44, 266122)).toMicroseconds()); try t.expectEqual(1801796752202729, (try fromUtc(2027, 2, 5, 3, 5, 52, 202729)).toMicroseconds()); try t.expectEqual(-21458729756349585, (try fromUtc(1289, 12, 31, 1, 44, 3, 650415)).toMicroseconds()); try t.expectEqual(27431277767015263, (try fromUtc(2839, 4, 6, 15, 22, 47, 15263)).toMicroseconds()); try t.expectEqual(-11932647633976328, (try fromUtc(1591, 11, 14, 15, 39, 26, 23672)).toMicroseconds()); try t.expectEqual(11561116817530249, (try fromUtc(2336, 5, 11, 5, 20, 17, 530249)).toMicroseconds()); try t.expectEqual(-20238374988448844, (try fromUtc(1328, 9, 2, 13, 10, 11, 551156)).toMicroseconds()); try t.expectEqual(17825448287939368, (try fromUtc(2534, 11, 13, 1, 24, 47, 939368)).toMicroseconds()); try t.expectEqual(-16551182110752962, (try fromUtc(1445, 7, 7, 9, 24, 49, 247038)).toMicroseconds()); try t.expectEqual(7773488831126355, (try fromUtc(2216, 5, 1, 22, 27, 11, 126355)).toMicroseconds()); try t.expectEqual(-17967725644400042, (try fromUtc(1400, 8, 17, 5, 5, 55, 599958)).toMicroseconds()); try t.expectEqual(30634276344447791, (try fromUtc(2940, 10, 5, 9, 12, 24, 447791)).toMicroseconds()); try t.expectEqual(-3201531339091604, (try fromUtc(1868, 7, 19, 5, 44, 20, 908396)).toMicroseconds()); try t.expectEqual(16621702451341054, (try fromUtc(2496, 9, 19, 19, 34, 11, 341054)).toMicroseconds()); try t.expectEqual(-12321145808433043, (try fromUtc(1579, 7, 24, 3, 29, 51, 566957)).toMicroseconds()); try t.expectEqual(116851935152341, (try fromUtc(1973, 9, 14, 10, 52, 15, 152341)).toMicroseconds()); try t.expectEqual(-26516365395395707, (try fromUtc(1129, 9, 24, 14, 56, 44, 604293)).toMicroseconds()); try t.expectEqual(29944637164250909, (try fromUtc(2918, 11, 28, 10, 46, 4, 250909)).toMicroseconds()); try t.expectEqual(-14268089958574835, (try fromUtc(1517, 11, 12, 1, 40, 41, 425165)).toMicroseconds()); try t.expectEqual(10902808879115327, (try fromUtc(2315, 7, 1, 22, 1, 19, 115327)).toMicroseconds()); try t.expectEqual(-13675746347719473, (try fromUtc(1536, 8, 19, 21, 34, 12, 280527)).toMicroseconds()); try t.expectEqual(9823904882276154, (try fromUtc(2281, 4, 22, 14, 28, 2, 276154)).toMicroseconds()); try t.expectEqual(-8027825490751946, (try fromUtc(1715, 8, 11, 8, 28, 29, 248054)).toMicroseconds()); try t.expectEqual(8338818189787922, (try fromUtc(2234, 4, 1, 2, 23, 9, 787922)).toMicroseconds()); try t.expectEqual(-2417779710874201, (try fromUtc(1893, 5, 20, 10, 31, 29, 125799)).toMicroseconds()); try t.expectEqual(15579463520321126, (try fromUtc(2463, 9, 10, 20, 45, 20, 321126)).toMicroseconds()); try t.expectEqual(-30111774746323219, (try fromUtc(1015, 10, 19, 2, 7, 33, 676781)).toMicroseconds()); try t.expectEqual(8586318907201828, (try fromUtc(2242, 2, 2, 16, 35, 7, 201828)).toMicroseconds()); try t.expectEqual(-20727462914538728, (try fromUtc(1313, 3, 4, 19, 24, 45, 461272)).toMicroseconds()); try t.expectEqual(12684924982677857, (try fromUtc(2371, 12, 21, 6, 16, 22, 677857)).toMicroseconds()); try t.expectEqual(-26995363453933698, (try fromUtc(1114, 7, 21, 15, 55, 46, 66302)).toMicroseconds()); try t.expectEqual(5769549719315448, (try fromUtc(2152, 10, 30, 4, 41, 59, 315448)).toMicroseconds()); try t.expectEqual(-9362762735064704, (try fromUtc(1673, 4, 21, 16, 34, 24, 935296)).toMicroseconds()); try t.expectEqual(5196087673076825, (try fromUtc(2134, 8, 28, 21, 41, 13, 76825)).toMicroseconds()); try t.expectEqual(-10198286600499296, (try fromUtc(1646, 10, 30, 6, 36, 39, 500704)).toMicroseconds()); try t.expectEqual(19333137979539125, (try fromUtc(2582, 8, 23, 4, 6, 19, 539125)).toMicroseconds()); try t.expectEqual(-18867539824804327, (try fromUtc(1372, 2, 10, 16, 42, 55, 195673)).toMicroseconds()); try t.expectEqual(14853031249581056, (try fromUtc(2440, 9, 3, 2, 0, 49, 581056)).toMicroseconds()); try t.expectEqual(-1356282109230506, (try fromUtc(1927, 1, 9, 6, 58, 10, 769494)).toMicroseconds()); try t.expectEqual(15713222018105813, (try fromUtc(2467, 12, 6, 23, 53, 38, 105813)).toMicroseconds()); try t.expectEqual(-12693041975378709, (try fromUtc(1567, 10, 10, 19, 0, 24, 621291)).toMicroseconds()); try t.expectEqual(29394313298789588, (try fromUtc(2901, 6, 20, 23, 1, 38, 789588)).toMicroseconds()); try t.expectEqual(-10583952098364782, (try fromUtc(1634, 8, 10, 13, 18, 21, 635218)).toMicroseconds()); try t.expectEqual(22418800474726154, (try fromUtc(2680, 6, 3, 20, 34, 34, 726154)).toMicroseconds()); try t.expectEqual(-13067278028607441, (try fromUtc(1555, 12, 1, 8, 32, 51, 392559)).toMicroseconds()); try t.expectEqual(22348003126725817, (try fromUtc(2678, 3, 7, 10, 38, 46, 725817)).toMicroseconds()); try t.expectEqual(-11101998054915852, (try fromUtc(1618, 3, 11, 15, 39, 5, 84148)).toMicroseconds()); try t.expectEqual(30004645932503986, (try fromUtc(2920, 10, 22, 23, 52, 12, 503986)).toMicroseconds()); try t.expectEqual(-27551013013624622, (try fromUtc(1096, 12, 10, 12, 49, 46, 375378)).toMicroseconds()); try t.expectEqual(10162791607756167, (try fromUtc(2292, 1, 17, 21, 40, 7, 756167)).toMicroseconds()); try t.expectEqual(-31309636417799549, (try fromUtc(977, 11, 1, 22, 46, 22, 200451)).toMicroseconds()); try t.expectEqual(9816298180956872, (try fromUtc(2281, 1, 24, 13, 29, 40, 956872)).toMicroseconds()); try t.expectEqual(-13248552913008079, (try fromUtc(1550, 3, 4, 6, 24, 46, 991921)).toMicroseconds()); try t.expectEqual(24898184818866845, (try fromUtc(2758, 12, 29, 10, 26, 58, 866845)).toMicroseconds()); try t.expectEqual(-10721424878768860, (try fromUtc(1630, 4, 2, 10, 25, 21, 231140)).toMicroseconds()); try t.expectEqual(3556757075942051, (try fromUtc(2082, 9, 16, 4, 4, 35, 942051)).toMicroseconds()); try t.expectEqual(-9515936853544912, (try fromUtc(1668, 6, 13, 20, 12, 26, 455088)).toMicroseconds()); try t.expectEqual(23236928933459964, (try fromUtc(2706, 5, 8, 22, 28, 53, 459964)).toMicroseconds()); try t.expectEqual(-5811784886171477, (try fromUtc(1785, 10, 30, 23, 18, 33, 828523)).toMicroseconds()); try t.expectEqual(27342496921109542, (try fromUtc(2836, 6, 13, 2, 2, 1, 109542)).toMicroseconds()); try t.expectEqual(-25369943235288340, (try fromUtc(1166, 1, 22, 9, 32, 44, 711660)).toMicroseconds()); try t.expectEqual(10054378230055484, (try fromUtc(2288, 8, 11, 2, 50, 30, 55484)).toMicroseconds()); try t.expectEqual(-10826899878642792, (try fromUtc(1626, 11, 28, 15, 48, 41, 357208)).toMicroseconds()); } test "DateTime: now" { const dt = now(t.io); try t.expectDelta(Io.Timestamp.now(t.io, .real).toMicroseconds(), dt.toMicroseconds(), 100); } test "DateTime: date" { try t.expectEqual(Date{ .year = 2023, .month = .november, .day = 25 }, (fromUnixTimestamp(1700886257, .seconds)).date()); try t.expectEqual(Date{ .year = 2023, .month = .november, .day = 25 }, (fromUnixTimestamp(1700886257655, .milliseconds)).date()); try t.expectEqual(Date{ .year = 2023, .month = .november, .day = 25 }, (fromUnixTimestamp(1700886257655392, .microseconds)).date()); try t.expectEqual(Date{ .year = 1970, .month = .january, .day = 1 }, (fromUnixTimestamp(0, .milliseconds)).date()); // GO: // for i := 0; i < 100; i++ { // us := rand.Int63n(31536000000000000) // if i%2 == 1 { // us = -us // } // date := time.UnixMicro(us).UTC() // fmt.Printf("\ttry t.expectEqual(Date{.year = %d, .month = %d, .day = %d}, DateTime.fromUnix(%d, .seconds).date());\n", date.Year(), date.Month(), date.Day(), date.Unix()) // } try t.expectEqual(Date{ .year = 2438, .month = .august, .day = 8 }, (fromUnixTimestamp(14787635606, .seconds)).date()); try t.expectEqual(Date{ .year = 1290, .month = .october, .day = 9 }, (fromUnixTimestamp(-21434368940, .seconds)).date()); try t.expectEqual(Date{ .year = 2769, .month = .december, .day = 3 }, (fromUnixTimestamp(25243136028, .seconds)).date()); try t.expectEqual(Date{ .year = 1437, .month = .june, .day = 30 }, (fromUnixTimestamp(-16804239664, .seconds)).date()); try t.expectEqual(Date{ .year = 2752, .month = .april, .day = 7 }, (fromUnixTimestamp(24685876670, .seconds)).date()); try t.expectEqual(Date{ .year = 1484, .month = .january, .day = 29 }, (fromUnixTimestamp(-15334209737, .seconds)).date()); try t.expectEqual(Date{ .year = 2300, .month = .january, .day = 4 }, (fromUnixTimestamp(10414107497, .seconds)).date()); try t.expectEqual(Date{ .year = 1520, .month = .march, .day = 27 }, (fromUnixTimestamp(-14193188705, .seconds)).date()); try t.expectEqual(Date{ .year = 2628, .month = .november, .day = 21 }, (fromUnixTimestamp(20792540664, .seconds)).date()); try t.expectEqual(Date{ .year = 1807, .month = .february, .day = 21 }, (fromUnixTimestamp(-5139411928, .seconds)).date()); try t.expectEqual(Date{ .year = 2249, .month = .december, .day = 12 }, (fromUnixTimestamp(8834245007, .seconds)).date()); try t.expectEqual(Date{ .year = 1694, .month = .november, .day = 17 }, (fromUnixTimestamp(-8681990253, .seconds)).date()); try t.expectEqual(Date{ .year = 2725, .month = .june, .day = 10 }, (fromUnixTimestamp(23839369640, .seconds)).date()); try t.expectEqual(Date{ .year = 1947, .month = .february, .day = 16 }, (fromUnixTimestamp(-721811319, .seconds)).date()); try t.expectEqual(Date{ .year = 2293, .month = .september, .day = 28 }, (fromUnixTimestamp(10216323340, .seconds)).date()); try t.expectEqual(Date{ .year = 1614, .month = .august, .day = 12 }, (fromUnixTimestamp(-11214942944, .seconds)).date()); try t.expectEqual(Date{ .year = 2923, .month = .june, .day = 24 }, (fromUnixTimestamp(30088834422, .seconds)).date()); try t.expectEqual(Date{ .year = 1120, .month = .april, .day = 16 }, (fromUnixTimestamp(-26814276389, .seconds)).date()); try t.expectEqual(Date{ .year = 2035, .month = .december, .day = 9 }, (fromUnixTimestamp(2080850037, .seconds)).date()); try t.expectEqual(Date{ .year = 1167, .month = .january, .day = 15 }, (fromUnixTimestamp(-25338977309, .seconds)).date()); try t.expectEqual(Date{ .year = 2665, .month = .april, .day = 15 }, (fromUnixTimestamp(21941133655, .seconds)).date()); try t.expectEqual(Date{ .year = 1375, .month = .june, .day = 18 }, (fromUnixTimestamp(-18761787336, .seconds)).date()); try t.expectEqual(Date{ .year = 2189, .month = .june, .day = 13 }, (fromUnixTimestamp(6925211914, .seconds)).date()); try t.expectEqual(Date{ .year = 1938, .month = .january, .day = 12 }, (fromUnixTimestamp(-1008879186, .seconds)).date()); try t.expectEqual(Date{ .year = 2556, .month = .june, .day = 9 }, (fromUnixTimestamp(18506255391, .seconds)).date()); try t.expectEqual(Date{ .year = 1294, .month = .october, .day = 29 }, (fromUnixTimestamp(-21306371902, .seconds)).date()); try t.expectEqual(Date{ .year = 2330, .month = .march, .day = 19 }, (fromUnixTimestamp(11367189469, .seconds)).date()); try t.expectEqual(Date{ .year = 1696, .month = .may, .day = 22 }, (fromUnixTimestamp(-8634251099, .seconds)).date()); try t.expectEqual(Date{ .year = 2759, .month = .may, .day = 14 }, (fromUnixTimestamp(24909971092, .seconds)).date()); try t.expectEqual(Date{ .year = 1641, .month = .january, .day = 31 }, (fromUnixTimestamp(-10379518549, .seconds)).date()); try t.expectEqual(Date{ .year = 2451, .month = .june, .day = 26 }, (fromUnixTimestamp(15194147684, .seconds)).date()); try t.expectEqual(Date{ .year = 1962, .month = .january, .day = 4 }, (fromUnixTimestamp(-252197440, .seconds)).date()); try t.expectEqual(Date{ .year = 2883, .month = .november, .day = 15 }, (fromUnixTimestamp(28839089617, .seconds)).date()); try t.expectEqual(Date{ .year = 1587, .month = .august, .day = 5 }, (fromUnixTimestamp(-12067604792, .seconds)).date()); try t.expectEqual(Date{ .year = 2724, .month = .may, .day = 28 }, (fromUnixTimestamp(23806729201, .seconds)).date()); try t.expectEqual(Date{ .year = 1043, .month = .february, .day = 25 }, (fromUnixTimestamp(-29248487174, .seconds)).date()); try t.expectEqual(Date{ .year = 2927, .month = .march, .day = 9 }, (fromUnixTimestamp(30205844459, .seconds)).date()); try t.expectEqual(Date{ .year = 1451, .month = .june, .day = 16 }, (fromUnixTimestamp(-16363722083, .seconds)).date()); try t.expectEqual(Date{ .year = 2145, .month = .january, .day = 21 }, (fromUnixTimestamp(5524305523, .seconds)).date()); try t.expectEqual(Date{ .year = 1497, .month = .october, .day = 31 }, (fromUnixTimestamp(-14900125085, .seconds)).date()); try t.expectEqual(Date{ .year = 2162, .month = .april, .day = 1 }, (fromUnixTimestamp(6066812142, .seconds)).date()); try t.expectEqual(Date{ .year = 1738, .month = .august, .day = 12 }, (fromUnixTimestamp(-7301852750, .seconds)).date()); try t.expectEqual(Date{ .year = 2100, .month = .february, .day = 7 }, (fromUnixTimestamp(4105665807, .seconds)).date()); try t.expectEqual(Date{ .year = 1847, .month = .september, .day = 29 }, (fromUnixTimestamp(-3858020808, .seconds)).date()); try t.expectEqual(Date{ .year = 2370, .month = .september, .day = 19 }, (fromUnixTimestamp(12645416176, .seconds)).date()); try t.expectEqual(Date{ .year = 1292, .month = .july, .day = 8 }, (fromUnixTimestamp(-21379166225, .seconds)).date()); try t.expectEqual(Date{ .year = 2931, .month = .december, .day = 19 }, (fromUnixTimestamp(30356691249, .seconds)).date()); try t.expectEqual(Date{ .year = 1064, .month = .may, .day = 12 }, (fromUnixTimestamp(-28579189254, .seconds)).date()); try t.expectEqual(Date{ .year = 2295, .month = .may, .day = 13 }, (fromUnixTimestamp(10267494406, .seconds)).date()); try t.expectEqual(Date{ .year = 1449, .month = .december, .day = 4 }, (fromUnixTimestamp(-16411941423, .seconds)).date()); try t.expectEqual(Date{ .year = 2565, .month = .january, .day = 16 }, (fromUnixTimestamp(18777760055, .seconds)).date()); try t.expectEqual(Date{ .year = 1968, .month = .june, .day = 25 }, (fromUnixTimestamp(-47882241, .seconds)).date()); try t.expectEqual(Date{ .year = 2817, .month = .may, .day = 9 }, (fromUnixTimestamp(26739900891, .seconds)).date()); try t.expectEqual(Date{ .year = 1334, .month = .july, .day = 16 }, (fromUnixTimestamp(-20053254809, .seconds)).date()); try t.expectEqual(Date{ .year = 2945, .month = .april, .day = 24 }, (fromUnixTimestamp(30777844895, .seconds)).date()); try t.expectEqual(Date{ .year = 1930, .month = .february, .day = 27 }, (fromUnixTimestamp(-1257362995, .seconds)).date()); try t.expectEqual(Date{ .year = 2768, .month = .october, .day = 19 }, (fromUnixTimestamp(25207675701, .seconds)).date()); try t.expectEqual(Date{ .year = 1372, .month = .june, .day = 12 }, (fromUnixTimestamp(-18856904218, .seconds)).date()); try t.expectEqual(Date{ .year = 2603, .month = .august, .day = 29 }, (fromUnixTimestamp(19996315706, .seconds)).date()); try t.expectEqual(Date{ .year = 1201, .month = .april, .day = 7 }, (fromUnixTimestamp(-24258926407, .seconds)).date()); try t.expectEqual(Date{ .year = 2466, .month = .april, .day = 16 }, (fromUnixTimestamp(15661407305, .seconds)).date()); try t.expectEqual(Date{ .year = 1513, .month = .may, .day = 7 }, (fromUnixTimestamp(-14410616341, .seconds)).date()); try t.expectEqual(Date{ .year = 2619, .month = .september, .day = 11 }, (fromUnixTimestamp(20502308837, .seconds)).date()); try t.expectEqual(Date{ .year = 1501, .month = .may, .day = 13 }, (fromUnixTimestamp(-14788768973, .seconds)).date()); try t.expectEqual(Date{ .year = 2765, .month = .november, .day = 19 }, (fromUnixTimestamp(25115683551, .seconds)).date()); try t.expectEqual(Date{ .year = 1881, .month = .february, .day = 9 }, (fromUnixTimestamp(-2805094638, .seconds)).date()); try t.expectEqual(Date{ .year = 2253, .month = .april, .day = 28 }, (fromUnixTimestamp(8940802800, .seconds)).date()); try t.expectEqual(Date{ .year = 1941, .month = .november, .day = 23 }, (fromUnixTimestamp(-886973505, .seconds)).date()); try t.expectEqual(Date{ .year = 2565, .month = .january, .day = 18 }, (fromUnixTimestamp(18777963967, .seconds)).date()); try t.expectEqual(Date{ .year = 1313, .month = .may, .day = 20 }, (fromUnixTimestamp(-20720877804, .seconds)).date()); try t.expectEqual(Date{ .year = 2401, .month = .may, .day = 6 }, (fromUnixTimestamp(13611949193, .seconds)).date()); try t.expectEqual(Date{ .year = 1146, .month = .november, .day = 2 }, (fromUnixTimestamp(-25976564837, .seconds)).date()); try t.expectEqual(Date{ .year = 2115, .month = .june, .day = 11 }, (fromUnixTimestamp(4589719542, .seconds)).date()); try t.expectEqual(Date{ .year = 1276, .month = .august, .day = 1 }, (fromUnixTimestamp(-21882043432, .seconds)).date()); try t.expectEqual(Date{ .year = 2224, .month = .april, .day = 26 }, (fromUnixTimestamp(8025468043, .seconds)).date()); try t.expectEqual(Date{ .year = 1336, .month = .june, .day = 19 }, (fromUnixTimestamp(-19992405201, .seconds)).date()); try t.expectEqual(Date{ .year = 2717, .month = .may, .day = 5 }, (fromUnixTimestamp(23583761778, .seconds)).date()); try t.expectEqual(Date{ .year = 1222, .month = .march, .day = 15 }, (fromUnixTimestamp(-23598239244, .seconds)).date()); try t.expectEqual(Date{ .year = 2841, .month = .august, .day = 29 }, (fromUnixTimestamp(27506984246, .seconds)).date()); try t.expectEqual(Date{ .year = 1818, .month = .july, .day = 28 }, (fromUnixTimestamp(-4778656923, .seconds)).date()); try t.expectEqual(Date{ .year = 2533, .month = .may, .day = 13 }, (fromUnixTimestamp(17778031068, .seconds)).date()); try t.expectEqual(Date{ .year = 1146, .month = .july, .day = 28 }, (fromUnixTimestamp(-25984946441, .seconds)).date()); try t.expectEqual(Date{ .year = 2451, .month = .february, .day = 1 }, (fromUnixTimestamp(15181688532, .seconds)).date()); try t.expectEqual(Date{ .year = 1091, .month = .august, .day = 28 }, (fromUnixTimestamp(-27717880960, .seconds)).date()); try t.expectEqual(Date{ .year = 2168, .month = .april, .day = 12 }, (fromUnixTimestamp(6257133476, .seconds)).date()); try t.expectEqual(Date{ .year = 1718, .month = .october, .day = 16 }, (fromUnixTimestamp(-7927438165, .seconds)).date()); try t.expectEqual(Date{ .year = 2614, .month = .august, .day = 21 }, (fromUnixTimestamp(20342724001, .seconds)).date()); try t.expectEqual(Date{ .year = 1869, .month = .may, .day = 4 }, (fromUnixTimestamp(-3176499822, .seconds)).date()); try t.expectEqual(Date{ .year = 2504, .month = .april, .day = 20 }, (fromUnixTimestamp(16860953121, .seconds)).date()); try t.expectEqual(Date{ .year = 1401, .month = .may, .day = 2 }, (fromUnixTimestamp(-17945432544, .seconds)).date()); try t.expectEqual(Date{ .year = 2467, .month = .august, .day = 2 }, (fromUnixTimestamp(15702325347, .seconds)).date()); try t.expectEqual(Date{ .year = 1654, .month = .march, .day = 12 }, (fromUnixTimestamp(-9965864717, .seconds)).date()); try t.expectEqual(Date{ .year = 2371, .month = .september, .day = 2 }, (fromUnixTimestamp(12675412066, .seconds)).date()); try t.expectEqual(Date{ .year = 1784, .month = .january, .day = 16 }, (fromUnixTimestamp(-5868249970, .seconds)).date()); try t.expectEqual(Date{ .year = 2907, .month = .august, .day = 25 }, (fromUnixTimestamp(29589265328, .seconds)).date()); try t.expectEqual(Date{ .year = 987, .month = .april, .day = 9 }, (fromUnixTimestamp(-31011963272, .seconds)).date()); try t.expectEqual(Date{ .year = 1980, .month = .october, .day = 19 }, (fromUnixTimestamp(340838803, .seconds)).date()); try t.expectEqual(Date{ .year = 1386, .month = .may, .day = 18 }, (fromUnixTimestamp(-18417299412, .seconds)).date()); try t.expectEqual(Date{ .year = 2622, .month = .february, .day = 5 }, (fromUnixTimestamp(20578157994, .seconds)).date()); try t.expectEqual(Date{ .year = 1056, .month = .november, .day = 6 }, (fromUnixTimestamp(-28816263601, .seconds)).date()); } test "DateTime: time" { // GO: // for i := 0; i < 100; i++ { // us := rand.Int63n(31536000000000000) // if i%2 == 1 { // us = -us // } // date := time.UnixMicro(us).UTC() // fmt.Printf("\ttry t.expectEqual(Time{.hour = %d, .min = %d, .sec = %d, .micros = %d}, (try DateTime.fromUnix(%d, .microseconds)).time());\n", date.Hour(), date.Minute(), date.Second(), date.Nanosecond()/1000, us) // } try t.expectEqual(Time{ .hour = 18, .min = 56, .sec = 18, .micros = 38399 }, (fromUnixTimestamp(6940752978038399, .microseconds)).time()); try t.expectEqual(Time{ .hour = 14, .min = 10, .sec = 48, .micros = 481799 }, (fromUnixTimestamp(-15037004951518201, .microseconds)).time()); try t.expectEqual(Time{ .hour = 13, .min = 49, .sec = 27, .micros = 814723 }, (fromUnixTimestamp(26507483367814723, .microseconds)).time()); try t.expectEqual(Time{ .hour = 3, .min = 53, .sec = 47, .micros = 990825 }, (fromUnixTimestamp(-15290625972009175, .microseconds)).time()); try t.expectEqual(Time{ .hour = 9, .min = 28, .sec = 54, .micros = 16606 }, (fromUnixTimestamp(28046078934016606, .microseconds)).time()); try t.expectEqual(Time{ .hour = 17, .min = 36, .sec = 38, .micros = 380600 }, (fromUnixTimestamp(-8638640601619400, .microseconds)).time()); try t.expectEqual(Time{ .hour = 17, .min = 29, .sec = 27, .micros = 109527 }, (fromUnixTimestamp(26649192567109527, .microseconds)).time()); try t.expectEqual(Time{ .hour = 23, .min = 54, .sec = 48, .micros = 10233 }, (fromUnixTimestamp(-24667200311989767, .microseconds)).time()); try t.expectEqual(Time{ .hour = 5, .min = 44, .sec = 50, .micros = 913226 }, (fromUnixTimestamp(22200932690913226, .microseconds)).time()); try t.expectEqual(Time{ .hour = 5, .min = 36, .sec = 19, .micros = 337687 }, (fromUnixTimestamp(-13186952620662313, .microseconds)).time()); try t.expectEqual(Time{ .hour = 20, .min = 6, .sec = 37, .micros = 157270 }, (fromUnixTimestamp(17827416397157270, .microseconds)).time()); try t.expectEqual(Time{ .hour = 4, .min = 43, .sec = 33, .micros = 871331 }, (fromUnixTimestamp(-15558635786128669, .microseconds)).time()); try t.expectEqual(Time{ .hour = 0, .min = 26, .sec = 54, .micros = 557236 }, (fromUnixTimestamp(23322644814557236, .microseconds)).time()); try t.expectEqual(Time{ .hour = 7, .min = 38, .sec = 40, .micros = 370732 }, (fromUnixTimestamp(-1368030079629268, .microseconds)).time()); try t.expectEqual(Time{ .hour = 2, .min = 31, .sec = 9, .micros = 223691 }, (fromUnixTimestamp(20164386669223691, .microseconds)).time()); try t.expectEqual(Time{ .hour = 12, .min = 41, .sec = 23, .micros = 165207 }, (fromUnixTimestamp(-20761960716834793, .microseconds)).time()); try t.expectEqual(Time{ .hour = 0, .min = 46, .sec = 49, .micros = 962075 }, (fromUnixTimestamp(549247609962075, .microseconds)).time()); try t.expectEqual(Time{ .hour = 2, .min = 7, .sec = 12, .micros = 984678 }, (fromUnixTimestamp(-11643688367015322, .microseconds)).time()); try t.expectEqual(Time{ .hour = 11, .min = 32, .sec = 16, .micros = 343799 }, (fromUnixTimestamp(4022998336343799, .microseconds)).time()); try t.expectEqual(Time{ .hour = 17, .min = 26, .sec = 54, .micros = 366277 }, (fromUnixTimestamp(-8557597985633723, .microseconds)).time()); try t.expectEqual(Time{ .hour = 16, .min = 1, .sec = 4, .micros = 485152 }, (fromUnixTimestamp(15070896064485152, .microseconds)).time()); try t.expectEqual(Time{ .hour = 4, .min = 14, .sec = 18, .micros = 923558 }, (fromUnixTimestamp(-15995389541076442, .microseconds)).time()); try t.expectEqual(Time{ .hour = 5, .min = 37, .sec = 58, .micros = 948826 }, (fromUnixTimestamp(16828148278948826, .microseconds)).time()); try t.expectEqual(Time{ .hour = 6, .min = 52, .sec = 27, .micros = 1770 }, (fromUnixTimestamp(-30509975252998230, .microseconds)).time()); try t.expectEqual(Time{ .hour = 0, .min = 32, .sec = 28, .micros = 381047 }, (fromUnixTimestamp(7813499548381047, .microseconds)).time()); try t.expectEqual(Time{ .hour = 14, .min = 1, .sec = 49, .micros = 267686 }, (fromUnixTimestamp(-14265712690732314, .microseconds)).time()); try t.expectEqual(Time{ .hour = 4, .min = 53, .sec = 23, .micros = 233239 }, (fromUnixTimestamp(31107646403233239, .microseconds)).time()); try t.expectEqual(Time{ .hour = 3, .min = 0, .sec = 53, .micros = 292242 }, (fromUnixTimestamp(-10317099546707758, .microseconds)).time()); try t.expectEqual(Time{ .hour = 8, .min = 22, .sec = 13, .micros = 966628 }, (fromUnixTimestamp(11215959733966628, .microseconds)).time()); try t.expectEqual(Time{ .hour = 17, .min = 32, .sec = 22, .micros = 779813 }, (fromUnixTimestamp(-15711949657220187, .microseconds)).time()); try t.expectEqual(Time{ .hour = 1, .min = 6, .sec = 36, .micros = 405828 }, (fromUnixTimestamp(6872691996405828, .microseconds)).time()); try t.expectEqual(Time{ .hour = 12, .min = 0, .sec = 55, .micros = 420129 }, (fromUnixTimestamp(-31068273544579871, .microseconds)).time()); try t.expectEqual(Time{ .hour = 22, .min = 17, .sec = 6, .micros = 930158 }, (fromUnixTimestamp(26304473826930158, .microseconds)).time()); try t.expectEqual(Time{ .hour = 12, .min = 45, .sec = 25, .micros = 203619 }, (fromUnixTimestamp(-5358482074796381, .microseconds)).time()); try t.expectEqual(Time{ .hour = 19, .min = 28, .sec = 0, .micros = 476749 }, (fromUnixTimestamp(9134623680476749, .microseconds)).time()); try t.expectEqual(Time{ .hour = 11, .min = 58, .sec = 41, .micros = 864572 }, (fromUnixTimestamp(-29314353678135428, .microseconds)).time()); try t.expectEqual(Time{ .hour = 6, .min = 19, .sec = 27, .micros = 566937 }, (fromUnixTimestamp(9005494767566937, .microseconds)).time()); try t.expectEqual(Time{ .hour = 9, .min = 3, .sec = 17, .micros = 164061 }, (fromUnixTimestamp(-24631052202835939, .microseconds)).time()); try t.expectEqual(Time{ .hour = 23, .min = 2, .sec = 41, .micros = 147703 }, (fromUnixTimestamp(27754959761147703, .microseconds)).time()); try t.expectEqual(Time{ .hour = 16, .min = 51, .sec = 1, .micros = 710888 }, (fromUnixTimestamp(-29839475338289112, .microseconds)).time()); try t.expectEqual(Time{ .hour = 1, .min = 31, .sec = 44, .micros = 244667 }, (fromUnixTimestamp(13143000704244667, .microseconds)).time()); try t.expectEqual(Time{ .hour = 14, .min = 40, .sec = 45, .micros = 594500 }, (fromUnixTimestamp(-27029323154405500, .microseconds)).time()); try t.expectEqual(Time{ .hour = 3, .min = 28, .sec = 18, .micros = 941443 }, (fromUnixTimestamp(26929337298941443, .microseconds)).time()); try t.expectEqual(Time{ .hour = 18, .min = 34, .sec = 26, .micros = 418287 }, (fromUnixTimestamp(-16849401933581713, .microseconds)).time()); try t.expectEqual(Time{ .hour = 16, .min = 51, .sec = 12, .micros = 390293 }, (fromUnixTimestamp(24013471872390293, .microseconds)).time()); try t.expectEqual(Time{ .hour = 5, .min = 27, .sec = 59, .micros = 116472 }, (fromUnixTimestamp(-4881839520883528, .microseconds)).time()); try t.expectEqual(Time{ .hour = 22, .min = 38, .sec = 58, .micros = 829840 }, (fromUnixTimestamp(28012689538829840, .microseconds)).time()); try t.expectEqual(Time{ .hour = 13, .min = 31, .sec = 51, .micros = 397163 }, (fromUnixTimestamp(-14000034488602837, .microseconds)).time()); try t.expectEqual(Time{ .hour = 16, .min = 25, .sec = 36, .micros = 566333 }, (fromUnixTimestamp(3819630336566333, .microseconds)).time()); try t.expectEqual(Time{ .hour = 23, .min = 52, .sec = 35, .micros = 404576 }, (fromUnixTimestamp(-24790838844595424, .microseconds)).time()); try t.expectEqual(Time{ .hour = 14, .min = 17, .sec = 56, .micros = 248627 }, (fromUnixTimestamp(4303462676248627, .microseconds)).time()); try t.expectEqual(Time{ .hour = 22, .min = 56, .sec = 31, .micros = 445770 }, (fromUnixTimestamp(-7573827808554230, .microseconds)).time()); try t.expectEqual(Time{ .hour = 1, .min = 36, .sec = 32, .micros = 60901 }, (fromUnixTimestamp(12791180192060901, .microseconds)).time()); try t.expectEqual(Time{ .hour = 4, .min = 12, .sec = 1, .micros = 816276 }, (fromUnixTimestamp(-29726596078183724, .microseconds)).time()); try t.expectEqual(Time{ .hour = 17, .min = 25, .sec = 2, .micros = 88680 }, (fromUnixTimestamp(9072494702088680, .microseconds)).time()); try t.expectEqual(Time{ .hour = 7, .min = 14, .sec = 18, .micros = 149127 }, (fromUnixTimestamp(-20968821941850873, .microseconds)).time()); try t.expectEqual(Time{ .hour = 15, .min = 45, .sec = 55, .micros = 818121 }, (fromUnixTimestamp(14590424755818121, .microseconds)).time()); try t.expectEqual(Time{ .hour = 13, .min = 45, .sec = 5, .micros = 544234 }, (fromUnixTimestamp(-21099694494455766, .microseconds)).time()); try t.expectEqual(Time{ .hour = 20, .min = 58, .sec = 32, .micros = 361661 }, (fromUnixTimestamp(27070837112361661, .microseconds)).time()); try t.expectEqual(Time{ .hour = 18, .min = 42, .sec = 3, .micros = 375293 }, (fromUnixTimestamp(-22783699076624707, .microseconds)).time()); try t.expectEqual(Time{ .hour = 15, .min = 5, .sec = 18, .micros = 844868 }, (fromUnixTimestamp(3924515118844868, .microseconds)).time()); try t.expectEqual(Time{ .hour = 17, .min = 39, .sec = 15, .micros = 454348 }, (fromUnixTimestamp(-19519510844545652, .microseconds)).time()); try t.expectEqual(Time{ .hour = 22, .min = 34, .sec = 57, .micros = 584438 }, (fromUnixTimestamp(25405223697584438, .microseconds)).time()); try t.expectEqual(Time{ .hour = 12, .min = 58, .sec = 48, .micros = 604253 }, (fromUnixTimestamp(-23848167671395747, .microseconds)).time()); try t.expectEqual(Time{ .hour = 21, .min = 6, .sec = 10, .micros = 130143 }, (fromUnixTimestamp(9179039170130143, .microseconds)).time()); try t.expectEqual(Time{ .hour = 11, .min = 40, .sec = 45, .micros = 806457 }, (fromUnixTimestamp(-10457900354193543, .microseconds)).time()); try t.expectEqual(Time{ .hour = 5, .min = 32, .sec = 3, .micros = 84471 }, (fromUnixTimestamp(20206560723084471, .microseconds)).time()); try t.expectEqual(Time{ .hour = 11, .min = 8, .sec = 48, .micros = 571978 }, (fromUnixTimestamp(-13147966271428022, .microseconds)).time()); try t.expectEqual(Time{ .hour = 10, .min = 37, .sec = 9, .micros = 847397 }, (fromUnixTimestamp(9639599829847397, .microseconds)).time()); try t.expectEqual(Time{ .hour = 20, .min = 15, .sec = 37, .micros = 731453 }, (fromUnixTimestamp(-17972509462268547, .microseconds)).time()); try t.expectEqual(Time{ .hour = 0, .min = 36, .sec = 51, .micros = 658834 }, (fromUnixTimestamp(23080639011658834, .microseconds)).time()); try t.expectEqual(Time{ .hour = 3, .min = 6, .sec = 2, .micros = 359939 }, (fromUnixTimestamp(-13484004837640061, .microseconds)).time()); try t.expectEqual(Time{ .hour = 1, .min = 24, .sec = 8, .micros = 76822 }, (fromUnixTimestamp(22642161848076822, .microseconds)).time()); try t.expectEqual(Time{ .hour = 5, .min = 20, .sec = 47, .micros = 940649 }, (fromUnixTimestamp(-9576815952059351, .microseconds)).time()); try t.expectEqual(Time{ .hour = 16, .min = 19, .sec = 30, .micros = 228423 }, (fromUnixTimestamp(11237847570228423, .microseconds)).time()); try t.expectEqual(Time{ .hour = 16, .min = 54, .sec = 33, .micros = 913828 }, (fromUnixTimestamp(-9146156726086172, .microseconds)).time()); try t.expectEqual(Time{ .hour = 20, .min = 14, .sec = 10, .micros = 663120 }, (fromUnixTimestamp(12400805650663120, .microseconds)).time()); try t.expectEqual(Time{ .hour = 15, .min = 22, .sec = 22, .micros = 500411 }, (fromUnixTimestamp(-13183893457499589, .microseconds)).time()); try t.expectEqual(Time{ .hour = 18, .min = 42, .sec = 11, .micros = 637021 }, (fromUnixTimestamp(17415888131637021, .microseconds)).time()); try t.expectEqual(Time{ .hour = 22, .min = 7, .sec = 43, .micros = 497651 }, (fromUnixTimestamp(-3828045136502349, .microseconds)).time()); try t.expectEqual(Time{ .hour = 9, .min = 25, .sec = 22, .micros = 960397 }, (fromUnixTimestamp(25585406722960397, .microseconds)).time()); try t.expectEqual(Time{ .hour = 20, .min = 36, .sec = 31, .micros = 312572 }, (fromUnixTimestamp(-11209202608687428, .microseconds)).time()); try t.expectEqual(Time{ .hour = 5, .min = 25, .sec = 18, .micros = 104173 }, (fromUnixTimestamp(7748544318104173, .microseconds)).time()); try t.expectEqual(Time{ .hour = 11, .min = 23, .sec = 25, .micros = 504363 }, (fromUnixTimestamp(-22111446994495637, .microseconds)).time()); try t.expectEqual(Time{ .hour = 19, .min = 48, .sec = 44, .micros = 703684 }, (fromUnixTimestamp(21347696924703684, .microseconds)).time()); try t.expectEqual(Time{ .hour = 12, .min = 10, .sec = 21, .micros = 67035 }, (fromUnixTimestamp(-29976004178932965, .microseconds)).time()); try t.expectEqual(Time{ .hour = 6, .min = 0, .sec = 55, .micros = 355102 }, (fromUnixTimestamp(15622869655355102, .microseconds)).time()); try t.expectEqual(Time{ .hour = 21, .min = 12, .sec = 1, .micros = 574873 }, (fromUnixTimestamp(-28386384478425127, .microseconds)).time()); try t.expectEqual(Time{ .hour = 22, .min = 29, .sec = 45, .micros = 886627 }, (fromUnixTimestamp(27787703385886627, .microseconds)).time()); try t.expectEqual(Time{ .hour = 8, .min = 43, .sec = 51, .micros = 403514 }, (fromUnixTimestamp(-591981368596486, .microseconds)).time()); try t.expectEqual(Time{ .hour = 12, .min = 1, .sec = 19, .micros = 667089 }, (fromUnixTimestamp(411998479667089, .microseconds)).time()); try t.expectEqual(Time{ .hour = 14, .min = 15, .sec = 53, .micros = 366760 }, (fromUnixTimestamp(-29916899046633240, .microseconds)).time()); try t.expectEqual(Time{ .hour = 19, .min = 31, .sec = 23, .micros = 639485 }, (fromUnixTimestamp(29847555083639485, .microseconds)).time()); try t.expectEqual(Time{ .hour = 0, .min = 21, .sec = 29, .micros = 207122 }, (fromUnixTimestamp(-13356229110792878, .microseconds)).time()); try t.expectEqual(Time{ .hour = 10, .min = 35, .sec = 51, .micros = 789976 }, (fromUnixTimestamp(2401353351789976, .microseconds)).time()); try t.expectEqual(Time{ .hour = 23, .min = 51, .sec = 4, .micros = 23674 }, (fromUnixTimestamp(-8687002135976326, .microseconds)).time()); try t.expectEqual(Time{ .hour = 3, .min = 23, .sec = 21, .micros = 985741 }, (fromUnixTimestamp(7637772201985741, .microseconds)).time()); try t.expectEqual(Time{ .hour = 22, .min = 3, .sec = 34, .micros = 497666 }, (fromUnixTimestamp(-22331814985502334, .microseconds)).time()); try t.expectEqual(Time{ .hour = 17, .min = 15, .sec = 11, .micros = 818441 }, (fromUnixTimestamp(14544983711818441, .microseconds)).time()); try t.expectEqual(Time{ .hour = 17, .min = 47, .sec = 39, .micros = 303089 }, (fromUnixTimestamp(-19977775940696911, .microseconds)).time()); } test "DateTime: parse RFC3339" { { const dt = try parse(.rfc3339, "-3221-01-02T03:04:05Z"); try t.expectEqual(-163812056155000000, dt.toMicroseconds()); try t.expectEqual(-3221, dt.date().year); try t.expectEqual(.january, dt.date().month); try t.expectEqual(2, dt.date().day); try t.expectEqual(3, dt.time().hour); try t.expectEqual(4, dt.time().min); try t.expectEqual(5, dt.time().sec); try t.expectEqual(0, dt.time().micros); } { const dt = try parse(.rfc3339, "0001-02-03T04:05:06.789+00:00"); try t.expectEqual(-62132730893211000, dt.toMicroseconds()); try t.expectEqual(1, dt.date().year); try t.expectEqual(.february, dt.date().month); try t.expectEqual(3, dt.date().day); try t.expectEqual(4, dt.time().hour); try t.expectEqual(5, dt.time().min); try t.expectEqual(6, dt.time().sec); try t.expectEqual(789000, dt.time().micros); } { const dt = try parse(.rfc3339, "5000-12-31T23:59:58.987654321Z"); try t.expectEqual(95649119998987654, dt.toMicroseconds()); try t.expectEqual(5000, dt.date().year); try t.expectEqual(.december, dt.date().month); try t.expectEqual(31, dt.date().day); try t.expectEqual(23, dt.time().hour); try t.expectEqual(59, dt.time().min); try t.expectEqual(58, dt.time().sec); try t.expectEqual(987654, dt.time().micros); } // date portion is ISO8601 try t.expectError( Error.InvalidDate, parse(.rfc3339, "20230102T23:59:58.987654321Z"), ); for ([_][]const u8{ // invalid format "", "2023/01-02T00:00Z", "2023-01/02T00:00Z", "2023-1-02T00:00Z", "2023-01-2T00:00Z", "9-01-2T00:00Z", "99-01-2T00:00Z", "999-01-2T00:00Z", "-999-01-2T00:00Z", "-1-01-2T00:00Z", // invalid month "2023-00-22T00:00Z", "2023-0A-22T00:00Z", "2023-13-22T00:00Z", "2023-99-22T00:00Z", "-2023-00-22T00:00Z", "-2023-13-22T00:00Z", "-2023-99-22T00:00Z", // invalid day "2023-01-00T00:00Z", "2023-01-32T00:00Z", "2023-02-29T00:00Z", "2023-03-32T00:00Z", "2023-04-31T00:00Z", "2023-05-32T00:00Z", "2023-06-31T00:00Z", "2023-07-32T00:00Z", "2023-08-32T00:00Z", "2023-09-31T00:00Z", "2023-10-32T00:00Z", "2023-11-31T00:00Z", "2023-12-32T00:00Z", // leap years "2000-02-30T00:00Z", "2400-02-30T00:00Z", "2012-02-30T00:00Z", "2024-02-30T00:00Z", "2100-02-29T00:00Z", "2200-02-29T00:00Z", }) |string| try t.expectError(Error.InvalidDate, DateTime.parse(.rfc3339, string)); for ([_][]const u8{ // valid date, invalid separator or time "2023-10-10T00:00 00", "2023-10-10T10:20:30.a", "0001-01-01 T00:00Z", "0001-01-01t00:00Z", "0001-01-01 00:00Z", }) |string| try t.expectError(Error.InvalidDateTime, DateTime.parse(.rfc3339, string)); // valid (max day) try t.expectEqual(1675123200000000, (try parse(.rfc3339, "2023-01-31T00:00Z")).toMicroseconds()); try t.expectEqual(1677542400000000, (try parse(.rfc3339, "2023-02-28T00:00Z")).toMicroseconds()); try t.expectEqual(1680220800000000, (try parse(.rfc3339, "2023-03-31T00:00Z")).toMicroseconds()); try t.expectEqual(1682812800000000, (try parse(.rfc3339, "2023-04-30T00:00Z")).toMicroseconds()); try t.expectEqual(1685491200000000, (try parse(.rfc3339, "2023-05-31T00:00Z")).toMicroseconds()); try t.expectEqual(1688083200000000, (try parse(.rfc3339, "2023-06-30T00:00Z")).toMicroseconds()); try t.expectEqual(1690761600000000, (try parse(.rfc3339, "2023-07-31T00:00Z")).toMicroseconds()); try t.expectEqual(1693440000000000, (try parse(.rfc3339, "2023-08-31T00:00Z")).toMicroseconds()); try t.expectEqual(1696032000000000, (try parse(.rfc3339, "2023-09-30T00:00Z")).toMicroseconds()); try t.expectEqual(1698710400000000, (try parse(.rfc3339, "2023-10-31T00:00Z")).toMicroseconds()); try t.expectEqual(1701302400000000, (try parse(.rfc3339, "2023-11-30T00:00Z")).toMicroseconds()); try t.expectEqual(1703980800000000, (try parse(.rfc3339, "2023-12-31T00:00Z")).toMicroseconds()); // leap years try t.expectEqual(951782400000000, (try parse(.rfc3339, "2000-02-29T00:00Z")).toMicroseconds()); try t.expectEqual(13574563200000000, (try parse(.rfc3339, "2400-02-29T00:00Z")).toMicroseconds()); try t.expectEqual(1330473600000000, (try parse(.rfc3339, "2012-02-29T00:00Z")).toMicroseconds()); try t.expectEqual(1709164800000000, (try parse(.rfc3339, "2024-02-29T00:00Z")).toMicroseconds()); } test "DateTime: add" { { // positive var dt: DateTime = try .parse(.rfc3339, "2023-11-26T03:13:46.540234Z"); dt = dt.add(.microseconds(800)); try expectDateTime("2023-11-26T03:13:46.541034Z", dt); dt = dt.add(.milliseconds(950)); try expectDateTime("2023-11-26T03:13:47.491034Z", dt); dt = dt.add(.seconds(32)); try expectDateTime("2023-11-26T03:14:19.491034Z", dt); dt = dt.add(.minutes(1489)); try expectDateTime("2023-11-27T04:03:19.491034Z", dt); dt = dt.add(.days(6)); try expectDateTime("2023-12-03T04:03:19.491034Z", dt); dt = dt.add(.weeks(1)); try expectDateTime("2023-12-10T04:03:19.491034Z", dt); } { // negative var dt: DateTime = try .parse(.rfc3339, "2023-11-26T03:13:46.540234Z"); dt = dt.add(.microseconds(-800)); try expectDateTime("2023-11-26T03:13:46.539434Z", dt); dt = dt.add(.milliseconds(-950)); try expectDateTime("2023-11-26T03:13:45.589434Z", dt); dt = dt.add(.seconds(-50)); try expectDateTime("2023-11-26T03:12:55.589434Z", dt); dt = dt.add(.minutes(-1489)); try expectDateTime("2023-11-25T02:23:55.589434Z", dt); dt = dt.add(.days(-6)); try expectDateTime("2023-11-19T02:23:55.589434Z", dt); dt = dt.add(.weeks(-1)); try expectDateTime("2023-11-12T02:23:55.589434Z", dt); } } fn expectDateTime(expected: []const u8, dt: DateTime) !void { var buf: [30]u8 = undefined; const actual = try std.fmt.bufPrint(&buf, "{f}", .{dt}); try t.expectEqual(expected, actual); } test "DateTime: json" { { // DateTime, time no fraction const dt = try DateTime.parse(.rfc3339, "2023-09-22T23:59:02Z"); const out = try std.json.Stringify.valueAlloc(t.allocator, dt, .{}); defer t.allocator.free(out); try t.expectEqual("\"2023-09-22T23:59:02Z\"", out); } { // time, milliseconds only const dt = try DateTime.parse(.rfc3339, "2023-09-22T07:09:32.202Z"); const out = try std.json.Stringify.valueAlloc(t.allocator, dt, .{}); defer t.allocator.free(out); try t.expectEqual("\"2023-09-22T07:09:32.202Z\"", out); } { // time, micros const dt = try DateTime.parse(.rfc3339, "-0004-12-03T01:02:03.123456Z"); const out = try std.json.Stringify.valueAlloc(t.allocator, dt, .{}); defer t.allocator.free(out); try t.expectEqual("\"-0004-12-03T01:02:03.123456Z\"", out); } { // parse const ts = try std.json.parseFromSlice(TestStruct, t.allocator, "{\"datetime\":\"2023-09-22T07:09:32.202Z\"}", .{}); defer ts.deinit(); try t.expectEqual(try DateTime.parse(.rfc3339, "2023-09-22T07:09:32.202Z"), ts.value.datetime.?); } } test "DateTime: format" { { var buf: [30]u8 = undefined; const out = try std.fmt.bufPrint(&buf, "{f}", .{try DateTime.fromUtc(2023, 5, 22, 23, 59, 59, 0)}); try t.expectEqual("2023-05-22T23:59:59Z", out); } { var buf: [30]u8 = undefined; const out = try std.fmt.bufPrint(&buf, "{f}", .{try DateTime.fromUtc(2023, 5, 22, 8, 9, 10, 12)}); try t.expectEqual("2023-05-22T08:09:10.000012Z", out); } { var buf: [30]u8 = undefined; const out = try std.fmt.bufPrint(&buf, "{f}", .{try DateTime.fromUtc(2023, 5, 22, 8, 9, 10, 123)}); try t.expectEqual("2023-05-22T08:09:10.000123Z", out); } { var buf: [30]u8 = undefined; const out = try std.fmt.bufPrint(&buf, "{f}", .{try DateTime.fromUtc(2023, 5, 22, 8, 9, 10, 1234)}); try t.expectEqual("2023-05-22T08:09:10.001234Z", out); } { var buf: [30]u8 = undefined; const out = try std.fmt.bufPrint(&buf, "{f}", .{try DateTime.fromUtc(-102, 12, 9, 8, 9, 10, 12345)}); try t.expectEqual("-0102-12-09T08:09:10.012345Z", out); } { var buf: [30]u8 = undefined; const out = try std.fmt.bufPrint(&buf, "{f}", .{try DateTime.fromUtc(-102, 12, 9, 8, 9, 10, 123456)}); try t.expectEqual("-0102-12-09T08:09:10.123456Z", out); } } test "DateTime: order" { { const a = try fromUtc(2023, 11, 23, 19, 17, 22, 101002); const b = try fromUtc(2023, 11, 23, 19, 17, 22, 101002); try t.expectEqual(.eq, a.order(b)); } { const a = try fromUtc(2023, 5, 22, 12, 59, 2, 492); const b = try fromUtc(2022, 5, 22, 23, 59, 2, 492); try t.expectEqual(.gt, a.order(b)); try t.expectEqual(.lt, b.order(a)); } { const a = try fromUtc(2022, 6, 22, 23, 59, 2, 492); const b = try fromUtc(2022, 5, 22, 23, 33, 2, 492); try t.expectEqual(.gt, a.order(b)); try t.expectEqual(.lt, b.order(a)); } { const a = try fromUtc(2023, 5, 23, 23, 59, 2, 492); const b = try fromUtc(2022, 5, 22, 23, 59, 11, 492); try t.expectEqual(.gt, a.order(b)); try t.expectEqual(.lt, b.order(a)); } { const a = try fromUtc(2023, 11, 23, 20, 17, 22, 101002); const b = try fromUtc(2023, 11, 23, 19, 17, 22, 101002); try t.expectEqual(.gt, a.order(b)); try t.expectEqual(.lt, b.order(a)); } { const a = try fromUtc(2023, 11, 23, 19, 18, 22, 101002); const b = try fromUtc(2023, 11, 23, 19, 17, 22, 101002); try t.expectEqual(.gt, a.order(b)); try t.expectEqual(.lt, b.order(a)); } { const a = try fromUtc(2023, 11, 23, 19, 17, 23, 101002); const b = try fromUtc(2023, 11, 23, 19, 17, 22, 101002); try t.expectEqual(.gt, a.order(b)); try t.expectEqual(.lt, b.order(a)); } { const a = try fromUtc(2023, 11, 23, 19, 17, 22, 101003); const b = try fromUtc(2023, 11, 23, 19, 17, 22, 101002); try t.expectEqual(.gt, a.order(b)); try t.expectEqual(.lt, b.order(a)); } } test "DateTime: unix" { { const dt = try fromUtc(-4322, 1, 1, 0, 0, 0, 0); try t.expectEqual(-198556272000, dt.unix(.seconds)); try t.expectEqual(-198556272000000, dt.unix(.milliseconds)); try t.expectEqual(-198556272000000000, dt.unix(.microseconds)); } { const dt = try fromUtc(1970, 1, 1, 0, 0, 0, 0); try t.expectEqual(0, dt.unix(.seconds)); try t.expectEqual(0, dt.unix(.milliseconds)); try t.expectEqual(0, dt.unix(.microseconds)); } { const dt = try fromUtc(2023, 11, 24, 12, 6, 14, 918000); try t.expectEqual(1700827574, dt.unix(.seconds)); try t.expectEqual(1700827574918, dt.unix(.milliseconds)); try t.expectEqual(1700827574918000, dt.unix(.microseconds)); } // microseconds // GO: // for i := 0; i < 50; i++ { // us := rand.Int63n(3153600000000000) // if i%2 == 1 { // us = -us // } // date := time.UnixMicro(us).UTC() // fmt.Printf("\ttry t.expectEqual(%d, (try DateTime.parse(\"%s\", .rfc3339)).unix(.microseconds));\n", us, date.Format(time.RFC3339Nano)) // } try t.expectEqual(2568689002670356, (try parse(.rfc3339, "2051-05-26T04:43:22.670356Z")).unix(.microseconds)); try t.expectEqual(-2994122503199268, (try parse(.rfc3339, "1875-02-13T19:18:16.800732Z")).unix(.microseconds)); try t.expectEqual(2973860981156244, (try parse(.rfc3339, "2064-03-27T16:29:41.156244Z")).unix(.microseconds)); try t.expectEqual(-2122539648627924, (try parse(.rfc3339, "1902-09-28T13:39:11.372076Z")).unix(.microseconds)); try t.expectEqual(1440540448439442, (try parse(.rfc3339, "2015-08-25T22:07:28.439442Z")).unix(.microseconds)); try t.expectEqual(-843471236299718, (try parse(.rfc3339, "1943-04-10T14:26:03.700282Z")).unix(.microseconds)); try t.expectEqual(2428009970341301, (try parse(.rfc3339, "2046-12-09T23:12:50.341301Z")).unix(.microseconds)); try t.expectEqual(-861640488391156, (try parse(.rfc3339, "1942-09-12T07:25:11.608844Z")).unix(.microseconds)); try t.expectEqual(107457228254516, (try parse(.rfc3339, "1973-05-28T17:13:48.254516Z")).unix(.microseconds)); try t.expectEqual(-858997335483954, (try parse(.rfc3339, "1942-10-12T21:37:44.516046Z")).unix(.microseconds)); try t.expectEqual(1879201014676957, (try parse(.rfc3339, "2029-07-20T00:16:54.676957Z")).unix(.microseconds)); try t.expectEqual(-2779215184508509, (try parse(.rfc3339, "1881-12-06T03:46:55.491491Z")).unix(.microseconds)); try t.expectEqual(790920073212180, (try parse(.rfc3339, "1995-01-24T04:01:13.21218Z")).unix(.microseconds)); try t.expectEqual(-1986764905311346, (try parse(.rfc3339, "1907-01-17T00:51:34.688654Z")).unix(.microseconds)); try t.expectEqual(1567001594851223, (try parse(.rfc3339, "2019-08-28T14:13:14.851223Z")).unix(.microseconds)); try t.expectEqual(-2786308994565191, (try parse(.rfc3339, "1881-09-15T01:16:45.434809Z")).unix(.microseconds)); try t.expectEqual(1190930851203854, (try parse(.rfc3339, "2007-09-27T22:07:31.203854Z")).unix(.microseconds)); try t.expectEqual(-13894507787609, (try parse(.rfc3339, "1969-07-24T04:24:52.212391Z")).unix(.microseconds)); try t.expectEqual(1283185581222987, (try parse(.rfc3339, "2010-08-30T16:26:21.222987Z")).unix(.microseconds)); try t.expectEqual(-3080071240438154, (try parse(.rfc3339, "1872-05-25T00:39:19.561846Z")).unix(.microseconds)); try t.expectEqual(3091078494301752, (try parse(.rfc3339, "2067-12-14T08:54:54.301752Z")).unix(.microseconds)); try t.expectEqual(-2788286096253476, (try parse(.rfc3339, "1881-08-23T04:05:03.746524Z")).unix(.microseconds)); try t.expectEqual(1226140349962650, (try parse(.rfc3339, "2008-11-08T10:32:29.96265Z")).unix(.microseconds)); try t.expectEqual(-173789078990530, (try parse(.rfc3339, "1964-06-29T13:15:21.00947Z")).unix(.microseconds)); try t.expectEqual(2202006978733437, (try parse(.rfc3339, "2039-10-12T04:36:18.733437Z")).unix(.microseconds)); try t.expectEqual(-1957390566907891, (try parse(.rfc3339, "1907-12-23T00:23:53.092109Z")).unix(.microseconds)); try t.expectEqual(2704228013874812, (try parse(.rfc3339, "2055-09-10T22:26:53.874812Z")).unix(.microseconds)); try t.expectEqual(-2162891323622724, (try parse(.rfc3339, "1901-06-18T12:51:16.377276Z")).unix(.microseconds)); try t.expectEqual(2985526644225853, (try parse(.rfc3339, "2064-08-09T16:57:24.225853Z")).unix(.microseconds)); try t.expectEqual(-2714126911982044, (try parse(.rfc3339, "1883-12-29T11:51:28.017956Z")).unix(.microseconds)); try t.expectEqual(1389358847381035, (try parse(.rfc3339, "2014-01-10T13:00:47.381035Z")).unix(.microseconds)); try t.expectEqual(-2599632972496238, (try parse(.rfc3339, "1887-08-15T15:43:47.503762Z")).unix(.microseconds)); try t.expectEqual(2842567982275671, (try parse(.rfc3339, "2060-01-29T02:13:02.275671Z")).unix(.microseconds)); try t.expectEqual(-2924719405531619, (try parse(.rfc3339, "1877-04-27T01:56:34.468381Z")).unix(.microseconds)); try t.expectEqual(929389345478708, (try parse(.rfc3339, "1999-06-14T19:42:25.478708Z")).unix(.microseconds)); try t.expectEqual(-2928161617689577, (try parse(.rfc3339, "1877-03-18T05:46:22.310423Z")).unix(.microseconds)); try t.expectEqual(1981926664387480, (try parse(.rfc3339, "2032-10-20T23:11:04.38748Z")).unix(.microseconds)); try t.expectEqual(-3077852548046313, (try parse(.rfc3339, "1872-06-19T16:57:31.953687Z")).unix(.microseconds)); try t.expectEqual(323327680783683, (try parse(.rfc3339, "1980-03-31T05:14:40.783683Z")).unix(.microseconds)); try t.expectEqual(-1282955701919591, (try parse(.rfc3339, "1929-05-06T23:24:58.080409Z")).unix(.microseconds)); try t.expectEqual(1382921217423641, (try parse(.rfc3339, "2013-10-28T00:46:57.423641Z")).unix(.microseconds)); try t.expectEqual(-1431006940775286, (try parse(.rfc3339, "1924-08-27T10:04:19.224714Z")).unix(.microseconds)); try t.expectEqual(3074639946025509, (try parse(.rfc3339, "2067-06-07T02:39:06.025509Z")).unix(.microseconds)); try t.expectEqual(-2634608860053384, (try parse(.rfc3339, "1886-07-06T20:12:19.946616Z")).unix(.microseconds)); try t.expectEqual(2779915686281386, (try parse(.rfc3339, "2058-02-02T22:48:06.281386Z")).unix(.microseconds)); try t.expectEqual(-2016252325938190, (try parse(.rfc3339, "1906-02-09T17:54:34.06181Z")).unix(.microseconds)); try t.expectEqual(342848400150959, (try parse(.rfc3339, "1980-11-12T03:40:00.150959Z")).unix(.microseconds)); try t.expectEqual(-2645960576992651, (try parse(.rfc3339, "1886-02-25T10:57:03.007349Z")).unix(.microseconds)); try t.expectEqual(2460926767780856, (try parse(.rfc3339, "2047-12-25T22:46:07.780856Z")).unix(.microseconds)); try t.expectEqual(-3072719558320472, (try parse(.rfc3339, "1872-08-18T02:47:21.679528Z")).unix(.microseconds)); // milliseconds // GO // for i := 0; i < 50; i++ { // us := rand.Int63n(3153600000000000) // if i%2 == 1 { // us = -us // } // date := time.UnixMicro(us).UTC() // fmt.Printf("\ttry t.expectEqual(%d, (try DateTime.parse(\"%s\", .rfc3339)).unix(.milliseconds));\n", us/1000, date.Format(time.RFC3339Nano)) // } try t.expectEqual(1397526377500, (try parse(.rfc3339, "2014-04-15T01:46:17.500928Z")).unix(.milliseconds)); try t.expectEqual(-586731476093, (try parse(.rfc3339, "1951-05-30T03:02:03.906951Z")).unix(.milliseconds)); try t.expectEqual(2626709817261, (try parse(.rfc3339, "2053-03-27T17:36:57.261986Z")).unix(.milliseconds)); try t.expectEqual(-2699459388451, (try parse(.rfc3339, "1884-06-16T06:10:11.548899Z")).unix(.milliseconds)); try t.expectEqual(187068511670, (try parse(.rfc3339, "1975-12-06T03:28:31.670454Z")).unix(.milliseconds)); try t.expectEqual(-785593098555, (try parse(.rfc3339, "1945-02-08T11:41:41.444519Z")).unix(.milliseconds)); try t.expectEqual(2482013929293, (try parse(.rfc3339, "2048-08-26T00:18:49.293566Z")).unix(.milliseconds)); try t.expectEqual(-39404841784, (try parse(.rfc3339, "1968-10-01T22:12:38.215367Z")).unix(.milliseconds)); try t.expectEqual(1534769380821, (try parse(.rfc3339, "2018-08-20T12:49:40.821612Z")).unix(.milliseconds)); try t.expectEqual(-1980714497790, (try parse(.rfc3339, "1907-03-28T01:31:42.209908Z")).unix(.milliseconds)); try t.expectEqual(1981870811721, (try parse(.rfc3339, "2032-10-20T07:40:11.721424Z")).unix(.milliseconds)); try t.expectEqual(-554657243269, (try parse(.rfc3339, "1952-06-04T08:32:36.730587Z")).unix(.milliseconds)); try t.expectEqual(78531146024, (try parse(.rfc3339, "1972-06-27T22:12:26.024177Z")).unix(.milliseconds)); try t.expectEqual(-2360798362731, (try parse(.rfc3339, "1895-03-10T22:40:37.268319Z")).unix(.milliseconds)); try t.expectEqual(2843392029355, (try parse(.rfc3339, "2060-02-07T15:07:09.355931Z")).unix(.milliseconds)); try t.expectEqual(-1289360209568, (try parse(.rfc3339, "1929-02-21T20:23:10.431793Z")).unix(.milliseconds)); try t.expectEqual(2440116994057, (try parse(.rfc3339, "2047-04-29T02:16:34.057859Z")).unix(.milliseconds)); try t.expectEqual(-1958937239211, (try parse(.rfc3339, "1907-12-05T02:46:00.788847Z")).unix(.milliseconds)); try t.expectEqual(2092930144205, (try parse(.rfc3339, "2036-04-27T17:29:04.205599Z")).unix(.milliseconds)); try t.expectEqual(-1314934006371, (try parse(.rfc3339, "1928-05-01T20:33:13.628366Z")).unix(.milliseconds)); try t.expectEqual(1987707686213, (try parse(.rfc3339, "2032-12-26T21:01:26.21383Z")).unix(.milliseconds)); try t.expectEqual(-2863567343704, (try parse(.rfc3339, "1879-04-04T20:37:36.295226Z")).unix(.milliseconds)); try t.expectEqual(1776340450602, (try parse(.rfc3339, "2026-04-16T11:54:10.602059Z")).unix(.milliseconds)); try t.expectEqual(-135109264096, (try parse(.rfc3339, "1965-09-20T05:38:55.903281Z")).unix(.milliseconds)); try t.expectEqual(664556549013, (try parse(.rfc3339, "1991-01-22T15:02:29.013079Z")).unix(.milliseconds)); try t.expectEqual(-1265741428742, (try parse(.rfc3339, "1929-11-22T05:09:31.257333Z")).unix(.milliseconds)); try t.expectEqual(677440942549, (try parse(.rfc3339, "1991-06-20T18:02:22.549734Z")).unix(.milliseconds)); try t.expectEqual(-3086845293210, (try parse(.rfc3339, "1872-03-07T14:58:26.789666Z")).unix(.milliseconds)); try t.expectEqual(2662366721158, (try parse(.rfc3339, "2054-05-14T10:18:41.158507Z")).unix(.milliseconds)); try t.expectEqual(-35310777646, (try parse(.rfc3339, "1968-11-18T07:27:02.353055Z")).unix(.milliseconds)); try t.expectEqual(466748318057, (try parse(.rfc3339, "1984-10-16T04:18:38.057985Z")).unix(.milliseconds)); try t.expectEqual(-1142849776788, (try parse(.rfc3339, "1933-10-14T13:43:43.211425Z")).unix(.milliseconds)); try t.expectEqual(299657172861, (try parse(.rfc3339, "1979-07-01T06:06:12.86151Z")).unix(.milliseconds)); try t.expectEqual(-2674956599650, (try parse(.rfc3339, "1885-03-26T20:30:00.34904Z")).unix(.milliseconds)); try t.expectEqual(2608306771546, (try parse(.rfc3339, "2052-08-26T17:39:31.546441Z")).unix(.milliseconds)); try t.expectEqual(-2890194900832, (try parse(.rfc3339, "1878-05-31T16:04:59.167405Z")).unix(.milliseconds)); try t.expectEqual(396552033685, (try parse(.rfc3339, "1982-07-26T17:20:33.68525Z")).unix(.milliseconds)); try t.expectEqual(-107099840493, (try parse(.rfc3339, "1966-08-10T10:02:39.506219Z")).unix(.milliseconds)); try t.expectEqual(3003275118291, (try parse(.rfc3339, "2065-03-03T03:05:18.291675Z")).unix(.milliseconds)); try t.expectEqual(-1827348315834, (try parse(.rfc3339, "1912-02-05T03:14:44.165534Z")).unix(.milliseconds)); try t.expectEqual(276927903561, (try parse(.rfc3339, "1978-10-11T04:25:03.561761Z")).unix(.milliseconds)); try t.expectEqual(-2769749223625, (try parse(.rfc3339, "1882-03-25T17:12:56.374223Z")).unix(.milliseconds)); try t.expectEqual(2626498021199, (try parse(.rfc3339, "2053-03-25T06:47:01.199662Z")).unix(.milliseconds)); try t.expectEqual(-1394547124859, (try parse(.rfc3339, "1925-10-23T09:47:55.140254Z")).unix(.milliseconds)); try t.expectEqual(272330504585, (try parse(.rfc3339, "1978-08-18T23:21:44.585364Z")).unix(.milliseconds)); try t.expectEqual(-2210407675350, (try parse(.rfc3339, "1899-12-15T13:52:04.649158Z")).unix(.milliseconds)); try t.expectEqual(1506546882755, (try parse(.rfc3339, "2017-09-27T21:14:42.755649Z")).unix(.milliseconds)); try t.expectEqual(-2320627977264, (try parse(.rfc3339, "1896-06-17T21:07:02.735544Z")).unix(.milliseconds)); try t.expectEqual(2719300156090, (try parse(.rfc3339, "2056-03-03T09:09:16.090337Z")).unix(.milliseconds)); try t.expectEqual(-450791776320, (try parse(.rfc3339, "1955-09-19T12:03:43.679144Z")).unix(.milliseconds)); // seconds // GO // for i := 0; i < 50; i++ { // us := rand.Int63n(3153600000000000) // if i%2 == 1 { // us = -us // } // date := time.UnixMicro(us).UTC() // fmt.Printf("\ttry t.expectEqual(%d, (try DateTime.parse(\"%s\", .rfc3339)).unix(.milliseconds));\n", us/1000/1000, date.Format(time.RFC3339Nano)) // } try t.expectEqual(1019355037, (try parse(.rfc3339, "2002-04-21T02:10:37.264298Z")).unix(.seconds)); try t.expectEqual(-2639191098, (try parse(.rfc3339, "1886-05-14T19:21:41.481076Z")).unix(.seconds)); try t.expectEqual(552479765, (try parse(.rfc3339, "1987-07-05T10:36:05.374475Z")).unix(.seconds)); try t.expectEqual(-2842270449, (try parse(.rfc3339, "1879-12-07T08:25:50.857157Z")).unix(.seconds)); try t.expectEqual(2287542812, (try parse(.rfc3339, "2042-06-28T04:33:32.585424Z")).unix(.seconds)); try t.expectEqual(-1032056861, (try parse(.rfc3339, "1937-04-18T21:32:18.185245Z")).unix(.seconds)); try t.expectEqual(2294125759, (try parse(.rfc3339, "2042-09-12T09:09:19.324234Z")).unix(.seconds)); try t.expectEqual(-2434666174, (try parse(.rfc3339, "1892-11-05T23:50:25.855342Z")).unix(.seconds)); try t.expectEqual(2130180824, (try parse(.rfc3339, "2037-07-02T20:53:44.663679Z")).unix(.seconds)); try t.expectEqual(-2088926942, (try parse(.rfc3339, "1903-10-22T14:30:57.110159Z")).unix(.seconds)); try t.expectEqual(210188161, (try parse(.rfc3339, "1976-08-29T17:36:01.512348Z")).unix(.seconds)); try t.expectEqual(-1594811550, (try parse(.rfc3339, "1919-06-19T12:47:29.692995Z")).unix(.seconds)); try t.expectEqual(408055212, (try parse(.rfc3339, "1982-12-06T20:40:12.74791Z")).unix(.seconds)); try t.expectEqual(-763370385, (try parse(.rfc3339, "1945-10-23T16:40:14.54824Z")).unix(.seconds)); try t.expectEqual(2220686606, (try parse(.rfc3339, "2040-05-15T09:23:26.183323Z")).unix(.seconds)); try t.expectEqual(-1829267394, (try parse(.rfc3339, "1912-01-13T22:10:05.152891Z")).unix(.seconds)); try t.expectEqual(186103622, (try parse(.rfc3339, "1975-11-24T23:27:02.092278Z")).unix(.seconds)); try t.expectEqual(-104963797, (try parse(.rfc3339, "1966-09-04T03:23:22.379643Z")).unix(.seconds)); try t.expectEqual(188664629, (try parse(.rfc3339, "1975-12-24T14:50:29.082285Z")).unix(.seconds)); try t.expectEqual(-978305356, (try parse(.rfc3339, "1939-01-01T00:30:43.460779Z")).unix(.seconds)); try t.expectEqual(1857079750, (try parse(.rfc3339, "2028-11-05T23:29:10.225783Z")).unix(.seconds)); try t.expectEqual(-1059764722, (try parse(.rfc3339, "1936-06-02T04:54:37.841836Z")).unix(.seconds)); try t.expectEqual(2931563560, (try parse(.rfc3339, "2062-11-24T03:12:40.682221Z")).unix(.seconds)); try t.expectEqual(-58861051, (try parse(.rfc3339, "1968-02-19T17:42:28.861019Z")).unix(.seconds)); try t.expectEqual(2540374023, (try parse(.rfc3339, "2050-07-02T11:27:03.083527Z")).unix(.seconds)); try t.expectEqual(-369803898, (try parse(.rfc3339, "1958-04-13T20:41:41.391534Z")).unix(.seconds)); try t.expectEqual(1150522786, (try parse(.rfc3339, "2006-06-17T05:39:46.776689Z")).unix(.seconds)); try t.expectEqual(-3094311182, (try parse(.rfc3339, "1871-12-12T05:06:57.955425Z")).unix(.seconds)); try t.expectEqual(2742945297, (try parse(.rfc3339, "2056-12-02T01:14:57.552041Z")).unix(.seconds)); try t.expectEqual(-3055421456, (try parse(.rfc3339, "1873-03-06T07:49:03.861761Z")).unix(.seconds)); try t.expectEqual(1935913185, (try parse(.rfc3339, "2031-05-07T09:39:45.408961Z")).unix(.seconds)); try t.expectEqual(-1546803921, (try parse(.rfc3339, "1920-12-26T04:14:38.089431Z")).unix(.seconds)); try t.expectEqual(2430955251, (try parse(.rfc3339, "2047-01-13T01:20:51.611416Z")).unix(.seconds)); try t.expectEqual(-1162742133, (try parse(.rfc3339, "1933-02-26T08:04:26.776057Z")).unix(.seconds)); try t.expectEqual(2820984010, (try parse(.rfc3339, "2059-05-24T06:40:10.9707Z")).unix(.seconds)); try t.expectEqual(-2671779872, (try parse(.rfc3339, "1885-05-02T14:55:27.010415Z")).unix(.seconds)); try t.expectEqual(419726969, (try parse(.rfc3339, "1983-04-20T22:49:29.184213Z")).unix(.seconds)); try t.expectEqual(-2886236400, (try parse(.rfc3339, "1878-07-16T11:39:59.700923Z")).unix(.seconds)); try t.expectEqual(1091845921, (try parse(.rfc3339, "2004-08-07T02:32:01.949043Z")).unix(.seconds)); try t.expectEqual(-1345585389, (try parse(.rfc3339, "1927-05-13T02:16:50.807413Z")).unix(.seconds)); try t.expectEqual(968555612, (try parse(.rfc3339, "2000-09-10T03:13:32.056103Z")).unix(.seconds)); try t.expectEqual(-525723150, (try parse(.rfc3339, "1953-05-05T05:47:29.657935Z")).unix(.seconds)); try t.expectEqual(2179443523, (try parse(.rfc3339, "2039-01-24T00:58:43.238504Z")).unix(.seconds)); try t.expectEqual(-2200838901, (try parse(.rfc3339, "1900-04-05T07:51:38.801707Z")).unix(.seconds)); try t.expectEqual(567335109, (try parse(.rfc3339, "1987-12-24T09:05:09.535877Z")).unix(.seconds)); try t.expectEqual(-714932675, (try parse(.rfc3339, "1947-05-07T07:35:24.863781Z")).unix(.seconds)); try t.expectEqual(2735649359, (try parse(.rfc3339, "2056-09-08T14:35:59.483204Z")).unix(.seconds)); try t.expectEqual(-2386101706, (try parse(.rfc3339, "1894-05-22T01:58:13.445088Z")).unix(.seconds)); try t.expectEqual(115985094, (try parse(.rfc3339, "1973-09-04T10:04:54.005266Z")).unix(.seconds)); try t.expectEqual(-3046532170, (try parse(.rfc3339, "1873-06-17T05:03:49.260067Z")).unix(.seconds)); } test "DateTime: limits min" { const dt1 = try fromUtc(-4712, 1, 1, 0, 0, 0, 0); const dt2 = try parse(.rfc3339, "-4712-01-01T00:00:00.000000Z"); const dt3 = fromUnix(-210863520000, .seconds); const dt4 = fromUnix(-210863520000000, .milliseconds); const dt5 = fromUnix(-210863520000000000, .microseconds); for ([_]DateTime{ dt1, dt2, dt3, dt4, dt5 }) |dt| { try t.expectEqual(-4712, dt.date().year); try t.expectEqual(.january, dt.date().month); try t.expectEqual(1, dt.date().day); try t.expectEqual(0, dt.time().hour); try t.expectEqual(0, dt.time().min); try t.expectEqual(0, dt.time().sec); try t.expectEqual(0, dt.time().micros); try t.expectEqual(-210863520000, dt.unix(.seconds)); try t.expectEqual(-210863520000000, dt.unix(.milliseconds)); try t.expectEqual(-210863520000000000, dt.unix(.microseconds)); } } test "DateTime: limits max" { const dt1 = try fromUtc(9999, 12, 31, 23, 59, 59, 999999); const dt2 = try parse(.rfc3339, "9999-12-31T23:59:59.999999Z"); const dt3 = fromUnix(253402300799, .seconds); const dt4 = fromUnix(253402300799999, .milliseconds); const dt5 = fromUnix(253402300799999999, .microseconds); for ([_]DateTime{ dt1, dt2, dt3, dt4, dt5 }, 0..) |dt, i| { try t.expectEqual(9999, dt.date().year); try t.expectEqual(.december, dt.date().month); try t.expectEqual(31, dt.date().day); try t.expectEqual(23, dt.time().hour); try t.expectEqual(59, dt.time().min); try t.expectEqual(59, dt.time().sec); try t.expectEqual(253402300799, dt.unix(.seconds)); if (i == 2) { try t.expectEqual(0, dt.time().micros); try t.expectEqual(253402300799000, dt.unix(.milliseconds)); try t.expectEqual(253402300799000000, dt.unix(.microseconds)); } else if (i == 3) { try t.expectEqual(999000, dt.time().micros); try t.expectEqual(253402300799999, dt.unix(.milliseconds)); try t.expectEqual(253402300799999000, dt.unix(.microseconds)); } else { try t.expectEqual(999999, dt.time().micros); try t.expectEqual(253402300799999, dt.unix(.milliseconds)); try t.expectEqual(253402300799999999, dt.unix(.microseconds)); } } }