mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-26 00:15:36 +00:00
Added from_unix operator #1535
This commit is contained in:
parent
d21bb920d6
commit
75920481b1
@ -86,6 +86,18 @@ a: cool
|
|||||||
updated: 2021-05-19T01:02:03Z
|
updated: 2021-05-19T01:02:03Z
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## From Unix
|
||||||
|
Converts from unix time
|
||||||
|
|
||||||
|
Running
|
||||||
|
```bash
|
||||||
|
yq --null-input '1675301929 | from_unix'
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
2023-02-02T12:38:49+11:00
|
||||||
|
```
|
||||||
|
|
||||||
## 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.
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ var participleYqRules = []*participleYqRule{
|
|||||||
simpleOp("format_datetime", formatDateTimeOpType),
|
simpleOp("format_datetime", formatDateTimeOpType),
|
||||||
simpleOp("now", nowOpType),
|
simpleOp("now", nowOpType),
|
||||||
simpleOp("tz", tzOpType),
|
simpleOp("tz", tzOpType),
|
||||||
|
simpleOp("from_?unix", fromUnixOpType),
|
||||||
simpleOp("with_dtf", withDtFormatOpType),
|
simpleOp("with_dtf", withDtFormatOpType),
|
||||||
simpleOp("error", errorOpType),
|
simpleOp("error", errorOpType),
|
||||||
simpleOp("sortKeys", sortKeysOpType),
|
simpleOp("sortKeys", sortKeysOpType),
|
||||||
|
@ -93,6 +93,7 @@ var formatDateTimeOpType = &operationType{Type: "FORMAT_DATE_TIME", NumArgs: 1,
|
|||||||
var withDtFormatOpType = &operationType{Type: "WITH_DATE_TIME_FORMAT", NumArgs: 1, Precedence: 50, Handler: withDateTimeFormat}
|
var withDtFormatOpType = &operationType{Type: "WITH_DATE_TIME_FORMAT", NumArgs: 1, Precedence: 50, Handler: withDateTimeFormat}
|
||||||
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 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}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"container/list"
|
"container/list"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@ -129,3 +130,43 @@ func tzOp(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode)
|
|||||||
|
|
||||||
return context.ChildContext(results), nil
|
return context.ChildContext(results), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseUnixTime(unixTime string) (time.Time, error) {
|
||||||
|
seconds, err := strconv.ParseFloat(unixTime, 64)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return time.Now(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.UnixMilli(int64(seconds * 1000)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromUnixOp(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
|
|
||||||
|
var results = list.New()
|
||||||
|
|
||||||
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
||||||
|
candidate := el.Value.(*CandidateNode)
|
||||||
|
|
||||||
|
actualTag := guessTagFromCustomType(candidate.Node)
|
||||||
|
|
||||||
|
if actualTag != "!!int" && guessTagFromCustomType(candidate.Node) != "!!float" {
|
||||||
|
return Context{}, fmt.Errorf("from_unix only works on numbers, found %v instead", candidate.Node.Tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedTime, err := parseUnixTime(candidate.Node.Value)
|
||||||
|
if err != nil {
|
||||||
|
return Context{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
node := &yaml.Node{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Tag: "!!timestamp",
|
||||||
|
Value: parsedTime.Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
|
||||||
|
results.PushBack(candidate.CreateReplacement(node))
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.ChildContext(results), nil
|
||||||
|
}
|
||||||
|
@ -39,6 +39,14 @@ var dateTimeOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (doc)::a: cool\nupdated: 2021-05-19T01:02:03Z\n",
|
"D0, P[], (doc)::a: cool\nupdated: 2021-05-19T01:02:03Z\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "From Unix",
|
||||||
|
subdescription: "Converts from unix time",
|
||||||
|
expression: `1675301929 | from_unix`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!timestamp)::2023-02-02T12:38:49+11:00\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