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

login
register
mail settings
Submitter Gregory Szorc
Date Nov. 22, 2015, 4:14 a.m.
Message ID <f73b8f9ebe371ed9f806.1448165670@ubuntu-main>
Download mbox | patch
Permalink /patch/11575/
State Accepted
Headers show

Comments

Gregory Szorc - Nov. 22, 2015, 4:14 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@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.
Pierre-Yves David - Nov. 23, 2015, 3:15 a.m.
On 11/21/2015 08:14 PM, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1447551010 28800
> #      Sat Nov 14 17:30:10 2015 -0800
> # Node ID f73b8f9ebe371ed9f806f1e97561c5b93942ca90
> # Parent  7f3a388e86fc1a4ecfb5052ebf8496e78bb486e4
> util.datestr: use divmod()

Pushed to the clowncopter.

Patch

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