How can I convert a date with a named month to a unix timestamp with only Busybox tools?

Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
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]`
linux date busybox
add a comment |Â
up vote
7
down vote
favorite
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]`
linux date busybox
1
Possible duplicate of BusyBox Date Command Set Time with UNIX Timestamp
â Jeff Schaller
Feb 9 at 18:34
1
See also: unix.stackexchange.com/a/296073/117549
â Jeff Schaller
Feb 9 at 18:35
1
@JeffSchaller No, the BusyBox Date Command Set Time with UNIX Timestamp is about setting the computer time, it does not use the-doption for a date string.
â Isaac
Feb 9 at 18:50
Thanks, @isaac; I'll retract my vote to avoid misleading others.
â Jeff Schaller
Feb 9 at 18:56
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
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]`
linux date busybox
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]`
linux date busybox
edited Feb 9 at 21:50
asked Feb 9 at 18:22
Nic
1385
1385
1
Possible duplicate of BusyBox Date Command Set Time with UNIX Timestamp
â Jeff Schaller
Feb 9 at 18:34
1
See also: unix.stackexchange.com/a/296073/117549
â Jeff Schaller
Feb 9 at 18:35
1
@JeffSchaller No, the BusyBox Date Command Set Time with UNIX Timestamp is about setting the computer time, it does not use the-doption for a date string.
â Isaac
Feb 9 at 18:50
Thanks, @isaac; I'll retract my vote to avoid misleading others.
â Jeff Schaller
Feb 9 at 18:56
add a comment |Â
1
Possible duplicate of BusyBox Date Command Set Time with UNIX Timestamp
â Jeff Schaller
Feb 9 at 18:34
1
See also: unix.stackexchange.com/a/296073/117549
â Jeff Schaller
Feb 9 at 18:35
1
@JeffSchaller No, the BusyBox Date Command Set Time with UNIX Timestamp is about setting the computer time, it does not use the-doption for a date string.
â Isaac
Feb 9 at 18:50
Thanks, @isaac; I'll retract my vote to avoid misleading others.
â Jeff Schaller
Feb 9 at 18:56
1
1
Possible duplicate of BusyBox Date Command Set Time with UNIX Timestamp
â Jeff Schaller
Feb 9 at 18:34
Possible duplicate of BusyBox Date Command Set Time with UNIX Timestamp
â Jeff Schaller
Feb 9 at 18:34
1
1
See also: unix.stackexchange.com/a/296073/117549
â Jeff Schaller
Feb 9 at 18:35
See also: unix.stackexchange.com/a/296073/117549
â Jeff Schaller
Feb 9 at 18:35
1
1
@JeffSchaller No, the BusyBox Date Command Set Time with UNIX Timestamp is about setting the computer time, it does not use the
-d option for a date string.â Isaac
Feb 9 at 18:50
@JeffSchaller No, the BusyBox Date Command Set Time with UNIX Timestamp is about setting the computer time, it does not use the
-d option for a date string.â Isaac
Feb 9 at 18:50
Thanks, @isaac; I'll retract my vote to avoid misleading others.
â Jeff Schaller
Feb 9 at 18:56
Thanks, @isaac; I'll retract my vote to avoid misleading others.
â Jeff Schaller
Feb 9 at 18:56
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
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 isdate: 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 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 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 FMToption. Also (at the same link) you can download busybox 1.27, it is just of size 900k Byte.
â Isaac
Feb 9 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 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 at 22:38
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
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 isdate: 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 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 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 FMToption. Also (at the same link) you can download busybox 1.27, it is just of size 900k Byte.
â Isaac
Feb 9 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 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 at 22:38
add a comment |Â
up vote
7
down vote
accepted
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 isdate: 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 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 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 FMToption. Also (at the same link) you can download busybox 1.27, it is just of size 900k Byte.
â Isaac
Feb 9 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 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 at 22:38
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
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.
â¦
â¦
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.
â¦
â¦
edited Feb 10 at 8:27
JdeBP
28.4k459133
28.4k459133
answered Feb 9 at 18:45
Isaac
6,6381734
6,6381734
I guess I must have a different busybox version than you're using here, all I get with your example isdate: 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 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 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 FMToption. Also (at the same link) you can download busybox 1.27, it is just of size 900k Byte.
â Isaac
Feb 9 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 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 at 22:38
add a comment |Â
I guess I must have a different busybox version than you're using here, all I get with your example isdate: 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 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 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 FMToption. Also (at the same link) you can download busybox 1.27, it is just of size 900k Byte.
â Isaac
Feb 9 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 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 at 22:38
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 at 21:39
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 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 at 21:46
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 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 at 21:57
@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 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 at 22:05
@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 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 at 22:38
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 at 22:38
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f423101%2fhow-can-i-convert-a-date-with-a-named-month-to-a-unix-timestamp-with-only-busybo%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Possible duplicate of BusyBox Date Command Set Time with UNIX Timestamp
â Jeff Schaller
Feb 9 at 18:34
1
See also: unix.stackexchange.com/a/296073/117549
â Jeff Schaller
Feb 9 at 18:35
1
@JeffSchaller No, the BusyBox Date Command Set Time with UNIX Timestamp is about setting the computer time, it does not use the
-doption for a date string.â Isaac
Feb 9 at 18:50
Thanks, @isaac; I'll retract my vote to avoid misleading others.
â Jeff Schaller
Feb 9 at 18:56