#!/usr/bin/env python # ----------------------------------------------------------------------- # Copyright (C) 2004 Dan Fritz. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MER- # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # ----------------------------------------------------------------------- # # This is a testcase generator for the timezone.py file. # It finds daylight saving transition times, and outputs them as an unittest case # # Usage: # TZ='' python timezone_test_gen.py # # where is the timezone you want to generate tests for: Australia/Sydney # and is the number you want your testcase to start at. # # ----------------------------------------------------------------------- # Subversion Information, do not edit # # $Rev: $ # $LastChangedDate: $ # $LastChangedRevision: $ # $LastChangedBy: $ # # $Log: $ # from datetime import datetime, timedelta import time import sys from tvgrab.datetime2 import DateTime, Date, Time, now from copy import copy import re def toUtcOffset(sec): # altzone has DST included offset = time.altzone + 3600 if sec.t[8]: offset -= 3600 az = abs(offset) # altzone rv = str(az/3600) + ":" az = az % 3600 rv += str(az/60).zfill(2) + ":" az = az % 60 rv += str(az).zfill(2) if offset > 0: # Lol, this is funny. altzone is positive if utcoffset is negative. # A bit US centric, are we? rv = "-" + rv return rv def findTransition(from_dt, to_dt): if from_dt.t[8] == to_dt.t[8]: return None diff = to_dt - from_dt index_dt = copy(from_dt).addSeconds(diff/2.0) while diff > 1 : if index_dt.t[8] == from_dt.t[8]: from_dt = index_dt else: to_dt = index_dt diff = to_dt - from_dt index_dt = copy(from_dt).addSeconds(diff/2.0) if from_dt.t[8] != to_dt.t[8]: return from_dt else: return None if __name__ == '__main__': print "utcOffset = " + toUtcOffset(now()) from_dt = DateTime(Date("2000-01-01"), Time("01:00:00")) to_dt = DateTime(Date("2025-01-01"), Time("01:00:00")) remove_silly_octal_mode = re.compile(",0(\d)") testNumber = int(sys.argv[1]) tzName = sys.argv[2] while from_dt < to_dt: tran_dt = findTransition(copy(from_dt), copy(from_dt).addHours(24*7)) if tran_dt: #generate the testcase tc = " def test" + str(testNumber).zfill(3) + "(self): self.vt(" tran_dt.output_fmt = "d(%Y,%m,%d,%H,%M,%S)" tc += str(tran_dt) tc += ",'" + tzName + "',('" tc += toUtcOffset(tran_dt) + "','" + toUtcOffset(tran_dt.addSeconds(1)) tc += "'))" tc = remove_silly_octal_mode.sub(", \g<1>", tc) print tc testNumber += 1 from_dt.addHours(24*7)