mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-13 03:45:37 +00:00
Added to_unix operator
This commit is contained in:
parent
f9f340b6bf
commit
915ab69922
@ -87,7 +87,7 @@ updated: 2021-05-19T01:02:03Z
|
|||||||
```
|
```
|
||||||
|
|
||||||
## From Unix
|
## From Unix
|
||||||
Converts from unix time
|
Converts from unix time. Note, you don't have to pipe through the tz operator :)
|
||||||
|
|
||||||
Running
|
Running
|
||||||
```bash
|
```bash
|
||||||
@ -98,6 +98,18 @@ will output
|
|||||||
2023-02-02T01:38:49Z
|
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
|
## 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.
|
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.
|
||||||
|
|
||||||
|
@ -46,6 +46,7 @@ var participleYqRules = []*participleYqRule{
|
|||||||
simpleOp("now", nowOpType),
|
simpleOp("now", nowOpType),
|
||||||
simpleOp("tz", tzOpType),
|
simpleOp("tz", tzOpType),
|
||||||
simpleOp("from_?unix", fromUnixOpType),
|
simpleOp("from_?unix", fromUnixOpType),
|
||||||
|
simpleOp("to_?unix", toUnixOpType),
|
||||||
simpleOp("with_dtf", withDtFormatOpType),
|
simpleOp("with_dtf", withDtFormatOpType),
|
||||||
simpleOp("error", errorOpType),
|
simpleOp("error", errorOpType),
|
||||||
simpleOp("sortKeys", sortKeysOpType),
|
simpleOp("sortKeys", sortKeysOpType),
|
||||||
|
@ -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 nowOpType = &operationType{Type: "NOW", NumArgs: 0, Precedence: 50, Handler: nowOp}
|
||||||
var tzOpType = &operationType{Type: "TIMEZONE", NumArgs: 1, Precedence: 50, Handler: tzOp}
|
var tzOpType = &operationType{Type: "TIMEZONE", NumArgs: 1, Precedence: 50, Handler: tzOp}
|
||||||
var fromUnixOpType = &operationType{Type: "FROM_UNIX", NumArgs: 0, Precedence: 50, Handler: fromUnixOp}
|
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 encodeOpType = &operationType{Type: "ENCODE", NumArgs: 0, Precedence: 50, Handler: encodeOperator}
|
||||||
var decodeOpType = &operationType{Type: "DECODE", NumArgs: 0, Precedence: 50, Handler: decodeOperator}
|
var decodeOpType = &operationType{Type: "DECODE", NumArgs: 0, Precedence: 50, Handler: decodeOperator}
|
||||||
|
@ -170,3 +170,29 @@ func fromUnixOp(d *dataTreeNavigator, context Context, expressionNode *Expressio
|
|||||||
|
|
||||||
return context.ChildContext(results), nil
|
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
|
||||||
|
}
|
||||||
|
@ -41,12 +41,20 @@ var dateTimeOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "From Unix",
|
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")`,
|
expression: `1675301929 | from_unix | tz("UTC")`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!timestamp)::2023-02-02T01:38:49Z\n",
|
"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",
|
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.",
|
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.",
|
||||||
|
Loading…
Reference in New Issue
Block a user