hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 7d4d5d5fea3dc6dbdca119d3d0e341fce8fe3268
parent a07e67cf9df27292a4edff20699ca58bb1b61ce6
Author: Haelwenn (lanodan) Monnier <contact+sr.ht@hacktivis.me>
Date:   Tue,  2 Aug 2022 06:01:54 +0200

datetime: Add %s to format

Diffstat:
Mdatetime/chronology.ha | 8+++++++-
Mdatetime/format.ha | 5+++++
2 files changed, 12 insertions(+), 1 deletion(-)

diff --git 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 @@ -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) {