Search code examples
datemercurial

Get Unix timestamp (in seconds) for a hg commit


Given a commit with the date Mon Aug 18 21:05:38 2014 +0200, how can I get the Unix timestamp of it in seconds?

The following command produces a number that discards the number (presumably because the timezone information of date got discarded):

$ hg log -l1 --template '{date(date, "%s")}\n'
1408392338
$ date -d@1408392338
Mon Aug 18 22:05:38 CEST 2014

I am effectively looking for the equivalent of the git command that produces the commit date as a Unix timestamp:

git log -n1 --pretty=%ct

Solution

  • The requested timestamp was one that is independent of the timezone, so UTC time. As date(..., "%s") produces a number which is relative to the current timezone, one should request a UTC output by combining the localdate filter with the TZ environment variable to set a timezone:

    TZ=UTC hg log -l1 --template '{date(date|localdate, "%s")}\n')