DateTimeToUnixNs
Description
Inverse of DateTimeFromUnixNs: collapse a DateTime back to UTC nanoseconds since the Unix epoch. The instant’s utc_offset_seconds is subtracted so the result is always UTC, independent of how the DateTime was rendered. Round-trips exactly with DateTimeFromUnixNs.
Parameters
| Name | Direction | Description |
|---|---|---|
dt |
in | Instant to collapse. |
Success
Returns UTC nanoseconds since the Unix epoch.
Failure
Cannot fail.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Io.c:1749:
// Derive the weekday from the canonical conversion so a parsed value
// carries a correct weekday regardless of the input text.
tmp.weekday = DateTimeFromUnixNs(DateTimeToUnixNs(tmp), offset).weekday;
*dt = tmp;
return p;- In
DateTime.c:73:
}
u64 DateTimeToUnixNs(DateTime dt) {
i64 days = days_from_civil(dt.year, dt.month, dt.day);
i64 local_sec = days * SECS_PER_DAY + (i64)dt.hour * 3600 + (i64)dt.minute * 60 + (i64)dt.second;- In
DateTime.c:81:
i32 DateTimeCompare(DateTime a, DateTime b) {
u64 ua = DateTimeToUnixNs(a);
u64 ub = DateTimeToUnixNs(b);
if (ua < ub)- In
DateTime.c:82:
i32 DateTimeCompare(DateTime a, DateTime b) {
u64 ua = DateTimeToUnixNs(a);
u64 ub = DateTimeToUnixNs(b);
if (ua < ub)
return -1;- In
DateTime.c:91:
i64 DateTimeDiffNs(DateTime a, DateTime b) {
return (i64)DateTimeToUnixNs(a) - (i64)DateTimeToUnixNs(b);
}- In
DateTime.c:95:
DateTime DateTimeAddNs(DateTime dt, i64 delta_ns) {
u64 base = DateTimeToUnixNs(dt);
return DateTimeFromUnixNs((u64)((i64)base + delta_ns), dt.utc_offset_seconds);
}- In
DateTime.c:20:
for (u32 o = 0; o < sizeof(offsets) / sizeof(offsets[0]); ++o) {
DateTime d = DateTimeFromUnixNs(samples[s], offsets[o]);
if (DateTimeToUnixNs(d) != samples[s]) {
return false;
}- In
DateTime.c:31:
DateTime epoch = {.year = 1970, .month = 1, .day = 1};
DateTime day1 = {.year = 1970, .month = 1, .day = 2};
return DateTimeToUnixNs(epoch) == 0 && DateTimeToUnixNs(day1) == 86400ull * NS_PER_SEC;
}- In
DateTime.c:84:
for (u32 m = 1; m <= 12; ++m) {
DateTime d = {.year = years[yi], .month = (u8)m, .day = 15, .hour = 13, .minute = 7, .second = 5};
DateTime r = DateTimeFromUnixNs(DateTimeToUnixNs(d), 0);
if (r.year != years[yi] || r.month != (u8)m || r.day != 15 || r.hour != 13 || r.minute != 7 ||
r.second != 5) {- In
DateTime.c:96:
static bool test_leap_day_roundtrip(void) {
DateTime feb29 = {.year = 2024, .month = 2, .day = 29, .hour = 23, .minute = 59, .second = 59};
DateTime r = DateTimeFromUnixNs(DateTimeToUnixNs(feb29), 0);
return r.year == 2024 && r.month == 2 && r.day == 29 && r.hour == 23 && r.minute == 59 && r.second == 59;
}- In
DateTime.c:117:
DateTime jan1 = {.year = y, .month = 1, .day = 1};
DateTime dec31 = {.year = y, .month = 12, .day = 31};
DateTime rj = DateTimeFromUnixNs(DateTimeToUnixNs(jan1), 0);
DateTime rd = DateTimeFromUnixNs(DateTimeToUnixNs(dec31), 0);
if (rj.year != y || rj.month != 1 || rj.day != 1) {- In
DateTime.c:118:
DateTime dec31 = {.year = y, .month = 12, .day = 31};
DateTime rj = DateTimeFromUnixNs(DateTimeToUnixNs(jan1), 0);
DateTime rd = DateTimeFromUnixNs(DateTimeToUnixNs(dec31), 0);
if (rj.year != y || rj.month != 1 || rj.day != 1) {
return false;- In
DateTime.c:135:
};
for (u32 i = 0; i < sizeof(exact) / sizeof(exact[0]); ++i) {
DateTime r = DateTimeFromUnixNs(DateTimeToUnixNs(exact[i]), 0);
if (r.year != exact[i].year || r.month != exact[i].month || r.day != exact[i].day) {
return false;
Last updated on