timezone_test_gen.py :  » Web-Services » python-xmltv » pytvgrab-lib-0.5.1 » lib » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Services » python xmltv 
python xmltv » pytvgrab lib 0.5.1 » lib » timezone_test_gen.py
#!/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='<tz>' python timezone_test_gen.py <testcase number> <tz>
#
# where <tz> is the timezone you want to generate tests for: Australia/Sydney
# and <testcase number> 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)
  
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.