def test_tz_database(self): tz_db = local_time.time_zone_database() tz_db.load_from_file( os.path.join( os.path.split( date_time.__file__ )[0] , 'date_time_zonespec.csv' ) ) self.failUnless( not tz_db.time_zone_from_region("Invalid/name") ) nyc_test = tz_db.time_zone_from_region("America/New_York") self.failUnless( nyc_test ) self.failUnless( nyc_test.std_zone_abbrev() == "EST" ) self.failUnless( nyc_test.std_zone_name() == "Eastern Standard Time" ) self.failUnless( nyc_test.dst_zone_abbrev() == "EDT" ) self.failUnless( nyc_test.dst_zone_name() == "Eastern Daylight Time" ) self.failUnless( nyc_test.base_utc_offset() == posix_time.hours(-5) ) self.failUnless( nyc_test.dst_offset() == posix_time.hours(1) ) self.failUnless( nyc_test.dst_local_start_time(2004) == posix_time.ptime( gregorian.date(2004, date_time.Apr, 4) , posix_time.hours(2) ) ) self.failUnless( nyc_test.dst_local_end_time(2004) == posix_time.ptime( gregorian.date(2004, date_time.Oct, 31) , posix_time.hours(2) ) ) self.failUnless( nyc_test.has_dst() ) phx_test = tz_db.time_zone_from_region("America/Phoenix") self.failUnless( phx_test ) self.failUnless( phx_test.std_zone_abbrev() == "MST" ) self.failUnless( phx_test.std_zone_name() == "Mountain Standard Time" ) self.failUnless( phx_test.dst_zone_abbrev() == "" ) self.failUnless( phx_test.dst_zone_name() == "" ) self.failUnless( phx_test.base_utc_offset() == posix_time.hours(-7) ) self.failUnless( phx_test.dst_offset() == posix_time.hours(0) ) self.failUnless( phx_test.has_dst() == False) eastern = local_time.posix_time_zone("EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00") tz_db.add_record("United States/Eastern", eastern) eastern_test = tz_db.time_zone_from_region("United States/Eastern" ) self.failUnless( eastern_test ) self.failUnless( eastern_test.std_zone_abbrev() == "EST" ) self.failUnless( eastern_test.std_zone_name() == "EST" ) self.failUnless( eastern_test.dst_zone_abbrev() == "EDT" ) self.failUnless( eastern_test.dst_zone_name() == "EDT" ) self.failUnless( eastern_test.base_utc_offset() == posix_time.hours(-5) ) self.failUnless( eastern_test.dst_local_start_time(2004) == posix_time.ptime( gregorian.date(2004, date_time.Apr, 4) , posix_time.hours(2) ) ) self.failUnless( eastern_test.dst_local_end_time(2004) == posix_time.ptime( gregorian.date(2004, date_time.Oct, 31) , posix_time.hours(2) ) ) self.failUnless( eastern_test.has_dst() )
t2 = t1 - posix_time.minutes(4) + posix_time.seconds(2) print 't2: ', to_simple_string(t2) now = posix_time.second_clock.local_time() #use the clock print 'now: ', to_simple_string(now) today = now.date() #Get the date part out of the time print 'today: ', to_simple_string(today) tomorrow = today + gregorian.date_duration(1) print 'tomorrow: ', to_simple_string(tomorrow) #Local time programming: #setup some timezones for creating and adjusting times #first time zone uses the time zone file for regional timezone definitions tz_db = local_time.time_zone_database() tz_db.load_from_file( os.path.join(date_time.__path__[0], "date_time_zonespec.csv")) nyc_tz = tz_db.time_zone_from_region("America/New_York") #This timezone uses a posix time zone string definition to create a time zone phx_tz = local_time.posix_time_zone("MST-07:00:00") #local departure time in phoenix is 11 pm on April 2 2005 #Note that New York changes to daylight savings on Apr 3 at 2 am) phx_departure = local_time.local_date_time( gregorian.date(2005, date_time.Apr, 2), posix_time.hours(23), phx_tz, local_time.local_date_time.NOT_DATE_TIME_ON_ERROR) flight_length = posix_time.hours(4) + posix_time.minutes(30) phx_arrival = phx_departure + flight_length #//convert the phx time to a nyz time
t2 = t1 - posix_time.minutes(4) + posix_time.seconds(2) print "t2: ", to_simple_string(t2) now = posix_time.second_clock.local_time() # use the clock print "now: ", to_simple_string(now) today = now.date() # Get the date part out of the time print "today: ", to_simple_string(today) tomorrow = today + gregorian.date_duration(1) print "tomorrow: ", to_simple_string(tomorrow) # Local time programming: # setup some timezones for creating and adjusting times # first time zone uses the time zone file for regional timezone definitions tz_db = local_time.time_zone_database() tz_db.load_from_file(os.path.join(date_time.__path__[0], "date_time_zonespec.csv")) nyc_tz = tz_db.time_zone_from_region("America/New_York") # This timezone uses a posix time zone string definition to create a time zone phx_tz = local_time.posix_time_zone("MST-07:00:00") # local departure time in phoenix is 11 pm on April 2 2005 # Note that New York changes to daylight savings on Apr 3 at 2 am) phx_departure = local_time.local_date_time( gregorian.date(2005, date_time.Apr, 2), posix_time.hours(23), phx_tz, local_time.local_date_time.NOT_DATE_TIME_ON_ERROR, ) flight_length = posix_time.hours(4) + posix_time.minutes(30)