Use 64-bit years

This change makes strftime() go faster and makes it possible to format
timestamps through the big bang to most of the stelliferous era. India
has also been added as a timezone to most binaries. Since we were able
to change the struct tm abi, this makes cosmopolitan libc superior, to
just about everything else, when it comes to standing the test of time
This commit is contained in:
Justine Tunney 2022-05-11 17:48:50 -07:00
parent 2aebda7718
commit cfc3a953ae
13 changed files with 542 additions and 482 deletions

View file

@ -2793,12 +2793,12 @@ UNIX MODULE
stdio
Allows clock_getres, clock_gettime, close, dup, dup2, dup3,
fchdir, fstat, fsync, ftruncate, getdents, getegid, getrandom,
geteuid, getgid, getgroups, getitimer, getpgid, getpgrp, getpid,
getppid, getresgid, getresuid, getrlimit, getsid, gettimeofday,
getuid, lseek, madvise, brk, mmap, mprotect, munmap, nanosleep,
pipe, pipe2, poll, pread, preadv, pwrite, pwritev, read, readv,
Allows clock_getres, clock_gettime, close, dup, fchdir, fstat,
fsync, ftruncate, getdents, getegid, getrandom, geteuid, getgid,
getgroups, getitimer, getpgid, getpgrp, getpid, getppid,
getresgid, getresuid, getrlimit, getsid, gettimeofday, getuid,
lseek, madvise, brk, mmap, mprotect, munmap, nanosleep, pipe,
pipe2, poll, pread, preadv, pwrite, pwritev, read, readv,
recvfrom, recvmsg, select, sendmsg, sendto, setitimer, shutdown,
sigaction, sigprocmask, sigreturn, socketpair, umask, wait4,
write, writev.
@ -2863,17 +2863,49 @@ UNIX MODULE
├─→ year,mon,mday,hour,min,sec,gmtoffsec,wday,yday,dst:int,zone:str
└─→ nil,unix.Errno
Breaks down UNIX timestamp into Zulu Time numbers.
Breaks down UNIX timestamp into Shaka Zulu Time numbers.
- `mon` 1 ≤ mon ≤ 12
- `mday` 1 ≤ mday ≤ 31
- `hour` 0 ≤ hour ≤ 23
- `min` 0 ≤ min ≤ 59
- `sec` 0 ≤ sec ≤ 60
- `gmtoff` ±93600 seconds
- `wday` 0 ≤ wday ≤ 6
- `yday` 0 ≤ yday ≤ 365
- `dst` 1 if daylight savings, 0 if not, -1 if not unknown
This function is like localtime() except it always returns Greenwich
Mean Time irrespective of the TZ environment variable.
For example:
>: unix.gmtime(unix.clock_gettime())
2022 5 11 22 43 20 0 3 130 0 "GMT"
Here's how you might format a localized timestamp with nanoseconds:
>: unixsec, nanos = unix.clock_gettime()
>: year,mon,mday,hour,min,sec = unix.localtime(unixsec)
>: '%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%.9dZ' % {year,mon,mday,hour,min,sec,nanos}
"2022-05-11T15:46:32.160239978Z"
`year` is the year, where zero is defined as 0 A.D. This value may
be on the interval `-13.7e9 ≤ year ≤ 10e14` which is the time from
the Big Bang, through most of the Stelliferous Era.
`mon` is the month of the year, on the interval `1 ≤ mon ≤ 12` in
order to make printf style formatting easier.
`mday` is the day of the month, on the interval `1 ≤ mday ≤ 31` in
order to make printf style formatting easier.
`hour` represent hours, on the interval `0 ≤ hour ≤ 23`. - `min`
represents minutes, on the interval `0 ≤ min ≤ 59`.
`sec` represents seconds, on the interval `0 ≤ sec ≤ 60`. Please
note this is a 61 second interval in order to accommodate highly
rare leap second events.
`wday` is the day of the week, on the interval `0 ≤ wday ≤ 6`.
`yday` is the day of the year on the interval `0 ≤ yday ≤ 365`.
`gmtoff` is the Shaka Zulu time offset in seconds, which should be
on the interval ±93600 seconds.
`dst` will be 1 if daylight savings, 0 if not daylight savings, or
-1 if it couldn't be determined.
unix.localtime(unixts:int)
├─→ year,mon,mday,hour,min,sec,gmtoffsec,wday,yday,dst:int,zone:str
@ -2884,7 +2916,21 @@ UNIX MODULE
>: unix.localtime(unix.clock_gettime())
2022 4 28 2 14 22 -25200 4 117 1 "PDT"
This follows the same API as gmtime() which has further details.
This follows the same API as gmtime() except it takes the `TZ`
environment variable into consideration to determine the most
appropriate localization.
Please see the gmtime() function for documentaiton on the meaning of
the various returned values.
Here's an example of how you might format a localized timestamp:
>: unixsec, nanos = unix.clock_gettime()
>: year, mon, mday, hour, min, sec, gmtoffsec = unix.localtime(unixsec)
>: '%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%.9d%+.2d%.2d' % {
year, mon, mday, hour, min, sec, nanos,
gmtoffsec / (60 * 60), math.abs(gmtoffsec) % 60}
"2022-05-11T15:46:32.160239978-0700"
Your redbean ships with a subset of the time zone database.