Today I made a few API calls to debug something and took a look at the timestamps the objects were created at. The time was saved in ISO-8601 UTC but I needed it in my own timezone so I wrote a little shell one-liner.
I however noticed that most articles online describe how it’s done for GNU date, but my Mac OSX had BSD date installed. Here is how you solve it on Macs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# First get the data curl "https://jsonAPI.example.com/gimmeAllTheThings.json | # Parse it into a human readable format using python's built in JSON module python -mjson.tool | # In my case I only wanted to know about the created_at fields. # I had one on the root level of each element and one in a sub-element # so I added a quick hack to only take the one prefixed with # 12 whitespaces (the root level per element) instead of 14 (one level deeper) # Also drop the 'Z' as date doesn't understand it and needs +0000 as timezone # I only display the date using regex capture groups and --output instead of # displaying the full line ack --output "\$1" '^\s{12}"created_at": "(.*)Z"' | # TZ is an environment variable to set the target timezone # xargs iterates over each line again and executes date. # {} is being replaced by the actual content of the line, i.e. the date # -jf tells date the date format of the incoming date # we append +0000 to signal UTC ( 'Z' is not recognized ) # + signals a user-defined format string for the output following strftime syntax TZ=America/New_York xargs -I {} date -jf "%Y-%m-%dT%H:%M:%S%z" "{}+0000" +"%Y-%m-%d %H:%M:%S %z" In one line: curl "https://jsonAPI.example.com/gimmeAllTheThings.json | python -mjson.tool | ack --output "\$1" '^\s{12}"created_at": "(.*)Z"' | TZ=America/New_York xargs -I {} date -jf "%Y-%m-%dT%H:%M:%S%z" "{}+0000" +"%Y-%m-%d %H:%M:%S %z" |
Hope this was useful to you. If so, tweet about it, maybe some of your followers might find it useful as well.