Get current time in seconds since the Epoch on Linux, Bash

前端 未结 7 1495
长发绾君心
长发绾君心 2020-12-02 03:42

I need something simple like date, but in seconds since 1970 instead of the current date, hours, minutes, and seconds.

date doesn\'t seem t

7条回答
  •  独厮守ぢ
    2020-12-02 03:55

    This is an extension to what @pellucide has done, but for Macs:

    To determine the number of seconds since epoch (Jan 1 1970) for any given date (e.g. Oct 21 1973)

    $ date -j -f "%b %d %Y %T" "Oct 21 1973 00:00:00" "+%s"
    120034800
    

    Please note, that for completeness, I have added the time part to the format. The reason being is that date will take whatever date part you gave it and add the current time to the value provided. For example, if you execute the above command at 4:19PM, without the '00:00:00' part, it will add the time automatically. Such that "Oct 21 1973" will be parsed as "Oct 21 1973 16:19:00". That may not be what you want.

    To convert your timestamp back to a date:

    $ date -j -r 120034800
    Sun Oct 21 00:00:00 PDT 1973
    

    Apple's man page for the date implementation: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/date.1.html

提交回复
热议问题