[PATCH 1 of 1] convert: add config option to use the local time zone

Julian Cowley julian at lava.net
Tue Nov 13 03:54:10 CST 2012


# HG changeset patch
# User Julian Cowley <julian at lava.net>
# Date 1352799916 36000
# Node ID e9eeb95e46948aa2efe8d759287e1c0561f3da16
# Parent  fb14a5dcdc62987512820531fe60719d650491b6
convert: add config option to use the local time zone

The default for the time zone offset in a converted changeset has
always been 0 (UTC).  With this patch, the converted changeset is
modified so that the local offset from UTC is specified as the time
zone offset.

The option is specified as the boolean convert.localtime (default
False).  Example usage:

    hg convert -s cvs --config convert.localtime=True example-cvs example-hg

IMPORTANT: the patch only applies to conversions from cvs or svn.
The documentation for the option only appears in those two sections
in the convert help text.

To facilitate the patch, util.makedate() is modified to give it an
optional argument to ct: when creating a time stamp, use the specified
time instead of the current time.  The function then returns the local
time zone offset which was in effect at that time.  This modification
is compatible with current code since all calls to util.makedate()
have no argument.

diff --git a/hgext/convert/__init__.py b/hgext/convert/__init__.py
--- a/hgext/convert/__init__.py
+++ b/hgext/convert/__init__.py
@@ -191,6 +191,10 @@
         branch indicated in the regex as the second parent of the
         changeset. Default is ``{{mergefrombranch ([-\\w]+)}}``
 
+    :convert.localtime: use local time (as determined by the TZ
+        environment variable) for changeset date/times. The default
+        is False (use UTC).
+
     :hook.cvslog: Specify a Python function to be called at the end of
         gathering the CVS log. The function is passed a list with the
         log entries, and can modify the entries in-place, or add or
@@ -231,6 +235,10 @@
     :convert.svn.trunk: specify the name of the trunk branch. The
         default is ``trunk``.
 
+    :convert.localtime: use local time (as determined by the TZ
+        environment variable) for changeset date/times. The default
+        is False (use UTC).
+
     Source history can be retrieved starting at a specific revision,
     instead of being integrally converted. Only single branch
     conversions are supported.
diff --git a/hgext/convert/cvs.py b/hgext/convert/cvs.py
--- a/hgext/convert/cvs.py
+++ b/hgext/convert/cvs.py
@@ -70,6 +70,8 @@
                 cs.author = self.recode(cs.author)
                 self.lastbranch[cs.branch] = id
                 cs.comment = self.recode(cs.comment)
+                if self.ui.configbool('convert', 'localtime', False):
+                    cs.date = util.makedate(cs.date[0])
                 date = util.datestr(cs.date, '%Y-%m-%d %H:%M:%S %1%2')
                 self.tags.update(dict.fromkeys(cs.tags, id))
 
diff --git a/hgext/convert/hg.py b/hgext/convert/hg.py
--- a/hgext/convert/hg.py
+++ b/hgext/convert/hg.py
@@ -203,7 +203,11 @@
             return context.memfilectx(f, data, False, False, None)
 
         self.ui.status(_("updating tags\n"))
-        date = "%s 0" % int(time.mktime(time.gmtime()))
+        if self.ui.configbool('convert', 'localtime', False):
+            date = util.makedate()
+        else:
+            date = (util.makedate()[0], 0)
+        date = "%d %d" % date
         extra = {'branch': self.tagsbranch}
         ctx = context.memctx(self.repo, (tagparent, None), "update tags",
                              [".hgtags"], getfilectx, "convert-repo", date,
diff --git a/hgext/convert/subversion.py b/hgext/convert/subversion.py
--- a/hgext/convert/subversion.py
+++ b/hgext/convert/subversion.py
@@ -802,6 +802,8 @@
             # ISO-8601 conformant
             # '2007-01-04T17:35:00.902377Z'
             date = util.parsedate(date[:19] + " UTC", ["%Y-%m-%dT%H:%M:%S"])
+            if self.ui.configbool('convert', 'localtime', False):
+                date = util.makedate(date[0])
 
             log = message and self.recode(message) or ''
             author = author and self.recode(author) or ''
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -944,15 +944,14 @@
             limit -= len(s)
         yield s
 
-def makedate():
-    ct = time.time()
+def makedate(ct=time.time()):
     if ct < 0:
         hint = _("check your clock")
         raise Abort(_("negative timestamp: %d") % ct, hint=hint)
     delta = (datetime.datetime.utcfromtimestamp(ct) -
              datetime.datetime.fromtimestamp(ct))
     tz = delta.days * 86400 + delta.seconds
-    return ct, tz
+    return int(ct), tz
 
 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
     """represent a (unixtime, offset) tuple as a localized time.
diff --git a/tests/test-convert-cvs.t b/tests/test-convert-cvs.t
--- a/tests/test-convert-cvs.t
+++ b/tests/test-convert-cvs.t
@@ -320,7 +320,91 @@
   |/
   o  0 () Initial revision files: a b/c
   
+Check localtime option.
 
+NOTE: This doesn't check all time zones -- it merely determines that
+the configuration option is taking effect.
+
+An arbitrary (U.S.) time zone is used here.  TZ=US/Hawaii is selected
+since it does not use DST (unlike other U.S. time zones) and is always
+a fixed difference from UTC.
+
+  $ TZ=US/Hawaii hg convert --config convert.localtime=True src localtime
+  initializing destination localtime repository
+  connecting to $TESTTMP/cvsrepo
+  scanning source...
+  collecting CVS rlog
+  11 log entries
+  cvslog hook: 11 entries
+  creating changesets
+  7 changeset entries
+  cvschangesets hook: 7 changesets
+  sorting...
+  converting...
+  6 Initial revision
+  5 import
+  4 ci0
+  3 ci1
+  2 ci2
+  1 funny
+  0 fuzzy
+  updating tags
+  $ hg glog -R localtime --template '{rev} {desc|firstline} {date|date}\n'
+  o  7 update tags * -1000 (glob)
+  |
+  | o  6 fuzzy * -1000 (glob)
+  | |
+  | o  5 funny * -1000 (glob)
+  | |
+  | o  4 ci2 * -1000 (glob)
+  |
+  o  3 ci1 * -1000 (glob)
+  |
+  o  2 ci0 * -1000 (glob)
+  |
+  | o  1 import * -1000 (glob)
+  |/
+  o  0 Initial revision * -1000 (glob)
+  
+Now test without option.
+
+  $ TZ=US/Hawaii hg convert --config convert.localtime=False src nolocaltime
+  initializing destination nolocaltime repository
+  connecting to $TESTTMP/cvsrepo
+  scanning source...
+  collecting CVS rlog
+  11 log entries
+  cvslog hook: 11 entries
+  creating changesets
+  7 changeset entries
+  cvschangesets hook: 7 changesets
+  sorting...
+  converting...
+  6 Initial revision
+  5 import
+  4 ci0
+  3 ci1
+  2 ci2
+  1 funny
+  0 fuzzy
+  updating tags
+  $ hg glog -R nolocaltime --template '{rev} {desc|firstline} {date|date}\n'
+  o  7 update tags * +0000 (glob)
+  |
+  | o  6 fuzzy * +0000 (glob)
+  | |
+  | o  5 funny * +0000 (glob)
+  | |
+  | o  4 ci2 * +0000 (glob)
+  |
+  o  3 ci1 * +0000 (glob)
+  |
+  o  2 ci0 * +0000 (glob)
+  |
+  | o  1 import * +0000 (glob)
+  |/
+  o  0 Initial revision * +0000 (glob)
+  
 testing debugcvsps
 
   $ cd src
diff --git a/tests/test-convert-svn-source.t b/tests/test-convert-svn-source.t
--- a/tests/test-convert-svn-source.t
+++ b/tests/test-convert-svn-source.t
@@ -161,7 +161,65 @@
   |
   o  0 second letter files: letter2.txt
   
+Check localtime option.
 
+NOTE: This doesn't check all time zones -- it merely determines that
+the configuration option is taking effect.
+
+An arbitrary (U.S.) time zone is used here.  TZ=US/Hawaii is selected
+since it does not use DST (unlike other U.S. time zones) and is always
+a fixed difference from UTC.
+
+  $ TZ=US/Hawaii hg convert --config convert.localtime=True "$SVNREPOURL/proj%20B/mytrunk" localtime
+  initializing destination localtime repository
+  scanning source...
+  sorting...
+  converting...
+  5 init projB
+  4 hello
+  3 world
+  2 nice day
+  1 second letter
+  0 work in progress
+  $ hg glog -R localtime --template '{rev} {desc|firstline} {date|date}\n'
+  o  5 work in progress * -1000 (glob)
+  |
+  o  4 second letter * -1000 (glob)
+  |
+  o  3 nice day * -1000 (glob)
+  |
+  o  2 world * -1000 (glob)
+  |
+  o  1 hello * -1000 (glob)
+  |
+  o  0 init projB * -1000 (glob)
+  
+Now test without option.
+
+  $ TZ=US/Hawaii hg convert --config convert.localtime=False "$SVNREPOURL/proj%20B/mytrunk" nolocaltime
+  initializing destination nolocaltime repository
+  scanning source...
+  sorting...
+  converting...
+  5 init projB
+  4 hello
+  3 world
+  2 nice day
+  1 second letter
+  0 work in progress
+  $ hg glog -R nolocaltime --template '{rev} {desc|firstline} {date|date}\n'
+  o  5 work in progress * +0000 (glob)
+  |
+  o  4 second letter * +0000 (glob)
+  |
+  o  3 nice day * +0000 (glob)
+  |
+  o  2 world * +0000 (glob)
+  |
+  o  1 hello * +0000 (glob)
+  |
+  o  0 init projB * +0000 (glob)
+  
 Test stop revision
   $ hg convert --rev 1 "$SVNREPOURL/proj%20B/mytrunk" stoprev
   initializing destination stoprev repository
diff --git a/tests/test-convert.t b/tests/test-convert.t
--- a/tests/test-convert.t
+++ b/tests/test-convert.t
@@ -172,6 +172,10 @@
                     will add the most recent revision on the branch indicated in
                     the regex as the second parent of the changeset. Default is
                     "{{mergefrombranch ([-\w]+)}}"
+      convert.localtime
+                    use local time (as determined by the TZ environment
+                    variable) for changeset date/times. The default is False
+                    (use UTC).
       hook.cvslog   Specify a Python function to be called at the end of
                     gathering the CVS log. The function is passed a list with
                     the log entries, and can modify the entries in-place, or add
@@ -211,6 +215,10 @@
       convert.svn.trunk
                     specify the name of the trunk branch. The default is
                     "trunk".
+      convert.localtime
+                    use local time (as determined by the TZ environment
+                    variable) for changeset date/times. The default is False
+                    (use UTC).
   
       Source history can be retrieved starting at a specific revision, instead
       of being integrally converted. Only single branch conversions are


More information about the Mercurial-devel mailing list