2022-02-14 04:37:43 +00:00
package yqlib
import (
"testing"
)
var dateTimeOperatorScenarios = [ ] expressionScenario {
{
description : "Format: from standard RFC3339 format" ,
subdescription : "Providing a single parameter assumes a standard RFC3339 datetime format. If the target format is not a valid yaml datetime format, the result will be a string tagged node." ,
document : ` a: 2001-12-15T02:59:43.1Z ` ,
expression : ` .a |= format_datetime("Monday, 02-Jan-06 at 3:04PM") ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: Saturday, 15-Dec-01 at 2:59AM\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "Format: from custom date time" ,
subdescription : "Use with_dtf to set a custom datetime format for parsing." ,
document : ` a: Saturday, 15-Dec-01 at 2:59AM ` ,
expression : ` .a |= with_dtf("Monday, 02-Jan-06 at 3:04PM"; format_datetime("2006-01-02")) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: 2001-12-15\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "Format: get the day of the week" ,
2022-11-04 01:21:12 +00:00
document : ` a: 2001-12-15 ` ,
2022-02-14 04:37:43 +00:00
expression : ` .a | format_datetime("Monday") ` ,
expected : [ ] string {
"D0, P[a], (!!str)::Saturday\n" ,
} ,
} ,
{
description : "Now" ,
document : "a: cool" ,
expression : ` .updated = now ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: cool\nupdated: 2021-05-19T01:02:03Z\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
2023-02-02 01:42:36 +00:00
{
description : "From Unix" ,
2023-02-02 01:56:16 +00:00
subdescription : "Converts from unix time. Note, you don't have to pipe through the tz operator :)" ,
2023-02-02 01:47:59 +00:00
expression : ` 1675301929 | from_unix | tz("UTC") ` ,
2023-02-02 01:42:36 +00:00
expected : [ ] string {
2023-02-02 01:47:59 +00:00
"D0, P[], (!!timestamp)::2023-02-02T01:38:49Z\n" ,
2023-02-02 01:42:36 +00:00
} ,
} ,
2023-02-02 01:56:16 +00:00
{
description : "To Unix" ,
subdescription : "Converts to unix time" ,
expression : ` now | to_unix ` ,
expected : [ ] string {
"D0, P[], (!!int)::1621386123\n" ,
} ,
} ,
2022-02-14 04:37:43 +00:00
{
description : "Timezone: from standard RFC3339 format" ,
subdescription : "Returns a new datetime in the specified timezone. Specify standard IANA Time Zone format or 'utc', 'local'. When given a single parameter, this assumes the datetime is in RFC3339 format." ,
document : "a: cool" ,
expression : ` .updated = (now | tz("Australia/Sydney")) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: cool\nupdated: 2021-05-19T11:02:03+10:00\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "Timezone: with custom format" ,
subdescription : "Specify standard IANA Time Zone format or 'utc', 'local'" ,
document : "a: Saturday, 15-Dec-01 at 2:59AM GMT" ,
expression : ` .a |= with_dtf("Monday, 02-Jan-06 at 3:04PM MST"; tz("Australia/Sydney")) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: Saturday, 15-Dec-01 at 1:59PM AEDT\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "Add and tz custom format" ,
subdescription : "Specify standard IANA Time Zone format or 'utc', 'local'" ,
document : "a: Saturday, 15-Dec-01 at 2:59AM GMT" ,
expression : ` .a |= with_dtf("Monday, 02-Jan-06 at 3:04PM MST"; tz("Australia/Sydney")) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: Saturday, 15-Dec-01 at 1:59PM AEDT\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "Date addition" ,
document : ` a: 2021-01-01T00:00:00Z ` ,
expression : ` .a += "3h10m" ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: 2021-01-01T03:10:00Z\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "Date subtraction" ,
2022-10-14 03:24:57 +00:00
subdescription : "You can subtract durations from dates. Assumes RFC3339 date time format, see [date-time operators](https://mikefarah.gitbook.io/yq/operators/datetime#date-time-formattings) for more information." ,
2022-02-14 04:37:43 +00:00
document : ` a: 2021-01-01T03:10:00Z ` ,
expression : ` .a -= "3h10m" ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: 2021-01-01T00:00:00Z\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "Date addition - custom format" ,
document : ` a: Saturday, 15-Dec-01 at 2:59AM GMT ` ,
expression : ` with_dtf("Monday, 02-Jan-06 at 3:04PM MST"; .a += "3h1m") ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: Saturday, 15-Dec-01 at 6:00AM GMT\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "Date script with custom format" ,
subdescription : "You can embed full expressions in with_dtf if needed." ,
document : ` a: Saturday, 15-Dec-01 at 2:59AM GMT ` ,
expression : ` with_dtf("Monday, 02-Jan-06 at 3:04PM MST"; .a = (.a + "3h1m" | tz("Australia/Perth"))) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: Saturday, 15-Dec-01 at 2:00PM AWST\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
{
description : "allow comma" ,
skipDoc : true ,
document : "a: Saturday, 15-Dec-01 at 2:59AM GMT" ,
expression : ` .a |= with_dtf("Monday, 02-Jan-06 at 3:04PM MST", tz("Australia/Sydney")) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: Saturday, 15-Dec-01 at 1:59PM AEDT\n" ,
2022-02-14 04:37:43 +00:00
} ,
} ,
}
func TestDatetimeOperatorScenarios ( t * testing . T ) {
for _ , tt := range dateTimeOperatorScenarios {
testScenario ( t , & tt )
}
documentOperatorScenarios ( t , "datetime" , dateTimeOperatorScenarios )
}