Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include current time stamp in jq command?

Tags:

json

jq

I am running a curl rest-api call and try to extra some key/value pairs in UBUNTU. This is my current command:

curl ..... | jq -c '{"online": .switches.optional.online, "offline": .switches.optional.offline}'

and the output I've received is as this:

{ "online": 85, "offline": 196 }

But what I am really looking for is to have the current time-stamp included the json body, something as if:

   { "current-time": "Wed Apr 15 14:18:42 PDT 2020", "online": 85, "offline": 196 }

The API response body does not have the current timestamp message, can this be triggered by jq itself ?

Thanks.

Jack

like image 908
user3595231 Avatar asked Oct 25 '25 10:10

user3595231


1 Answers

jq has the now builtin:

TZ=UTC jq -n 'now | strftime("%a %b %d, %Y %Z %H:%M:%S")'
"Wed Apr 15, 2020 UTC 21:51:07"

Note that the environment variable TZ will affect the %Z portion of the string produced by strftime, but not the numerical time portion:

TZ=Australia/Sydney jq -n 'now | strftime("%a %b %d, %Y %Z %H:%M:%S")'
"Wed Apr 15, 2020 AEST 21:52:19"

By contrast, the strflocaltime function of both jq and gojq (the Go implementation of jq) will present the "local time" relative to TZ:

$ gojq -n 'now | strflocaltime("%a %b %d, %Y %Z %H:%M:%S")'
"Wed May 04, 2022 EDT 17:39:48"

$ TZ=Australia/Sydney gojq -n 'now | strflocaltime("%a %b %d, %Y %Z %H:%M:%S")'
"Thu May 05, 2022 AEST 07:40:00"

$ TZ=Australia/Sydney jq -n 'now | strflocaltime("%a %b %d, %Y %Z %H:%M:%S")'
"Thu May 05, 2022 AEST 07:40:00"
like image 145
peak Avatar answered Oct 27 '25 00:10

peak