Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mac using date to show seconds has "illegal time format" error

Tags:

terminal

macos

I tried the following on mac terminal, and found it has some problem:

date –j –f '%d-%b-%Y' "22-Aug-2013" "+%s"
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Could anybody help me to parse this string 22-Aug-2013 and get the epoch seconds?

like image 712
Qiang Li Avatar asked Aug 25 '13 01:08

Qiang Li


2 Answers

You're using the wrong character before your option characters j and f — you’re using an en dash (–, U+2013) rather than a hyphen (-, U+002D). Unix tools don't tend to be terribly Unicode savvy when parsing command-line arguments :-)

You may find TextWrangler/BBEdit’s Character Inspector palette useful, or if you're more of an Emacs person, M-x describe-char.

like image 74
Nicholas Riley Avatar answered Sep 20 '22 16:09

Nicholas Riley


I had no issue in OS X 10.8.4:

$ date -j -f '%d-%b-%Y' "22-Aug-2013" "+%s"
1377223888

$ date -j -f '%s' 1377223888
Thu Aug 22 21:11:28 CDT 2013

Note that it is taking the current time and including it with the specified date. It would be more accurate to explicitly set the time:

$ date -j -f '%d-%b-%Y %T' "22-Aug-2013 00:00:00" "+%s"
1377147600

$ date -j -f '%s' 1377147600
Thu Aug 22 00:00:00 CDT 2013
like image 40
James Allman Avatar answered Sep 18 '22 16:09

James Allman