Patchwork D2054: util: extract strdate

login
register
mail settings
Submitter phabricator
Date Feb. 5, 2018, 4:24 p.m.
Message ID <differential-rev-PHID-DREV-2jiiv753ltjepglybzfq-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/27326/
State New
Headers show

Comments

phabricator - Feb. 5, 2018, 4:24 p.m.
lothiraldan created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Extract strdate from util.py to utils/dateutil.py

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2054

AFFECTED FILES
  hgext/convert/darcs.py
  hgext/convert/monotone.py
  mercurial/util.py
  mercurial/utils/dateutil.py

CHANGE DETAILS




To: lothiraldan, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/utils/dateutil.py b/mercurial/utils/dateutil.py
--- a/mercurial/utils/dateutil.py
+++ b/mercurial/utils/dateutil.py
@@ -7,13 +7,15 @@ 
 
 from __future__ import absolute_import, print_function
 
+import calendar
 import datetime
 import time
 
 from ..i18n import _
 from .. import (
     encoding,
     error,
+    pycompat,
 )
 
 # used by parsedate
@@ -139,3 +141,36 @@ 
 
     return None, s
 
+def strdate(string, format, defaults=None):
+    """parse a localized time string and return a (unixtime, offset) tuple.
+    if the string cannot be parsed, ValueError is raised."""
+    if defaults is None:
+        defaults = {}
+
+    # NOTE: unixtime = localunixtime + offset
+    offset, date = parsetimezone(string)
+
+    # add missing elements from defaults
+    usenow = False # default to using biased defaults
+    for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity
+        part = pycompat.bytestr(part)
+        found = [True for p in part if ("%"+p) in format]
+        if not found:
+            date += "@" + defaults[part][usenow]
+            format += "@%" + part[0]
+        else:
+            # We've found a specific time element, less specific time
+            # elements are relative to today
+            usenow = True
+
+    timetuple = time.strptime(encoding.strfromlocal(date),
+                              encoding.strfromlocal(format))
+    localunixtime = int(calendar.timegm(timetuple))
+    if offset is None:
+        # local timezone
+        unixtime = int(time.mktime(timetuple))
+        offset = unixtime - localunixtime
+    else:
+        unixtime = localunixtime + offset
+    return unixtime, offset
+
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -17,7 +17,6 @@ 
 
 import abc
 import bz2
-import calendar
 import codecs
 import collections
 import contextlib
@@ -1906,39 +1905,6 @@ 
             limit -= len(s)
         yield s
 
-def strdate(string, format, defaults=None):
-    """parse a localized time string and return a (unixtime, offset) tuple.
-    if the string cannot be parsed, ValueError is raised."""
-    if defaults is None:
-        defaults = {}
-
-    # NOTE: unixtime = localunixtime + offset
-    offset, date = dateutil.parsetimezone(string)
-
-    # add missing elements from defaults
-    usenow = False # default to using biased defaults
-    for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity
-        part = pycompat.bytestr(part)
-        found = [True for p in part if ("%"+p) in format]
-        if not found:
-            date += "@" + defaults[part][usenow]
-            format += "@%" + part[0]
-        else:
-            # We've found a specific time element, less specific time
-            # elements are relative to today
-            usenow = True
-
-    timetuple = time.strptime(encoding.strfromlocal(date),
-                              encoding.strfromlocal(format))
-    localunixtime = int(calendar.timegm(timetuple))
-    if offset is None:
-        # local timezone
-        unixtime = int(time.mktime(timetuple))
-        offset = unixtime - localunixtime
-    else:
-        unixtime = localunixtime + offset
-    return unixtime, offset
-
 def parsedate(date, formats=None, bias=None):
     """parse a localized date/time and return a (unixtime, offset) tuple.
 
@@ -2001,7 +1967,7 @@ 
 
         for format in formats:
             try:
-                when, offset = strdate(date, format, defaults)
+                when, offset = dateutil.strdate(date, format, defaults)
             except (ValueError, OverflowError):
                 pass
             else:
@@ -3855,3 +3821,9 @@ 
     nouideprecwarn(msg, "4.6")
     return dateutil.parsetimezone(*args, **kwargs)
 
+def strdate(*args, **kwargs):
+    msg = ("'util.strdate' is deprecated, "
+           "use 'utils.dateutil.strdate'")
+    nouideprecwarn(msg, "4.6")
+    return dateutil.strdate(*args, **kwargs)
+
diff --git a/hgext/convert/monotone.py b/hgext/convert/monotone.py
--- a/hgext/convert/monotone.py
+++ b/hgext/convert/monotone.py
@@ -13,7 +13,6 @@ 
 from mercurial.i18n import _
 from mercurial import (
     error,
-    util,
 )
 from mercurial.utils import dateutil
 
@@ -312,7 +311,7 @@ 
         dateformat = "%Y-%m-%dT%H:%M:%S"
         return common.commit(
             author=certs["author"],
-            date=dateutil.datestr(util.strdate(certs["date"], dateformat)),
+            date=dateutil.datestr(dateutil.strdate(certs["date"], dateformat)),
             desc=certs["changelog"],
             rev=rev,
             parents=self.mtnrun("parents", rev).splitlines(),
diff --git a/hgext/convert/darcs.py b/hgext/convert/darcs.py
--- a/hgext/convert/darcs.py
+++ b/hgext/convert/darcs.py
@@ -149,7 +149,8 @@ 
 
     def getcommit(self, rev):
         elt = self.changes[rev]
-        date = util.strdate(elt.get('local_date'), '%a %b %d %H:%M:%S %Z %Y')
+        dateformat = '%a %b %d %H:%M:%S %Z %Y'
+        date = dateutil.strdate(elt.get('local_date'), dateformat)
         desc = elt.findtext('name') + '\n' + elt.findtext('comment', '')
         # etree can return unicode objects for name, comment, and author,
         # so recode() is used to ensure str objects are emitted.