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?
In Perl:
in Haskell
in Go
in C
in Swift
where (GNU Coreutils 8.24 Date manual)
+%s
, seconds since 1970-01-01 00:00:00 UTC+%N
, nanoseconds (000000000..999999999) since epochExample output now
1454000043.704350695
. I noticed that BSD manual ofdate
did not include precise explanation about the flag+%s
.In Linux or MacOS you can use:
where
+%s
, seconds since 1970-01-01 00:00:00 UTC. (GNU Coreutils 8.24 Date manual)Example output now 1454000043.
The unix 'date' command is surprisingly versatile.
Takes the output of
date
, which will be in the format defined by -f, and then prints it out (-j says don't attempt to set the date) in the form +%s, seconds since epoch.Let's try JavaScript:
...or even nicer, the static approach:
In both cases I divide by
1000
to go from seconds to millis and I useMath.floor
to only represent whole seconds that have passed (vs. rounding, which might round up to a whole second that hasn't passed yet).