Parsing dates on Linux and Mac/BSD with jq
I need to be able to create scripts that run on both Linux (WSL) and on Mac/BSD. I'm having trouble parsing dates that are returned by the Wild Apricot API.
Using this file:
This works on Linux:
jq '.StartDate | strptime("%Y-%m-%dT%H:%M:%S%Z") | strftime("%B %d, %Y")' this.json
"June 14, 2019"
On Mac/BSD, I get this error:
jq: error (at <stdin>:1): date "2019-06-14T13:00:00-04:00" does not match format "%Y-%m-%dT%H:%M:%S%Z"
I'm wondering if anyone has found a good solution.
Thank in advance.
Karl Hakkarainen commented
Answered my own question. I used split() to unpack the datetime string, remove the errant colon in the timezone field, and repack the string:
def xDate(d): d | split(":") | . + ":" + . + ":" + . + . | strptime("%Y-%m-%dT%H:%M:%S%z") ; .| xDate(.StartDate) | strftime("%B %m")