[PATCH 5 of 5] util.datestr: use divmod()

Gregory Szorc gregory.szorc at gmail.com
Sat Nov 21 22:14:30 CST 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1447551010 28800
#      Sat Nov 14 17:30:10 2015 -0800
# Node ID f73b8f9ebe371ed9f806f1e97561c5b93942ca90
# Parent  7f3a388e86fc1a4ecfb5052ebf8496e78bb486e4
util.datestr: use divmod()

We were computing the quotient and remainder of a division operation
separately. The built-in divmod() function allows us to do this with
a single function call. Do that.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1362,19 +1362,20 @@ def datestr(date=None, format='%a %b %d 
     append time zone to string."""
     t, tz = date or makedate()
     if t < 0:
         t = 0   # time.gmtime(lt) fails on Windows for lt < -43200
         tz = 0
     if "%1" in format or "%2" in format or "%z" in format:
         sign = (tz > 0) and "-" or "+"
         minutes = abs(tz) // 60
+        q, r = divmod(minutes, 60)
         format = format.replace("%z", "%1%2")
-        format = format.replace("%1", "%c%02d" % (sign, minutes // 60))
-        format = format.replace("%2", "%02d" % (minutes % 60))
+        format = format.replace("%1", "%c%02d" % (sign, q))
+        format = format.replace("%2", "%02d" % r)
     try:
         t = time.gmtime(float(t) - tz)
     except ValueError:
         # time was out of range
         t = time.gmtime(sys.maxint)
     s = time.strftime(format, t)
     return s
 


More information about the Mercurial-devel mailing list