Skip to content

Commit

Permalink
Add %s format specifier for strftime (#1335)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Jul 4, 2023
1 parent 3baebea commit b30acea
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/src/manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3434,5 +3434,5 @@ MILLER(1) MILLER(1)



2023-07-02 MILLER(1)
2023-07-04 MILLER(1)
</pre>
2 changes: 1 addition & 1 deletion docs/src/manpage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3413,4 +3413,4 @@ MILLER(1) MILLER(1)



2023-07-02 MILLER(1)
2023-07-04 MILLER(1)
3 changes: 2 additions & 1 deletion docs/src/reference-dsl-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---------|-------------|
Expand Down Expand Up @@ -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` |
Expand Down
3 changes: 2 additions & 1 deletion docs/src/reference-dsl-time.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---------|-------------|
Expand Down Expand Up @@ -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` |
Expand Down
6 changes: 6 additions & 0 deletions internal/pkg/bifs/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion man/manpage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3413,4 +3413,4 @@ MILLER(1) MILLER(1)



2023-07-02 MILLER(1)
2023-07-04 MILLER(1)
4 changes: 2 additions & 2 deletions man/mlr.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/input/strfntime-tz.mlr
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}
2 changes: 1 addition & 1 deletion test/input/strftime-tz.mlr
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}

0 comments on commit b30acea

Please sign in to comment.