andig
andig

Reputation: 13898

Use jq to parse local time into Golang RFC 3339

I have "2024-12-01 00:00:00" in local TZ (Europe/Berlin) and want to parse/convert this into RFC3339 for Go time.Parse which would be "2024-12-01T00:00:00+01:00".

I've tried various variants of

strptime("%Y-%m-%d %H:%M:%S") | strflocaltime("%Y-%m-%dT%H:%M:%S%z")

but I can't get the timezone out in a suitable format since RFC 3339 insists on the ::

RFC3339     = "2006-01-02T15:04:05Z07:00"

and the above format returns

2024-12-01T00:00:00+0100

which is missing the : in the timezone.

How can I get proper timezone format out of the JQ expression (or parse/convert the timestamp so it can be output as UTC)?

Upvotes: 0

Views: 69

Answers (2)

Philippe
Philippe

Reputation: 26727

You can use sub to convert to the required format :

jq -nr '"2024-12-01 00:00:00" | strptime("%F %T") | strflocaltime("%FT%T%z") | sub("\\+(?<d>[0-9][0-9])";"Z\(.d):")'
# output: 2024-12-01T00:00:00Z01:00

Upvotes: 0

pmf
pmf

Reputation: 36391

Use %:z instead of %z. (You can also shorten %Y-%m-%d to %F and %H:%M:%S to %T):

gojq -nr '"2024-12-01 00:00:00" | strptime("%F %T") | strflocaltime("%FT%T%:z")'
2024-12-01T00:00:00+01:00

Tested with gojq 0.12.16

Upvotes: 1

Related Questions