[PATCH 5 of 5] templater: add optional timezone argument to localdate()
Yuya Nishihara
yuya at tcha.org
Tue Sep 1 08:25:15 CDT 2015
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1439903746 -32400
# Tue Aug 18 22:15:46 2015 +0900
# Node ID f32f27b50cda90b1973ea04a7719928dd90f0364
# Parent 7c5bb6ce0f2bb12648b73d5a09579c9890a4dbea
templater: add optional timezone argument to localdate()
The keyword extension uses "utcdate" for a different function, so we can't
add new "utcdate" filter or function. Instead, this patch extends "localdate"
to a general timezone converter.
diff --git a/mercurial/help/templates.txt b/mercurial/help/templates.txt
--- a/mercurial/help/templates.txt
+++ b/mercurial/help/templates.txt
@@ -69,6 +69,10 @@ Some sample command line templates:
$ hg log -r 0 --template "{date(date, '%Y')}\n"
+- Display date in UTC::
+
+ $ hg log -r 0 --template "{localdate(date, 'UTC')|date}\n"
+
- Output the description set to a fill-width of 30::
$ hg log -r 0 --template "{fill(desc, 30)}"
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -517,10 +517,11 @@ def label(context, mapping, args):
yield args[1][0](context, mapping, args[1][1])
def localdate(context, mapping, args):
- """:localdate(date): Converts a date to local date."""
- if len(args) != 1:
+ """:localdate(date[, tz]): Converts a date to the specified timezone.
+ The default is local date."""
+ if not (1 <= len(args) <= 2):
# i18n: "localdate" is a keyword
- raise error.ParseError(_("localdate expects one argument"))
+ raise error.ParseError(_("localdate expects one or two arguments"))
date = evalfuncarg(context, mapping, args[0])
try:
@@ -528,7 +529,19 @@ def localdate(context, mapping, args):
except AttributeError: # not str nor date tuple
# i18n: "localdate" is a keyword
raise error.ParseError(_("localdate expects a date information"))
- tzoffset = util.makedate()[1]
+ if len(args) >= 2:
+ tzoffset = None
+ tz = evalfuncarg(context, mapping, args[1])
+ if isinstance(tz, str):
+ tzoffset = util.parsetimezone(tz)
+ if tzoffset is None:
+ try:
+ tzoffset = int(tz)
+ except (TypeError, ValueError):
+ # i18n: "localdate" is a keyword
+ raise error.ParseError(_("localdate expects a timezone"))
+ else:
+ tzoffset = util.makedate()[1]
return (date[0], tzoffset)
def revset(context, mapping, args):
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -3109,6 +3109,25 @@ Test get function:
hg: parse error: get() expects a dict as first argument
[255]
+Test localdate(date, tz) function:
+
+ $ TZ=JST-09 hg log -r0 -T '{date|localdate|isodate}\n'
+ 1970-01-01 09:00 +0900
+ $ TZ=JST-09 hg log -r0 -T '{localdate(date, "UTC")|isodate}\n'
+ 1970-01-01 00:00 +0000
+ $ TZ=JST-09 hg log -r0 -T '{localdate(date, "+0200")|isodate}\n'
+ 1970-01-01 02:00 +0200
+ $ TZ=JST-09 hg log -r0 -T '{localdate(date, "0")|isodate}\n'
+ 1970-01-01 00:00 +0000
+ $ TZ=JST-09 hg log -r0 -T '{localdate(date, 0)|isodate}\n'
+ 1970-01-01 00:00 +0000
+ $ hg log -r0 -T '{localdate(date, "invalid")|isodate}\n'
+ hg: parse error: localdate expects a timezone
+ [255]
+ $ hg log -r0 -T '{localdate(date, date)|isodate}\n'
+ hg: parse error: localdate expects a timezone
+ [255]
+
Test shortest(node) function:
$ echo b > b
More information about the Mercurial-devel
mailing list