Related question is "Datetime To Unix timestamp", but this question is more general.
I need Unix timestamps to solve my last question. My interests are Python, Ruby and Haskell, but other approaches are welcome.
What is the easiest way to generate Unix timestamps?
First of all, the Unix 'epoch' or zero-time is 1970-01-01 00:00:00Z (meaning midnight of 1st January 1970 in the Zulu or GMT or UTC time zone). A Unix time stamp is the number of seconds since that time - not accounting for leap seconds.
Generating the current time in Perl is rather easy:
Generating the time corresponding to a given date/time value is rather less easy. Logically, you use the
strptime()
function from POSIX. However, the Perl POSIX::strptime module (which is separate from the POSIX module) has the signature:The function
mktime
in the POSIX module has the signature:So, if you know the format of your data, you could write a variant on:
With NodeJS, just open a terminal and type:
node -e "console.log(new Date().getTime())"
ornode -e "console.log(Date.now())"
If you need a Unix timestamp from a shell script (Bourne family: sh, ksh, bash, zsh, ...), this should work on any Unix machine as unlike the other suggestions (perl, haskell, ruby, python, GNU date), it is based on a POSIX standard command and feature.