From b30aceae36ae893482bbe959e152e5fb1f8f50b7 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Tue, 4 Jul 2023 17:00:02 -0400 Subject: [PATCH] Add `%s` format specifier for `strftime` (#1335) --- docs/src/manpage.md | 2 +- docs/src/manpage.txt | 2 +- docs/src/reference-dsl-time.md | 3 ++- docs/src/reference-dsl-time.md.in | 3 ++- internal/pkg/bifs/datetime.go | 6 ++++++ man/manpage.txt | 2 +- man/mlr.1 | 4 ++-- .../dsl-local-date-time-functions/strfntime-istanbul/expout | 2 +- .../strfntime-sao_paulo/expout | 2 +- .../dsl-local-date-time-functions/strfntime-utc/expout | 2 +- .../dsl-local-date-time-functions/strftime-istanbul/expout | 2 +- .../dsl-local-date-time-functions/strftime-sao_paulo/expout | 2 +- .../cases/dsl-local-date-time-functions/strftime-utc/expout | 2 +- test/input/strfntime-tz.mlr | 2 +- test/input/strftime-tz.mlr | 2 +- 15 files changed, 23 insertions(+), 15 deletions(-) diff --git a/docs/src/manpage.md b/docs/src/manpage.md index a6220789a2..60b9f783ec 100644 --- a/docs/src/manpage.md +++ b/docs/src/manpage.md @@ -3434,5 +3434,5 @@ MILLER(1) MILLER(1) - 2023-07-02 MILLER(1) + 2023-07-04 MILLER(1) diff --git a/docs/src/manpage.txt b/docs/src/manpage.txt index f5f2c30908..749124fd1e 100644 --- a/docs/src/manpage.txt +++ b/docs/src/manpage.txt @@ -3413,4 +3413,4 @@ MILLER(1) MILLER(1) - 2023-07-02 MILLER(1) + 2023-07-04 MILLER(1) diff --git a/docs/src/reference-dsl-time.md b/docs/src/reference-dsl-time.md index d7f0111a34..867bc8dc19 100644 --- a/docs/src/reference-dsl-time.md +++ b/docs/src/reference-dsl-time.md @@ -246,7 +246,7 @@ Notes: * For `strftime`, this is thanks to [https://github.com/lestrrat-go/strftime](https://github.com/lestrrat-go/strftime), with a Miller-specific modification for fractional seconds. * For `strftime`, this is thanks to [https://github.com/pbnjay/strptime](https://github.com/pbnjay/strptime), with Miller-specific modifications. -Available format strings for `strftime`, taken directly from [https://github.com/lestrrat-go/strftime](https://github.com/lestrrat-go/strftime) except for `%1..%9`, `%N`, and `%O` which are Miller-specific additions: +Available format strings for `strftime`, taken directly from [https://github.com/lestrrat-go/strftime](https://github.com/lestrrat-go/strftime) except for `%1..%9`, `%s`, `%N`, and `%O` which are Miller-specific additions: | Pattern | Description | |---------|-------------| @@ -274,6 +274,7 @@ Available format strings for `strftime`, taken directly from [https://github.com | `%p` | national representation of either "ante meridiem" (a.m.) or "post meridiem" (p.m.) as appropriate. | | `%R` | equivalent to `%H:%M` | | `%r` | equivalent to `%I:%M:%S %p` | +| `%s` | integer seconds since the epoch | | `%S` | the second as a decimal number (00-60) | | `%1S`, ..., `%9S` | the second as a decimal number (00-60) with 1..9 decimal places, respectively | | `%T` | equivalent to `%H:%M:%S` | diff --git a/docs/src/reference-dsl-time.md.in b/docs/src/reference-dsl-time.md.in index 9259abb14a..e2e02c3970 100644 --- a/docs/src/reference-dsl-time.md.in +++ b/docs/src/reference-dsl-time.md.in @@ -178,7 +178,7 @@ Notes: * For `strftime`, this is thanks to [https://github.com/lestrrat-go/strftime](https://github.com/lestrrat-go/strftime), with a Miller-specific modification for fractional seconds. * For `strftime`, this is thanks to [https://github.com/pbnjay/strptime](https://github.com/pbnjay/strptime), with Miller-specific modifications. -Available format strings for `strftime`, taken directly from [https://github.com/lestrrat-go/strftime](https://github.com/lestrrat-go/strftime) except for `%1..%9`, `%N`, and `%O` which are Miller-specific additions: +Available format strings for `strftime`, taken directly from [https://github.com/lestrrat-go/strftime](https://github.com/lestrrat-go/strftime) except for `%1..%9`, `%s`, `%N`, and `%O` which are Miller-specific additions: | Pattern | Description | |---------|-------------| @@ -206,6 +206,7 @@ Available format strings for `strftime`, taken directly from [https://github.com | `%p` | national representation of either "ante meridiem" (a.m.) or "post meridiem" (p.m.) as appropriate. | | `%R` | equivalent to `%H:%M` | | `%r` | equivalent to `%I:%M:%S %p` | +| `%s` | integer seconds since the epoch | | `%S` | the second as a decimal number (00-60) | | `%1S`, ..., `%9S` | the second as a decimal number (00-60) with 1..9 decimal places, respectively | | `%T` | equivalent to `%H:%M:%S` | diff --git a/internal/pkg/bifs/datetime.go b/internal/pkg/bifs/datetime.go index f94da527d8..9ab5d3031f 100644 --- a/internal/pkg/bifs/datetime.go +++ b/internal/pkg/bifs/datetime.go @@ -437,6 +437,11 @@ func init() { //return append(b, []byte(s)) return append(b, s...) }) + appenderS := strftime.AppendFunc(func(b []byte, t time.Time) []byte { + epochSeconds := t.Unix() + s := fmt.Sprintf("%d", epochSeconds) + return append(b, s...) + }) ss := strftime.NewSpecificationSet() ss.Set('1', appender1) @@ -450,6 +455,7 @@ func init() { ss.Set('9', appender9) ss.Set('N', appenderN) ss.Set('O', appenderO) + ss.Set('s', appenderS) strftimeExtensions = strftime.WithSpecificationSet(ss) } diff --git a/man/manpage.txt b/man/manpage.txt index f5f2c30908..749124fd1e 100644 --- a/man/manpage.txt +++ b/man/manpage.txt @@ -3413,4 +3413,4 @@ MILLER(1) MILLER(1) - 2023-07-02 MILLER(1) + 2023-07-04 MILLER(1) diff --git a/man/mlr.1 b/man/mlr.1 index 8b3fdb2e16..8a6079eff6 100644 --- a/man/mlr.1 +++ b/man/mlr.1 @@ -2,12 +2,12 @@ .\" Title: mlr .\" Author: [see the "AUTHOR" section] .\" Generator: ./mkman.rb -.\" Date: 2023-07-02 +.\" Date: 2023-07-04 .\" Manual: \ \& .\" Source: \ \& .\" Language: English .\" -.TH "MILLER" "1" "2023-07-02" "\ \&" "\ \&" +.TH "MILLER" "1" "2023-07-04" "\ \&" "\ \&" .\" ----------------------------------------------------------------- .\" * Portability definitions .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/test/cases/dsl-local-date-time-functions/strfntime-istanbul/expout b/test/cases/dsl-local-date-time-functions/strfntime-istanbul/expout index 6e6a4997a9..585e2c8ac3 100644 --- a/test/cases/dsl-local-date-time-functions/strfntime-istanbul/expout +++ b/test/cases/dsl-local-date-time-functions/strfntime-istanbul/expout @@ -10,5 +10,5 @@ TZ is Asia/Istanbul 1970-01-01 00:00:00 +0000 1970-01-01 00:00:00 UTC 1970-01-01 00:00:00 +0000 -00 000123456 +0 000123456 00 123456 diff --git a/test/cases/dsl-local-date-time-functions/strfntime-sao_paulo/expout b/test/cases/dsl-local-date-time-functions/strfntime-sao_paulo/expout index d871916095..19ad3c0830 100644 --- a/test/cases/dsl-local-date-time-functions/strfntime-sao_paulo/expout +++ b/test/cases/dsl-local-date-time-functions/strfntime-sao_paulo/expout @@ -10,5 +10,5 @@ TZ is America/Sao_Paulo 1970-01-01 00:00:00 +0000 1970-01-01 00:00:00 UTC 1970-01-01 00:00:00 +0000 -00 000123456 +0 000123456 00 123456 diff --git a/test/cases/dsl-local-date-time-functions/strfntime-utc/expout b/test/cases/dsl-local-date-time-functions/strfntime-utc/expout index b6ea47b6fe..926c6da413 100644 --- a/test/cases/dsl-local-date-time-functions/strfntime-utc/expout +++ b/test/cases/dsl-local-date-time-functions/strfntime-utc/expout @@ -10,5 +10,5 @@ TZ is UTC 1970-01-01 00:00:00 +0000 1970-01-01 00:00:00 UTC 1970-01-01 00:00:00 +0000 -00 000123456 +0 000123456 00 123456 diff --git a/test/cases/dsl-local-date-time-functions/strftime-istanbul/expout b/test/cases/dsl-local-date-time-functions/strftime-istanbul/expout index 6dc7ae793d..06c3473f54 100644 --- a/test/cases/dsl-local-date-time-functions/strftime-istanbul/expout +++ b/test/cases/dsl-local-date-time-functions/strftime-istanbul/expout @@ -10,5 +10,5 @@ TZ is Asia/Istanbul 1970-01-01 00:00:00 +0000 1970-01-01 00:00:00 UTC 1970-01-01 00:00:00 +0000 -00 123456000 +0 123456000 00 123456000 diff --git a/test/cases/dsl-local-date-time-functions/strftime-sao_paulo/expout b/test/cases/dsl-local-date-time-functions/strftime-sao_paulo/expout index af29730669..66d9b84804 100644 --- a/test/cases/dsl-local-date-time-functions/strftime-sao_paulo/expout +++ b/test/cases/dsl-local-date-time-functions/strftime-sao_paulo/expout @@ -10,5 +10,5 @@ TZ is America/Sao_Paulo 1970-01-01 00:00:00 +0000 1970-01-01 00:00:00 UTC 1970-01-01 00:00:00 +0000 -00 123456000 +0 123456000 00 123456000 diff --git a/test/cases/dsl-local-date-time-functions/strftime-utc/expout b/test/cases/dsl-local-date-time-functions/strftime-utc/expout index ba9190df51..a8b422e801 100644 --- a/test/cases/dsl-local-date-time-functions/strftime-utc/expout +++ b/test/cases/dsl-local-date-time-functions/strftime-utc/expout @@ -10,5 +10,5 @@ TZ is UTC 1970-01-01 00:00:00 +0000 1970-01-01 00:00:00 UTC 1970-01-01 00:00:00 +0000 -00 123456000 +0 123456000 00 123456000 diff --git a/test/input/strfntime-tz.mlr b/test/input/strfntime-tz.mlr index 0fc2dd72bc..06eeded4ec 100644 --- a/test/input/strfntime-tz.mlr +++ b/test/input/strfntime-tz.mlr @@ -13,7 +13,7 @@ end { print strfntime(123456, "%Y-%m-%d %H:%M:%S %z"); print strfntime(0, "%Y-%m-%d %H:%M:%S %Z"); print strfntime(0, "%Y-%m-%d %H:%M:%S %z"); - print strfntime(123456, "%S %N"); + print strfntime(123456, "%s %N"); print strfntime(123456, "%S %O"); } diff --git a/test/input/strftime-tz.mlr b/test/input/strftime-tz.mlr index fecb46b770..f36a5ab208 100644 --- a/test/input/strftime-tz.mlr +++ b/test/input/strftime-tz.mlr @@ -13,7 +13,7 @@ end { print strftime(0.123456, "%Y-%m-%d %H:%M:%S %z"); print strftime(0.0, "%Y-%m-%d %H:%M:%S %Z"); print strftime(0.0, "%Y-%m-%d %H:%M:%S %z"); - print strftime(0.123456, "%S %N"); + print strftime(0.123456, "%s %N"); print strftime(0.123456, "%S %O"); }