Convert date from YYYYJJJ in OS X?

4

1

I have a bash script that takes a date string containing the day-of-year (Julian day) and converts it to another format. The %j format character isn't parsing the string as I'd expect per the docs (man date and man strftime). For example, when I do this:

date -j -f "%Y %j %H %M %S" "2013 2 12 34 56" "+%a %b %d %T %Z %Y"

I get:

 Sat Mar 09 12:34:56 UTC 2013

Why is it showing today's day (Sat March 09)? Shouldn't it instead show Wed Jan 02 (day 2 of 2013)?

It seems to work fine with other formats. For example, this:

date -j -f "%Y %m %d %H %M %S" "2013 01 02 12 34 56" "+%a %b %d %T %Z %Y"

correctly gives Wed Jan 02 12:34:56 UTC 2013.

What's going on? Am I using the %j character correctly? I get identical results on OS 10.10 Yosemite and 10.11 El Capitan.

jt bullitt

Posted 2017-03-09T22:49:10.627

Reputation: 41

2

I would have expected that to work also, but I find on my laptop running OS X El Capitan that if I use *date -j -f "%Y %j %H %M %S" "2017 n 12 34 56" "+%a %b %d %T %Z %Y"* where n is any number from 001 to 365, I always see "Thu Mar 09" returned, yet "man date" shows "parsing is done using strptime(3)." And per http://man7.org/linux/man-pages/man3/strptime.3.html, %j should be day of the year, but it is obviously not working.

– moonpoint – 2017-03-10T03:09:36.427

@moonpoint : Thanks for confirming this bug. (And unfortunately it appears I don't yet have privileges to up-vote your comment.) As a relative newbie to SE, I'm not sure what my most helpful next step would be: wait for other comments/answers, edit my original question and refer to your comment, post an answer to my question, or just call it done. Time for me to go read the SE FAQ... – jt bullitt – 2017-03-10T03:31:22.697

Answers

0

It's a bug. /bin/date's "%j" (day-of-year) selector is broken (at least in OS X 10.10 and 10.11). I've submitted a bug report to Apple (#30975711).

Edit: Appears to be fixed in OS X 10.12 Sierra.

jt bullitt

Posted 2017-03-09T22:49:10.627

Reputation: 41

1I think this must have been fixed in Sierra because I'm not seeing this (I'm running 10.12.3). Is there anything keeping you from going to Sierra? There's probably no hope of seeing a fix for this in older OSes, which only get critical security fixes. – Spiff – 2017-03-10T20:23:20.667

I'm relieved to hear that it's working in Sierra. I will eventually upgrade, but I'm not in a big hurry to do so -- I manage a cluster of a half-dozen Macs that would all need to be updated. Not looking forward to all that downtime. In the meantime, I may write a little workaround... – jt bullitt – 2017-03-10T22:31:43.430