I'm working in an environment where I pretty much only have access to busybox tools, and trying to convert a date in the format Mon Jan 1 23:59:59 2018 GMT
to a unix timestamp, in a shell script. I can't change the format of the input time I am parsing. It seems that busybox date
can not understand this date format, or any other format with a named month. I have a really ugly script that can do it, but does anyone know of anything nicer?
Edit: the date -D
option doesn't work for me, I get
date: invalid option -- 'D'
BusyBox v1.24.1 (2018-01-11 16:07:45 PST) multi-call binary.
Usage: date [OPTIONS] [+FMT] [TIME]`
Best Answer
The busybox
date
2 is fully capable of parsing the date in the given string with some help1 (except for the GMT time zone).The help is given with the
-D
option: a description of the source format.To get a UNIX timestamp, just add the output format expected
+'%s'
:1
The busybox
date
has most of the GNUdate
command's capabilities and one that the GNUdate
command doesn't: the-D
option. Get the busybox help as follows:Note the
-D FMT
option.2
Note that you may be able to call busybox
date
in two ways:Or, if a link to
busybox
with the namedate
has been installed in the correctPATH
directory:To verify, just ask for
--version
or--help
to find out which date you have installed.With GNU
date
:Or (busybox
date
):