8

I have a date string in the following format Jul 27 23:59:59 2016 GMT and I need to convert it to the equivalent epoch timestamp with the OS X BSD date command.

GNU date has a nice -d/--date=STRING argument:

$ date -d "Jul 27 23:59:59 2016 GMT" +'%s'
1469663999

The BSD date command on OSX sadly has no such option.

date -j -f "<FORMAT>" "Jul 27 23:59:59 2016 GMT" +'%s' seems to be the way to go, but I can't find the write format string. Apple's man page states:

date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"

can be used to parse the output from date and express it in Epoch time.

But that doesn't appear to be true:

$ date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
Failed conversion of ``Wed 13 Jul 2016 11:17:49 BST'' using format ``%a %b %d %T %Z %Y''
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]

How can I convert a date string in this format to an epoch timestamp with the OS X BSD date command?


I can't seem to get a version out of date, but I'm on OS X 10.11.5 (El Capitan) if that's significant.

tommarshall
  • 2,038
  • 5
  • 23
  • 36

4 Answers4

12

Do you mean this?

date -j -f "%a %b %d %T %Z %Y" "Wed Jul 13 11:30:27 BST 2016" +"%s"
1468405827

I worked that out by telling date to output in the same format as you were using:

date -j +"%a %b %d %T %Z %Y"
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 2
    I found that I needed to add the -u option when calling the date command in order to convert to UTC, otherwise it was using the timezone of my local machine and throwing the timestamp off. – SomethingOn Aug 23 '17 at 14:53
5

Your date command is outputting the date in the following format:

Wed 13 Jul 2016 11:17:49 BST (Format sequence is: "%a %d %b %Y %T %Z")

and you're trying to parse it with an expression to match the following format:

Wed Jul 13 11:17:49 BST 2016 (Format sequence is: "%a %b %d %T %Z %Y")

Resulting in:

Failed conversion of ``Wed 13 Jul 2016 11:17:49 BST'' using format ``%a %b %d %T %Z %Y''

So, basically, you need to change the format sequence in your command with:

$ date -j -f "%a %d %b %Y %T %Z" "`date`" +"%s"

In order to match the Wed 13 Jul 2016 11:17:49 BST format that your date command is outputting by default.

To use a custom date based on the same format:

 $ date -j -f "%a %d %b %Y %T %Z" "Wed 13 Jul 2016 11:17:49 BST" +"%s"

Some references on what [some of] the format string sequences mean:

  • %a locale's abbreviated weekday name (e.g., Sun)
  • %d day of month (e.g., 01)
  • %b locale's abbreviated month name (e.g., Jan)
  • %Y year
  • %T time; same as %H:%M:%S
  • %Z alphabetic time zone abbreviation (e.g., EDT)
pah
  • 4,700
  • 6
  • 28
  • 37
  • Thanks for your help. That format did come from Apple's man page, so I assume the docs are out of date. Do you know if the full range of date symbols for the format are documented anywhere? – tommarshall Jul 13 '16 at 10:58
  • Mark Setchell was first out of the gate with the correct answer, so I'm going to give him the tick, but I've upvoted this. Thanks for the complete response. – tommarshall Jul 13 '16 at 10:59
  • 1
    Sure. Btw, regarding the full doc for the formats, you can `man strftime` or, if unavailable, see http://man7.org/linux/man-pages/man3/strftime.3.html – pah Jul 13 '16 at 11:01
5

I ran into an issue trying to convert a similar datetime to epoch. Thought I would put this out here for this format as well. date -j -f "%Y-%m-%d %H:%M:%S" "2018-01-30 15:58:50" "+%s" 1517349530

Lab Lab
  • 781
  • 10
  • 34
Kenneth King
  • 126
  • 1
  • 2
-1

Here's another way:

$ python -c 'import time
> print int(time.time())'
1468406482
Droppy
  • 9,691
  • 1
  • 20
  • 27
  • Not my downvote, but this converts the _current_ date, not an arbitrary one. Using Python is not a bad idea actually if you require portability between Linux and MacOS (modulo the complications of Python 2 vs Python 3). – tripleee May 08 '22 at 08:30