Some versions of busybox date accept custom format as input using -D
option. However, after spending a good hour on this, this option does not support timezone format %Z
.
List of all supported format for C strftime
: C library function - strftime()
Note: one needs to remove the +
from the start of the output format to use as input format with -D
. E.g. "+%FT%T%z"
becomes -D "%FT%T%z"
.
Generally working conversion
## Date context for other commands
# date -Iminutes
2021-04-01T13:30+0000
date -u -D '%b %e %Y' -d "Apr 1 2021"
Thu Apr 1 00:00:00 UTC 2021
date -u -d "2021 04xx25" -D '%Y %mxx%d'
Sun Apr 25 00:00:00 UTC 2021
date -u -d "5 12:35:58" -D "%e %H:%M:%S"
Mon Apr 5 12:35:58 UTC 2021
With the timezone
## Format returned by `openssl x509 -enddate`
# date -u +"%b %e %H:%M:%S %Y %Z"
Apr 1 13:33:58 2021 UTC
# date -u -D "%b %e %H:%M:%S %Y %Z" -d "Apr 1 13:33:58 2021 UTC"
date: invalid date 'Apr 1 13:33:58 2021 UTC'
removing the %Z timezone from the input:
date -u -D "%b %e %H:%M:%S %Y" -d "Apr 1 13:33:58 2021 UTC"
Thu Apr 1 13:33:58 UTC 2021
yet providing %Z in output format works well
date -u -D "%b %e %H:%M:%S %Y" -d "Apr 1 13:33:58 2021 UTC" +"%F %T %Z"
2021-04-01 13:33:58 UTC
Busybox version and date help
Container running alpine linux busybox
v1.32.1
# uname -a
Linux f4d750b1edf8 4.19.121-linuxkit #1 SMP Thu Jan 21 15:36:34 UTC 2021 x86_64 Linux
# busybox date --help
BusyBox v1.32.1 () 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 (strptime format) for -d TIME conversion
Recognized TIME formats:
hh:mm[:ss]
[YYYY.]MM.DD-hh:mm[:ss]
YYYY-MM-DD hh:mm[:ss]
[[[[[YY]YY]MM]DD]hh]mm[.ss]
'date TIME' form accepts MMDDhhmm[[YY]YY][.ss] instead