Haelwenn (lanodan) Monnier: 2 datetime: Add %F and %T to format datetime: Add %s to format 3 files changed, 22 insertions(+), 1 deletions(-)
hare/patches: SUCCESS in 1m35s [datetime: Add %F and %T to format][0] from [Haelwenn (lanodan) Monnier][1] [0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/34377 [1]: mailto:contact+sr.ht@hacktivis.me ✓ #814748 SUCCESS hare/patches/alpine.yml https://builds.sr.ht/~sircmpwn/job/814748 ✓ #814749 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/814749
Drew on IRC says he's open to having %s. This patch looks good to me, but I'm not certain we need to introduce another exported epochunix() function, as fitting as it may be. There is also a patchset which is about to reform certain things about datetime and time::chrono which will make this use of to_instant redundant. I'd say this patch can be updated and applied once the reform is applied.
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~sircmpwn/hare-dev/patches/34377/mbox | git am -3Learn more about email & git
--- datetime/format.ha | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/datetime/format.ha b/datetime/format.ha index c226823b..46699fed 100644 --- a/datetime/format.ha +++ b/datetime/format.ha @@ -109,6 +109,8 @@ fn fmtout(out: io::handle, r: rune, dt: *datetime) (size | io::error) = { return fmt::fprint(out, MONTHS[month(dt) - 1]); case 'd' => return fmt::fprintf(out, "{:02}", day(dt)); + case 'F' => + return fmt::fprintf(out, "{:04}-{:02}-{:02}", year(dt), month(dt), day(dt)); case 'H' => return fmt::fprintf(out, "{:02}", hour(dt)); case 'I' => @@ -132,6 +134,8 @@ fn fmtout(out: io::handle, r: rune, dt: *datetime) (size | io::error) = { return fmt::fprint(out, s); case 'S' => return fmt::fprintf(out, "{:02}", sec(dt)); + case 'T' => + return fmt::fprintf(out, "{:02}:{:02}:{:02}", hour(dt), min(dt), sec(dt)); case 'u' => return fmt::fprint(out, strconv::itos(weekday(dt))); case 'U' => @@ -178,6 +182,7 @@ fn fmtout(out: io::handle, r: rune, dt: *datetime) (size | io::error) = { // %b -- The abbreviated name of the month. // %B -- The full name of the month. // %d -- The day of the month (decimal, range 01 to 31). +// %F -- The full date, equivalent to %Y-%m-%d // %H -- The hour of the day as from a 24-hour clock (range 00 to 23). // %I -- The hour of the day as from a 12-hour clock (range 01 to 12). // %j -- The ordinal day of the year (range 001 to 366). @@ -188,6 +193,7 @@ fn fmtout(out: io::handle, r: rune, dt: *datetime) (size | io::error) = { // %p -- Either "AM" or "PM" according to the current time. // "AM" includes midnight, and "PM" includes noon. // %S -- The second of the minute (range 00 to 60). +// %T -- The full time, equivalent to %H:%M:%S // %u -- The day of the week (decimal, range 1 to 7). 1 represents Monday. // %U -- The week number of the current year (range 00 to 53), // starting with the first Sunday as the first day of week 01. @@ -344,6 +350,10 @@ fn hour12(dt: *datetime) int = { ("%j", "1"), // week ("%W", "00"), + // full date + ("%F", "1994-01-01"), + // full time + ("%T", "02:17:05"), ]; for (let i = 0z; i < len(cases); i += 1) { -- 2.35.1
We have made a decision to deliberately omit certain POSIX specifiers, including the redundant "compound" ones like %F and %T, which substitute an already working set of specifiers. The reasoning is, some specifers are commonly mixed up and misused, and cause hidden bugs in the long run, like %G.
Thanks! To git@git.sr.ht:~sircmpwn/hare f48bb4f8..21a3e430 master -> master
--- datetime/chronology.ha | 8 +++++++- datetime/format.ha | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/datetime/chronology.ha b/datetime/chronology.ha index 2c9e558d..1cdb0274 100644 --- a/datetime/chronology.ha +++ b/datetime/chronology.ha @@ -13,6 +13,9 @@ use time::chrono; // Returns a [[datetime]]'s number of days since the calendar epoch 0000-01-01. export fn epochal(dt: *datetime) chrono::date = _epochal(dt); +// Returns a [[datetime]]'s number of seconds since the Unix epoch 1970-01-01. +export fn epochunix(dt: *datetime) int = _epochunix(dt); + // Returns a [[datetime]]'s era. export fn era(dt: *datetime) int = _era(dt); @@ -55,12 +58,15 @@ export fn sec(dt: *datetime) int = _sec(dt); // Returns a [[datetime]]'s nanosecond of the second. export fn nsec(dt: *datetime) int = _nsec(dt); - fn _epochal(dt: *datetime) chrono::date = { const ldt = transform(*dt, dt.zone.zoffset); return ldt.date - EPOCHAL_GREGORIAN; }; +fn _epochunix(dt: *datetime) int = { + return to_instant(*dt).sec: int; +}; + fn _era(dt: *datetime) int = { match (dt.era) { case void => diff --git a/datetime/format.ha b/datetime/format.ha index 46699fed..0638f448 100644 --- a/datetime/format.ha +++ b/datetime/format.ha @@ -132,6 +132,8 @@ fn fmtout(out: io::handle, r: rune, dt: *datetime) (size | io::error) = { yield "PM"; }; return fmt::fprint(out, s); + case 's' => + return fmt::fprintf(out, "{:02}", epochunix(dt)); case 'S' => return fmt::fprintf(out, "{:02}", sec(dt)); case 'T' => @@ -192,6 +194,7 @@ fn fmtout(out: io::handle, r: rune, dt: *datetime) (size | io::error) = { // %N -- The nanosecond of the second (range 000000000 to 999999999). // %p -- Either "AM" or "PM" according to the current time. // "AM" includes midnight, and "PM" includes noon. +// %s -- Number of seconds since 1970-01-01 00:00:00, the Unix epoch // %S -- The second of the minute (range 00 to 60). // %T -- The full time, equivalent to %H:%M:%S // %u -- The day of the week (decimal, range 1 to 7). 1 represents Monday. @@ -354,6 +357,8 @@ fn hour12(dt: *datetime) int = { ("%F", "1994-01-01"), // full time ("%T", "02:17:05"), + // Unix timestamp + ("%s", "757390625"), ]; for (let i = 0z; i < len(cases); i += 1) { -- 2.35.1
builds.sr.ht <builds@sr.ht>hare/patches: SUCCESS in 1m35s [datetime: Add %F and %T to format][0] from [Haelwenn (lanodan) Monnier][1] [0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/34377 [1]: mailto:contact+sr.ht@hacktivis.me ✓ #814748 SUCCESS hare/patches/alpine.yml https://builds.sr.ht/~sircmpwn/job/814748 ✓ #814749 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/814749
Drew on IRC says he's open to having %s. This patch looks good to me, but I'm not certain we need to introduce another exported epochunix() function, as fitting as it may be. There is also a patchset which is about to reform certain things about datetime and time::chrono which will make this use of to_instant redundant. I'd say this patch can be updated and applied once the reform is applied.