5

I have seen that in order to convert an epoch into a datetime the following can be done:

date -d  @1346338800 +'%Y%m%d%H%M%S'

How can I do the opposite in Linux? I.E. convert 20191027163020 into an epoch value? Is it possible to do without doing a Python or other programming language script?

2 Answers 2

4

Linux is just a kernel, it has no bearing on that Q&A.

With the busybox implementation of date (GNU date are also considering implementing a similar option; GNU and busybox date are the two implementations commonly found on systems that use Linux as their kernel that I know that support your date -d @xxx syntax).

$ date -D %Y%m%d%H%M%S -d 20191027163020 +%s
1572193820

With the ast-open implementation of date (like the date builtin of ksh93 when built as part of ast-open):

$ date -p %Y%m%d%H%M%S -d 20191027163020 +%s
1572193820

With the strftime builtin of zsh:

$ zmodload zsh/datetime
$ strftime -r %Y%m%d%H%M%S 20191027163020
1572193820

With current versions of GNU date, you could transform it into a format it supports for its -d/-f (like 2019-10-27T16:30:20):

t=20191027163020
printf '%s%s-%s-%sT%s:%s:%s\n' $(printf '%s\n' "$t" | fold -w2) |
  date -f - +%s

Note that they all work on local time. As the timezone offset is not included in your format, it's ambiguous.

For instance, in mainland UK here, 20191027011200 could be either 1572138720 or 1572135120 as the clock showed 01:12:00 twice with one hour interval after the switch to winter time.

$ date -d @1572138720 +%Y%m%d%H%M%S
20191027011200
$ date -d @1572135120 +%Y%m%d%H%M%S
20191027011200

You may want to work with UTC times always to avoid this kind of problem (by setting the $TZ environment variable to UTC0 for instance).

6
  • 1
    What would you call the group of OSes that use Linux as their kernel?
    – user232326
    Oct 29, 2019 at 16:46
  • @Isaac, I don't see much point in giving a name encompassing all of them as there are huge differences between them (like between RHEL, OpenWRT, Android, ChromeOS...). GNU/Linux could be a good one for the subset of them that have a GNU userland (Debian GNU/Linux, RHEL...), though in this Q&A the Linux part is not relevant (the answer is the same for Debian GNU/Linux and Debian GNU/kFreeBSD for instance). Most useful is to specify the OS (Debian GNU/Linux for instance). Oct 29, 2019 at 16:52
  • 1
    Even if you dislike the idea, most people do give a generic name encompassing all of them, and that name is Linux. That is a language issue, not a formal, technical, personal or even taste issue. Maybe you should acknowledge and adapt to this fact.
    – user232326
    Oct 29, 2019 at 18:21
  • In any case, a discussion of what is or is not some OS should not be bought to bear in this answer IMhO, and I invite you to just remove that (conflicting) initial comment.
    – user232326
    Oct 29, 2019 at 18:23
  • In Debian, ksh93 reports no date builtin (output of ksh93 -c "builtin").
    – user232326
    Oct 29, 2019 at 22:38
2

In general, the date command (the implementations that have a -d option) could convert from some ISO8601 formatted date strings to an epoch number.

$ date -d '2019-10-27 16:30:20'
Sun 27 Oct 2019 04:30:20 PM EDT

$ busybox date -d '2019-10-27 16:30:20' +%s
1572208220

But if you want an answer for a general format, you need to declare the specific format in which the date string is written. That could be done with the -f option for *BSD date (no -d option), the -D option of busybox date, the option -p for ast-open, or the variable DATEMSK in ksh93.

date -j -f %Y%m%d%H%M%S 20191027163020 +%s                            # *BSD

busybox date -D %Y%m%d%H%M%S -d 20191027163020 +%s                    # busybox

date -p %Y%m%d%H%M%S -d 20191027163020 +%s                            # ast-open

DATEMSK=/dev/fd/3 3<<<%Y%m%d%H%M%S printf "%(%s)T\n" 20191027163020   # ksh93

In GNU date you need to do the formatting by hand:

$ t=20191027163020
$ T=$(echo "$t" | sed -E 's/(....)(..)(..)(..)(..)(..)/\1-\2-\3T\4:\5:\6/')

$ echo "$T"
2019-10-27T16:30:20

$ date -d "$T" +%s 
1572208220

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .