#!/usr/bin/env python2 from sys import argv # Unmodified `gittz(tz)` from git-remote-hg def gittz(tz): """ Converts UTC-offset (in negated seconds) to "+HHmm" notation. Examples: >>> gittz(-1*3600) # Paris '+0100' >>> gittz(5*3600) # New York '-0500' >>> gittz(int(3.5*3600)) # St. John\'s (fails!) '-0330' >>> gittz(int(-3.5*3600)) # Tehran '+0330' """ return '%+03d%02d' % (-tz / 3600, -tz % 3600 / 60) # fixed `gittz(tz)` def gittz_fixed(tz): """ Converts UTC-offset (in negated seconds) to "+HHmm" notation. Examples: >>> gittz_fixed(-1*3600) # Paris '+0100' >>> gittz_fixed(5*3600) # New York '-0500' >>> gittz_fixed(int(3.5*3600)) # St. John\'s '-0330' >>> gittz_fixed(int(-3.5*3600)) # Tehran '+0330' """ sign = 1 if tz < 0 else -1 return '%+03d%02d' % (sign * (abs(tz) / 3600), abs(tz) % 3600 / 60) # Unmodified from `get_author(self)` in git-remote-hg""" def pytz(tz): """ Converts timezone in "+HHmm" notation to UTC-offset (in negated seconds). Examples: >>> pytz('+0100') # Paris -3600 >>> pytz('-0500') # New York 18000 >>> pytz('-0330') # St. John\'s (fails!) 12600 >>> pytz('+0330') # Tehran -12600 """ tz = int(tz) tz = ((tz / 100) * 3600) + ((tz % 100) * 60) return -tz # fixed `pytz(tz)` def pytz_fixed(tz): """ Converts timezone in "+HHmm" notation to UTC-offset (in negated seconds). Examples: >>> pytz_fixed('+0100') # Paris -3600 >>> pytz_fixed('-0500') # New York 18000 >>> pytz_fixed('-0330') # St. John\'s (fails!) 12600 >>> pytz_fixed('+0330') # Tehran -12600 """ tz = int(tz) sign = 1 if tz < 0 else -1 tz = (( abs(tz) / 100 ) * 3600) + (( abs(tz) % 100) * 60) return sign * tz def main(): zones = [ ('Name' , 'ISO TZ', 'int tz' ), ('Tehran' , '+0330', int(-3.5*3600), ), ('Berlin' , '+0100', -1*3600, ), ('UTC' , '+0000', 0, ), ('St. John\'s', '-0330', int(3.5*3600), ), ('Halifax' , '-0400', 4*3600, ), ('Toronto' , '-0500', 5*3600, ), ('Calgary' , '-0700', 7*3600, ), ('Vancouver' , '-0800', 8*3600, ), ] table_fmt = "{:12s} | {:^7} {:^7} | {:^9}{:1} {:^9}{:1} | {:^9}{:1}" print "\nImplementation in git-remote-hg (fails for St. John's)" print "------------------------------------------------------" for zone in zones: zname, isoTZ, intTZ = zone if zname == 'Name': print table_fmt.format( zname, isoTZ, intTZ, "gittz(tz)", "", "pytz(TZ)", "", "iso->int->iso", "" ) else: flag1 = " " if gittz(intTZ) == isoTZ else "*" flag2 = " " if pytz(isoTZ) == intTZ else "*" flag3 = " " if gittz(pytz(isoTZ)) == isoTZ else "*" print table_fmt.format( zname, isoTZ, intTZ, gittz(intTZ), flag1, pytz(isoTZ), flag2, gittz(pytz(isoTZ)), flag3) print "\nCorrect implementation" print "----------------------" for zone in zones: zname, isoTZ, intTZ = zone if zname == 'Name': print table_fmt.format( zname, isoTZ, intTZ, "gittz(tz)", "", "pytz(TZ)", "", "iso->int->iso", "" ) else: print table_fmt.format( zname, isoTZ, intTZ, gittz_fixed(intTZ), flag1, pytz_fixed(isoTZ), flag2, gittz_fixed(pytz_fixed(isoTZ)), flag3) if __name__ == "__main__": if 'test' in argv: import doctest doctest.testmod() else: main()