gojq

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)?


Solution

  • 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