Added to_unix operator

This commit is contained in:
Mike Farah 2023-02-02 12:56:16 +11:00
parent f9f340b6bf
commit 915ab69922
5 changed files with 50 additions and 2 deletions

View File

@ -87,7 +87,7 @@ updated: 2021-05-19T01:02:03Z
```
## From Unix
Converts from unix time
Converts from unix time. Note, you don't have to pipe through the tz operator :)
Running
```bash
@ -98,6 +98,18 @@ will output
2023-02-02T01:38:49Z
```
## To Unix
Converts to unix time
Running
```bash
yq --null-input 'now | to_unix'
```
will output
```yaml
1621386123
```
## Timezone: from standard RFC3339 format
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.

View File

@ -46,6 +46,7 @@ var participleYqRules = []*participleYqRule{
simpleOp("now", nowOpType),
simpleOp("tz", tzOpType),
simpleOp("from_?unix", fromUnixOpType),
simpleOp("to_?unix", toUnixOpType),
simpleOp("with_dtf", withDtFormatOpType),
simpleOp("error", errorOpType),
simpleOp("sortKeys", sortKeysOpType),

View File

@ -94,6 +94,7 @@ var withDtFormatOpType = &operationType{Type: "WITH_DATE_TIME_FORMAT", NumArgs:
var nowOpType = &operationType{Type: "NOW", NumArgs: 0, Precedence: 50, Handler: nowOp}
var tzOpType = &operationType{Type: "TIMEZONE", NumArgs: 1, Precedence: 50, Handler: tzOp}
var fromUnixOpType = &operationType{Type: "FROM_UNIX", NumArgs: 0, Precedence: 50, Handler: fromUnixOp}
var toUnixOpType = &operationType{Type: "TO_UNIX", NumArgs: 0, Precedence: 50, Handler: toUnixOp}
var encodeOpType = &operationType{Type: "ENCODE", NumArgs: 0, Precedence: 50, Handler: encodeOperator}
var decodeOpType = &operationType{Type: "DECODE", NumArgs: 0, Precedence: 50, Handler: decodeOperator}

View File

@ -170,3 +170,29 @@ func fromUnixOp(d *dataTreeNavigator, context Context, expressionNode *Expressio
return context.ChildContext(results), nil
}
func toUnixOp(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
layout := context.GetDateTimeLayout()
var results = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
parsedTime, err := parseDateTime(layout, candidate.Node.Value)
if err != nil {
return Context{}, fmt.Errorf("could not parse datetime of [%v] using layout [%v]: %w", candidate.GetNicePath(), layout, err)
}
node := &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!int",
Value: fmt.Sprintf("%v", parsedTime.Unix()),
}
results.PushBack(candidate.CreateReplacement(node))
}
return context.ChildContext(results), nil
}

View File

@ -41,12 +41,20 @@ var dateTimeOperatorScenarios = []expressionScenario{
},
{
description: "From Unix",
subdescription: "Converts from unix time",
subdescription: "Converts from unix time. Note, you don't have to pipe through the tz operator :)",
expression: `1675301929 | from_unix | tz("UTC")`,
expected: []string{
"D0, P[], (!!timestamp)::2023-02-02T01:38:49Z\n",
},
},
{
description: "To Unix",
subdescription: "Converts to unix time",
expression: `now | to_unix`,
expected: []string{
"D0, P[], (!!int)::1621386123\n",
},
},
{
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.",