7

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]`
7

The busybox date2 is fully capable of parsing the date in the given string with some help1 (except for the GMT time zone).

$ gdate='Mon Jan 1 23:59:59 2018 GMT'
$ TZ=GMT0 busybox date -d "$gdate" -D '%a %b %d %T %Y %Z'
Mon Jan  1 23:59:59 GMT 2018

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':

$  TZ=GMT0 busybox date -d "$gdate" -D '%a %b %d %T %Y %Z' +'%s'
1514851199

1
The busybox date has most of the GNU date command's capabilities and one that the GNU date command doesn't: the -D option. Get the busybox help as follows:

$ busybox date --help

BusyBox v1.27.2 (Debian 1:1.27.2-2) multi-call binary.

Usage: date [OPTIONS] [+FMT] [TIME]

Display time (using +FMT), or set time

    [-s,--set] TIME Set time to TIME
    -u,--utc        Work in UTC (don't convert to local time)
    -R,--rfc-2822   Output RFC-2822 compliant date string
    -I[SPEC]        Output ISO-8601 compliant date string
                    SPEC='date' (default) for date only,
                    'hours', 'minutes', or 'seconds' for date and
                    time to the indicated precision
    -r,--reference FILE     Display last modification time of FILE
    -d,--date TIME  Display TIME, not 'now'
    -D FMT          Use FMT for -d TIME conversion

Note the -D FMT option.


2
Note that you may be able to call busybox date in two ways:

$ busybox date

Or, if a link to busybox with the name date has been installed in the correct PATH directory:

$ date

To verify, just ask for --version or --help to find out which date you have installed.

With GNU date:

$ date --version
date (GNU coreutils) 8.28

Or (busybox date):

$ date --help
BusyBox v1.27.2 (Debian 1:1.27.2-2) multi-call binary.
…
…
  • I guess I must have a different busybox version than you're using here, all I get with your example is date: invalid option -- 'D' BusyBox v1.24.1 (2018-01-11 16:07:45 PST) multi-call binary. Usage: date [OPTIONS] [+FMT] [TIME]. It's a good answer though, I'll probably accept it if I don't get anything that will work for me here. – Nic Feb 9 '18 at 21:39
  • Actually I'm not sure how that works at all... from the documentation here busybox.net/downloads/BusyBox.html there is a set of recognized TIME formats, none of which include a named month – Nic Feb 9 '18 at 21:46
  • @Nic The version 1.24.0 is from January-2017. Strange: I downloaded that version (busybox-i686) from that link and it has the -D FMT option. Also (at the same link) you can download busybox 1.27, it is just of size 900k Byte. – Isaac Feb 9 '18 at 21:57
  • @Nic On re-reading the documentation on the page you link to it is clear that the last option is -D FMT Use FMT for -d TIME conversion. You should be able to see it. – Isaac Feb 9 '18 at 22:05
  • I see it there, and it works on my host machine, but unfortunately just doesn't work on the yocto image I'm working with – Nic Feb 13 '18 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.