how to get UTC timestamp for commit?

Christian Ebert blacktrash at gmx.net
Tue Aug 18 02:32:57 UTC 2015


* Matt Mackall on Monday, August 17, 2015 at 14:03:33 -0500
> On Mon, 2015-08-17 at 10:18 +0200, Olaf Hering wrote:
>> Assume this output:
>> 
>> # mutt-hg $ hg log | head -n 4
>> # changeset:   6479:55ea6e829b46
>> # tag:         tip
>> # user:        Kevin McCarthy <kevin at 8t8.us>
>> # date:        Tue Aug 11 11:20:15 2015 -0700
>> 
>> I want to get the UTC date of this commit, like:
>> 
>> # mutt-hg $ hg tip --template "{date(date, '%Y%m%dT%H%M%S')}.r{rev}\n"
>> # 20150811T112015.r6479
>> 
>> But it is reported as localtime.
> 
> It's not reported in localtime (your localtime is apparently +0200).
> It's reported in the timezone of the committer (like email).
> 
>> Looking through "hg help templates" I see a
>> reference to "hg help dates":
>> 
>>        date(date[, fmt])
>>                Format a date. See "hg help dates" for formatting strings.
>> 
>> But that does not contain a list of formating strings as output, just various
>> formats for input.
> 
> Right you are! This actually takes the standard % codes from
> strftime(3) / date(1), but that won't help you because the timestamp
> being formatted is in the wrong offset to start with.
> 
>> What is the format string to report UTC time? I do not have control over how
>> hg(1) is invoked. All I can do is to pass options to --template.
> 
> Sadly, I don't think you can do it, then.
> 
> Mercurial dates are indeed stored in (UTC, offset) pairs, but the
> templater always combines in the offset for display. There's a helper
> function localdate which changes the offset to your current timezone, so
> you can do {date|localdate|date}. If you then invoke hg with TZ=UTC,
> you'll get a "local" time that's UTC. There is not at present a utcdate
> helper.

The keyword extension provides an utcdate filter:

$ hg --config extensions.keyword= log -l1 --template '{date|utcdate}\n'
2015/08/16 18:24:30

The code looks like this:
# date like in cvs' $Date
def utcdate(text):
    ''':utcdate: Date. Returns a UTC-date in this format: "2009/08/18 11:00:13".
    '''
    return util.datestr((util.parsedate(text)[0], 0), '%Y/%m/%d %H:%M:%S')


Maybe that helps.

-- 
theatre - books - texts - movies
Black Trash Productions at home: http://www.blacktrash.org
Black Trash Productions on Facebook:
http://www.facebook.com/blacktrashproductions



More information about the Mercurial mailing list