Ejemplo n.º 1
0
    def test_local_time_period( self ):
        d1 = gregorian.date(2001, date_time.Jan, 1)        
        az_tz = local_time.posix_time_zone("MST-07")
        
        t1 = local_time.local_date_time( d1, posix_time.hours(2), az_tz, False)
        t2  = local_time.local_date_time( d1, posix_time.hours(3), az_tz, False)
        p1 = local_time.local_time_period( t1, t2 ) 
        p2 = local_time.local_time_period( t1, t2 )
        self.failUnless( p1 == p2 )

        p4 = local_time.local_time_period( t1, posix_time.hours(1) )
        self.failUnless( p4.length() == posix_time.hours(1) )        
        self.failUnless( p1 == p4 )
        self.failUnless( p1.begin() == t1 )
        self.failUnless( p1.end() == t2 )
        self.failUnless( p1.last() == t2 - posix_time.time_duration.unit() )
        self.failUnless( p1.contains( t1 ) )
        self.failUnless( not p1.contains( t2 ) )
        self.failUnless( p1.contains( t2 - posix_time.seconds(1) ) )
        
        t3 = local_time.local_date_time( gregorian.date(2001, date_time.Jan,1)
                                         , posix_time.hours(4)
                                         , az_tz
                                         , False )
        p5 = local_time.local_time_period( t2, t3 )
        self.failUnless( not p1.contains( p5.begin() ) )
        self.failUnless( not p5.contains( p1.begin() ) )
        self.failUnless( not( p1 == p5 ) )
        self.failUnless( p1 < p5 )
        self.failUnless( p5 > p1 )
        self.failUnless( p5 != p1 )
        self.failUnless( p1.intersects(p1) )
        self.failUnless( not p1.intersects(p5) )
        self.failUnless( not p5.intersects(p1) )
        
        p6 = local_time.local_time_period( t2, t3 )
        p6.shift( posix_time.minutes(30) )
        self.failUnless( p5.intersects(p6) )
        self.failUnless( p6.intersects(p5) )
        self.failUnless( p5.contains( p6.begin() ) )
        p6.shift( posix_time.minutes(30) )
        self.failUnless( not p5.intersects(p6) )
        self.failUnless( not p6.intersects(p5) )
        p6.shift( posix_time.minutes(-30) )
        
        p7 = p5.intersection(p6)
        
        self.failUnless( p7 == local_time.local_time_period(
                                   local_time.local_date_time( d1
                                                               , posix_time.time_duration(3,30,0)
                                                               , az_tz, False)
                                   , local_time.local_date_time( d1
                                                                 , posix_time.time_duration(4,0,0)
                                                                 , az_tz
                                                                 , False) ) )
Ejemplo n.º 2
0
 def test_time_period(self):
     d1 = gregorian.date(2001,date_time.Jan, 1)
     t1 = posix_time.ptime(d1,posix_time.hours(2))
     t2 = posix_time.ptime(d1,posix_time.hours(3))
     p1 = posix_time.time_period(t1,t2) 
     p2 = posix_time.time_period(t1,t2) 
     p4 = posix_time.time_period(t1, posix_time.hours(1))
     self.failUnless( p4.length() == posix_time.hours(1))
     
     self.failUnless( p1 == p4)
     self.failUnless( p1.begin() == t1)
     self.failUnless( p1.end() == t2)
     self.failUnless( p1.last() == t2-posix_time.time_duration.unit())
     
     self.failUnless( p1.contains(t1))
     self.failUnless( not p1.contains(t2))
     self.failUnless( p1.contains(t2-posix_time.seconds(1)))
     t3 = posix_time.ptime(gregorian.date(2001,date_time.Jan,1),posix_time.hours(4))
     p5 = posix_time.time_period(t2,t3)
     self.failUnless( not p1.contains(p5.begin()))
     self.failUnless( not p5.contains(p1.begin()))
     self.failUnless( not (p1 == p5))
     self.failUnless( p1 < p5)
     self.failUnless( p5 > p1)
     self.failUnless( p5 != p1)
     self.failUnless( p1.intersects(p1))
     self.failUnless( not p1.intersects(p5))
     self.failUnless( not p5.intersects(p1))
     
     p6 = posix_time.time_period(t2,t3)
     p6.shift(posix_time.minutes(30))        
     
     self.failUnless( p5.intersects(p6))
     self.failUnless( p6.intersects(p5))
     self.failUnless( p5.contains(p6.begin()))
     p6.shift(posix_time.minutes(30))
     
     
     self.failUnless( not p5.intersects(p6))
     self.failUnless( not p6.intersects(p5))
     p6.shift(posix_time.minutes(-30))
     
     p7 = p5.intersection(p6)
     
     self.failUnless(  
     p7 == posix_time.time_period( posix_time.ptime(d1,posix_time.time_duration(3,30,0))
                                  , posix_time.ptime(d1,posix_time.time_duration(4,0,0))))
Ejemplo n.º 3
0
    def test_time_period(self):
        d1 = gregorian.date(2001, date_time.Jan, 1)
        t1 = posix_time.ptime(d1, posix_time.hours(2))
        t2 = posix_time.ptime(d1, posix_time.hours(3))
        p1 = posix_time.time_period(t1, t2)
        p2 = posix_time.time_period(t1, t2)
        p4 = posix_time.time_period(t1, posix_time.hours(1))
        self.failUnless(p4.length() == posix_time.hours(1))

        self.failUnless(p1 == p4)
        self.failUnless(p1.begin() == t1)
        self.failUnless(p1.end() == t2)
        self.failUnless(p1.last() == t2 - posix_time.time_duration.unit())

        self.failUnless(p1.contains(t1))
        self.failUnless(not p1.contains(t2))
        self.failUnless(p1.contains(t2 - posix_time.seconds(1)))
        t3 = posix_time.ptime(gregorian.date(2001, date_time.Jan, 1),
                              posix_time.hours(4))
        p5 = posix_time.time_period(t2, t3)
        self.failUnless(not p1.contains(p5.begin()))
        self.failUnless(not p5.contains(p1.begin()))
        self.failUnless(not (p1 == p5))
        self.failUnless(p1 < p5)
        self.failUnless(p5 > p1)
        self.failUnless(p5 != p1)
        self.failUnless(p1.intersects(p1))
        self.failUnless(not p1.intersects(p5))
        self.failUnless(not p5.intersects(p1))

        p6 = posix_time.time_period(t2, t3)
        p6.shift(posix_time.minutes(30))

        self.failUnless(p5.intersects(p6))
        self.failUnless(p6.intersects(p5))
        self.failUnless(p5.contains(p6.begin()))
        p6.shift(posix_time.minutes(30))

        self.failUnless(not p5.intersects(p6))
        self.failUnless(not p6.intersects(p5))
        p6.shift(posix_time.minutes(-30))

        p7 = p5.intersection(p6)

        self.failUnless(p7 == posix_time.time_period(
            posix_time.ptime(d1, posix_time.time_duration(3, 30, 0)),
            posix_time.ptime(d1, posix_time.time_duration(4, 0, 0))))
Ejemplo n.º 4
0
#US labor day is first Monday in Sept
first = gregorian.nth_day_of_the_week_in_month.first
labor_day = gregorian.nth_day_of_the_week_in_month(first, date_time.Monday,
                                                   date_time.Sep)
#calculate a specific date for 2004 from functor
print 'labor day 2004: ', to_simple_string(labor_day.get_date(2004))

#Time programming:
d = gregorian.date(2002, date_time.Feb, 1)  #an arbitrary date
t1 = posix_time.ptime(d,
                      posix_time.hours(5) + posix_time.millisec(100))
#date + time of day offset
print 't1: ', to_simple_string(t1)

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(
Ejemplo n.º 5
0
# calculate Sunday following d4
print "d5: ", to_simple_string(d5)

# US labor day is first Monday in Sept
first = gregorian.nth_day_of_the_week_in_month.first
labor_day = gregorian.nth_day_of_the_week_in_month(first, date_time.Monday, date_time.Sep)
# calculate a specific date for 2004 from functor
print "labor day 2004: ", to_simple_string(labor_day.get_date(2004))

# Time programming:
d = gregorian.date(2002, date_time.Feb, 1)  # an arbitrary date
t1 = posix_time.ptime(d, posix_time.hours(5) + posix_time.millisec(100))
# date + time of day offset
print "t1: ", to_simple_string(t1)

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"))
Ejemplo n.º 6
0
    def test_duration( self ):
        td0 = posix_time.time_duration()
        self.failUnless( posix_time.time_duration().ticks() == 0)
        td1 = posix_time.time_duration(1,25,0)
        td3 = posix_time.time_duration(td1.hours(),td1.minutes(),td1.seconds())
        self.failUnless( td1 == td3)
        td1 = -td1 
        td3 = posix_time.time_duration(td1.hours(),td1.minutes(),td1.seconds())
        self.failUnless( td1 == td3)
        t_1 = posix_time.time_duration(0,1,40)
        t_2 = posix_time.time_duration(0,1,41)
        self.failUnless( not ( t_2 < t_2 ) )
        self.failUnless( t_1 == t_1)
        self.failUnless( t_1 >= t_1)
        self.failUnless( t_2 >= t_1)
        self.failUnless( t_2 <= t_2)
        self.failUnless( t_2 > t_1)
        self.failUnless( not (t_1 > t_2))
        t_3 = posix_time.time_duration(0,1,41)
        self.failUnless( t_2 == t_3)
        t_4 = posix_time.time_duration(0,1,41)
        self.failUnless( t_2 == t_4)
        
        t_5 = posix_time.time_duration(1,30,20,10) 
        t_5 /= 2
        self.failUnless( t_5.hours() == 0 
                         and t_5.minutes() == 45 
                         and t_5.seconds() == 10 
                         and  t_5.fractional_seconds() == 5)
        t_5 = posix_time.time_duration(3,15,8,0) / 2        
        self.failUnless( t_5 == posix_time.time_duration(1,37,34,0) )
        td = posix_time.hours( 5 )
        td *= 5
        self.failUnless( td == posix_time.time_duration( 25, 0,0,0 ) )
        t_5 = t_2 + t_1
        self.failUnless( t_5 == posix_time.time_duration(0,3,21) )

        td_a = posix_time.time_duration(5,5,5,5)
        td_b = posix_time.time_duration(4,4,4,4)
        td_c = posix_time.time_duration(2,2,2,2)
        td_a += td_b
        self.failUnless( td_a == posix_time.time_duration(9,9,9,9) )
        td_d = td_b - td_c 
        self.failUnless( td_d == posix_time.time_duration(2,2,2,2) )
        td_d -= td_b 
        self.failUnless( td_d == td_c - td_b)
        
        utd = posix_time.time_duration(1,2,3,4)
        utd2 = -utd
        
        self.failUnless( utd2.hours() == -1
                         and  utd2.minutes() == -2
                         and  utd2.seconds() == -3
                         and  utd2.fractional_seconds() == -4 )
        utd2 = -posix_time.hours(5)
        self.failUnless( utd2.hours() == -5)
        utd2 = -utd2
        self.failUnless( utd2.hours() == 5)

        t_6 = posix_time.time_duration(5,4,3)  
        self.failUnless( t_6.hours() == 5)
        self.failUnless( t_6.minutes() == 4)
        self.failUnless( t_6.seconds() == 3)
        
        self.failUnless( t_6.total_seconds() == 18243)
        
        tenhours = posix_time.hours(10)
        fivemin = posix_time.minutes(5)
        t7 = posix_time.time_duration(1,2,3) + tenhours + fivemin
        self.failUnless( t7 == posix_time.time_duration(11,7,3))
        t8 = tenhours + posix_time.time_duration(1,2,3) +  fivemin
        self.failUnless(t8 == posix_time.time_duration(11,7,3))
        if posix_time.time_duration.resolution() >= date_time.micro:
            t_9 = posix_time.time_duration(5,4,3,9876) 
            self.failUnless(  t_9.hours() == 5)
            self.failUnless(  t_9.minutes() == 4)
            self.failUnless(  t_9.seconds() == 3)
            self.failUnless(  t_9.fractional_seconds() == 9876)
            self.failUnless( t_9.total_seconds() == 18243)
        
        if posix_time.time_duration.resolution() >= date_time.tenth:
            t_10 = posix_time.time_duration(5,4,3,9) 
            self.failUnless(  t_10.hours() == 5)
            self.failUnless(  t_10.minutes() == 4)
            self.failUnless(  t_10.seconds() == 3)
            self.failUnless(  t_10.fractional_seconds() == 9)
            self.failUnless( t_10.total_seconds() == 18243)

        if posix_time.time_duration.resolution() >= date_time.milli:
            ms = posix_time.millisec(9)        
            if posix_time.time_duration.resolution() == date_time.nano:
                self.failUnless(  ms.fractional_seconds() == 9000000)
                self.failUnless( ms.total_seconds() == 0)
                self.failUnless( ms.total_milliseconds() == 9)
                self.failUnless( posix_time.time_duration.ticks_per_second() == 1000000000)
            else:
                self.failUnless(  ms.fractional_seconds() == 9000)
                self.failUnless( ms.total_seconds() == 0)
                self.failUnless( ms.total_milliseconds() == 9)

        if hasattr( posix_time, 'nanoseconds' ):
            if posix_time.time_duration.resolution() >= date_time.nano:
                ns = posix_time.nanosec(9)
                
                self.failUnless(  ns.fractional_seconds() == 9)
                self.failUnless(  ns.total_nanoseconds() == 9)
                self.failUnless(  ns.total_microseconds() == 0)
                
                ns18 = ns + ns
                self.failUnless(  ns18.fractional_seconds() == 18)
                
                ns2 = posix_time.nanosec(1000000000) 
                self.failUnless(  ns2 == seconds(1))
                self.failUnless(  ns2.total_seconds() == 1)
                
                self.failUnless( (nanosec(3)/2) == nanosec(1))
                self.failUnless( nanosec(3)*1000 == microsec(3))
        
        t_11 = posix_time.time_duration(3600,0,0) 
        self.failUnless(  t_11.hours() == 3600)
        self.failUnless(  t_11.total_seconds() == 12960000)
        
        td_12 = posix_time.time_duration(1,2,3,10) 
        
        self.failUnless(  td_12.total_seconds() == 3723)
        
        self.failUnless( posix_time.hours(2)/2 == posix_time.hours(1))
        self.failUnless( posix_time.hours(3)/2 == posix_time.minutes(90))
        
        self.failUnless( posix_time.time_duration(3,0,0)*2 == posix_time.hours(6))
        self.failUnless( posix_time.hours(3600)*1000 == posix_time.hours(3600000))
        
        pi_dur = posix_time.time_duration(date_time.pos_infin)
        ni_dur = posix_time.time_duration(date_time.neg_infin)
        ndt_dur = posix_time.time_duration(date_time.not_a_date_time)
        self.failUnless( pi_dur + ni_dur == ndt_dur)
        self.failUnless( pi_dur / 3 == pi_dur)
        self.failUnless( pi_dur + td_12 == pi_dur)
        self.failUnless( pi_dur - td_12 == pi_dur)
        self.failUnless( -pi_dur == ni_dur)
        self.failUnless( ni_dur < pi_dur)
        self.failUnless( ni_dur < td_12)
        self.failUnless( pi_dur > td_12)
         
        self.failUnless( to_simple_string(pi_dur)=="+infinity")
        self.failUnless( to_simple_string(ni_dur)=="-infinity")
        self.failUnless( to_simple_string(ndt_dur)=="not-a-date-time")
        
        t1 = posix_time.ptime( gregorian.date(2001,7,14))
        t2 = posix_time.ptime( gregorian.date(2002,7,14))
        self.failUnless(  365*24 == (t2-t1).hours())
        self.failUnless(  365*24*3600 == (t2-t1).total_seconds())
                
        self.failUnless( posix_time.seconds(1).total_milliseconds() == 1000)
        self.failUnless( posix_time.seconds(1).total_microseconds() == 1000000)
        self.failUnless( posix_time.seconds(1).total_nanoseconds() == 1000000000)
        self.failUnless( posix_time.hours(1).total_milliseconds() == 3600*1000)
        
        tms = 3600*1000000*1001
        self.failUnless( posix_time.hours(1001).total_microseconds() == tms)
        tms = 3600*365*24*1000
        self.failUnless( (t2-t1).total_milliseconds() == tms)
        tms = 3600*365*24*1000000000
        self.failUnless( (t2-t1).total_nanoseconds() == tms)
        print posix_time.microseconds(25).ticks()/posix_time.time_duration.ticks_per_second()        
Ejemplo n.º 7
0
 def test_posix_time_zone(self):
     specs = [ "MST-07"
               , "MST-07:00:00"
               , "EST-05EDT,M4.1.0,M10.5.0"
               , "EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00"
               , "PST-08PDT,J46/1:30,J310"
               , "PST-08PDT,45,310/0:30:00" ]
     
     nyc1 = local_time.posix_time_zone( specs[2] )
     nyc2 = local_time.posix_time_zone( specs[3] )
     
     td = posix_time.hours(-5)        
     self.failUnless( nyc1.has_dst() and nyc2.has_dst() )
     self.failUnless( nyc1.base_utc_offset() == td )
     self.failUnless( nyc1.base_utc_offset() == nyc2.base_utc_offset() )
     self.failUnless( nyc1.std_zone_abbrev() == "EST" )
     self.failUnless( nyc2.std_zone_abbrev() == "EST" )
     self.failUnless( nyc1.dst_zone_abbrev() == "EDT" )
     self.failUnless( nyc2.dst_zone_abbrev() == "EDT" )
     self.failUnless( nyc1.std_zone_name() == "EST" ) 
     self.failUnless( nyc2.std_zone_name() == "EST" )
     self.failUnless( nyc1.dst_zone_name() == "EDT" )
     self.failUnless( nyc2.dst_zone_name() == "EDT" )
     
     td = posix_time.hours(1)
     self.failUnless( nyc1.dst_offset() == td )
     self.failUnless( nyc1.dst_offset() == nyc2.dst_offset() )
     self.failUnless( nyc1.dst_local_start_time(2003) 
                      ==  posix_time.ptime( gregorian.date(2003, date_time.Apr,6)
                                            , posix_time.time_duration(2,0,0) ) )
     self.failUnless( nyc1.dst_local_start_time(2003) == nyc2.dst_local_start_time(2003) ) 
     self.failUnless( nyc1.dst_local_end_time(2003) 
                      == posix_time.ptime( gregorian.date(2003, date_time.Oct, 26)
                                           , posix_time.time_duration(2,0,0)))
     self.failUnless( nyc1.dst_local_end_time(2003) == nyc2.dst_local_end_time(2003) ) 
     self.failUnless( nyc1.to_posix_string() == "EST-05EDT+01,M4.1.0/02:00,M10.5.0/02:00" )
     self.failUnless( nyc2.to_posix_string() == "EST-05EDT+01,M4.1.0/02:00,M10.5.0/02:00")
     
     az1 = local_time.posix_time_zone( specs[0] )
     az2 = local_time.posix_time_zone( specs[1] )
     td = posix_time.hours(-7)
     
     self.failUnless( not az1.has_dst() and not az2.has_dst() )
     self.failUnless( az1.base_utc_offset() == td )
     self.failUnless( az1.base_utc_offset() == az2.base_utc_offset() )
     self.failUnless( az1.dst_local_start_time(2005) 
                      == posix_time.ptime( date_time.not_a_date_time) )
     self.failUnless( az2.dst_local_end_time(2005) 
                      == posix_time.ptime( date_time.not_a_date_time ) )
     self.failUnless( az1.std_zone_abbrev() == "MST" )
     self.failUnless( az2.std_zone_abbrev() == "MST" )
     self.failUnless( az1.dst_zone_abbrev() == "" )
     self.failUnless( az2.dst_zone_abbrev() == "" )
     self.failUnless( az1.std_zone_name() == "MST" )
     self.failUnless( az2.std_zone_name() == "MST" )
     self.failUnless( az1.dst_zone_name() == "" )
     self.failUnless( az2.dst_zone_name() == "" )
     self.failUnless( az1.to_posix_string() == "MST-07")
     self.failUnless( az2.to_posix_string() == "MST-07")
     
     bz = local_time.posix_time_zone("BST+11:21:15BDT-00:28,M2.2.4/03:15:42,M11.5.2/01:08:53")
     self.failUnless( bz.has_dst() )
     self.failUnless( bz.base_utc_offset() == posix_time.time_duration(11,21,15) )
     self.failUnless( bz.std_zone_abbrev() == "BST" )
     self.failUnless( bz.dst_zone_abbrev() == "BDT" )
     self.failUnless( bz.dst_offset() == posix_time.time_duration(0,-28,0) )
     self.failUnless( bz.dst_local_start_time(1962) 
                      == posix_time.ptime( gregorian.date(1962, date_time.Feb,8)
                                           , posix_time.time_duration(3,15,42) ) )
     self.failUnless( bz.dst_local_end_time(1962) 
                      == posix_time.ptime( gregorian.date( 1962, date_time.Nov,27)
                                           , posix_time.time_duration(1,8,53) ) )
     
     la1 = local_time.posix_time_zone( specs[4] )         
     self.failUnless( la1.dst_local_start_time(2003) 
                      == posix_time.ptime( gregorian.date(2003, date_time.Feb,15)
                                           , posix_time.time_duration(1,30,0) ) )
     self.failUnless( la1.dst_local_end_time(2003) 
                      == posix_time.ptime( gregorian.date(2003, date_time.Nov,6)
                                           , posix_time.time_duration(2,0,0) ) )
     self.failUnless( la1.to_posix_string() == "PST-08PDT+01,45/01:30,310/02:00")
     
     la2 = local_time.posix_time_zone( specs[5] ) 
     self.failUnless( la2.dst_local_start_time(2003) 
                      == posix_time.ptime( gregorian.date(2003, date_time.Feb,15)
                                           ,posix_time.time_duration(2,0,0) ) )
     self.failUnless( la2.dst_local_end_time(2003) 
                      == posix_time.ptime( gregorian.date(2003, date_time.Nov,6)
                                           , posix_time.time_duration(0,30,0) ) )
     self.failUnless( la2.to_posix_string() == "PST-08PDT+01,45/02:00,310/00:30" )
     
     self.failUnlessRaises( Exception, local_time.posix_time_zone, "EST-13" )
     self.failUnlessRaises( Exception, local_time.posix_time_zone, "EST-5EDT24:00:01,J124/1:30,J310" )
     self.failUnlessRaises( Exception, local_time.posix_time_zone, "EST-5EDT01:00:00,J124/-1:30,J310" )
     self.failUnlessRaises( Exception, local_time.posix_time_zone, "EST-5EDT01:00:00,J124/1:30,J370" )
          
     cet_tz = local_time.posix_time_zone("CET+01:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00")
     self.failUnless( cet_tz.has_dst() )
     self.failUnless( cet_tz.base_utc_offset() == posix_time.hours(1) )
     self.failUnless( cet_tz.std_zone_abbrev() == "CET") 
     
     caus_tz = local_time.posix_time_zone("CAS+08:30:00CDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00")
     self.failUnless( caus_tz.has_dst() )
     self.failUnless( caus_tz.base_utc_offset() == posix_time.hours(8)+posix_time.minutes(30) )
     self.failUnless( caus_tz.std_zone_abbrev() == "CAS")
     
     spec = "FST+3FDT,J60,J304"
     fl_1 = local_time.posix_time_zone(spec)
     self.failUnless( fl_1.dst_local_start_time(2003) 
                      == posix_time.ptime( gregorian.date(2003,date_time.Mar,1)
                                           ,posix_time.hours(2) ) )
     self.failUnless( fl_1.dst_local_end_time(2003) 
                      == posix_time.ptime( gregorian.date(2003,date_time.Oct,31)
                                           , posix_time.hours(2) ) )
     self.failUnless( fl_1.dst_local_start_time(2004) 
                      == posix_time.ptime( gregorian.date(2004, date_time.Mar, 1)
                                           , posix_time.hours(2) ) )
     self.failUnless( fl_1.dst_local_end_time(2004) 
                      == posix_time.ptime( gregorian.date(2004,date_time.Oct,31)
                                           , posix_time.hours(2) ) )
Ejemplo n.º 8
0
    def test_duration(self):
        td0 = posix_time.time_duration()
        self.failUnless(posix_time.time_duration().ticks() == 0)
        td1 = posix_time.time_duration(1, 25, 0)
        td3 = posix_time.time_duration(td1.hours(), td1.minutes(),
                                       td1.seconds())
        self.failUnless(td1 == td3)
        td1 = -td1
        td3 = posix_time.time_duration(td1.hours(), td1.minutes(),
                                       td1.seconds())
        self.failUnless(td1 == td3)
        t_1 = posix_time.time_duration(0, 1, 40)
        t_2 = posix_time.time_duration(0, 1, 41)
        self.failUnless(not (t_2 < t_2))
        self.failUnless(t_1 == t_1)
        self.failUnless(t_1 >= t_1)
        self.failUnless(t_2 >= t_1)
        self.failUnless(t_2 <= t_2)
        self.failUnless(t_2 > t_1)
        self.failUnless(not (t_1 > t_2))
        t_3 = posix_time.time_duration(0, 1, 41)
        self.failUnless(t_2 == t_3)
        t_4 = posix_time.time_duration(0, 1, 41)
        self.failUnless(t_2 == t_4)

        t_5 = posix_time.time_duration(1, 30, 20, 10)
        t_5 /= 2
        self.failUnless(t_5.hours() == 0 and t_5.minutes() == 45
                        and t_5.seconds() == 10
                        and t_5.fractional_seconds() == 5)
        t_5 = posix_time.time_duration(3, 15, 8, 0) / 2
        self.failUnless(t_5 == posix_time.time_duration(1, 37, 34, 0))
        td = posix_time.hours(5)
        td *= 5
        self.failUnless(td == posix_time.time_duration(25, 0, 0, 0))
        t_5 = t_2 + t_1
        self.failUnless(t_5 == posix_time.time_duration(0, 3, 21))

        td_a = posix_time.time_duration(5, 5, 5, 5)
        td_b = posix_time.time_duration(4, 4, 4, 4)
        td_c = posix_time.time_duration(2, 2, 2, 2)
        td_a += td_b
        self.failUnless(td_a == posix_time.time_duration(9, 9, 9, 9))
        td_d = td_b - td_c
        self.failUnless(td_d == posix_time.time_duration(2, 2, 2, 2))
        td_d -= td_b
        self.failUnless(td_d == td_c - td_b)

        utd = posix_time.time_duration(1, 2, 3, 4)
        utd2 = -utd

        self.failUnless(utd2.hours() == -1 and utd2.minutes() == -2
                        and utd2.seconds() == -3
                        and utd2.fractional_seconds() == -4)
        utd2 = -posix_time.hours(5)
        self.failUnless(utd2.hours() == -5)
        utd2 = -utd2
        self.failUnless(utd2.hours() == 5)

        t_6 = posix_time.time_duration(5, 4, 3)
        self.failUnless(t_6.hours() == 5)
        self.failUnless(t_6.minutes() == 4)
        self.failUnless(t_6.seconds() == 3)

        self.failUnless(t_6.total_seconds() == 18243)

        tenhours = posix_time.hours(10)
        fivemin = posix_time.minutes(5)
        t7 = posix_time.time_duration(1, 2, 3) + tenhours + fivemin
        self.failUnless(t7 == posix_time.time_duration(11, 7, 3))
        t8 = tenhours + posix_time.time_duration(1, 2, 3) + fivemin
        self.failUnless(t8 == posix_time.time_duration(11, 7, 3))
        if posix_time.time_duration.resolution() >= date_time.micro:
            t_9 = posix_time.time_duration(5, 4, 3, 9876)
            self.failUnless(t_9.hours() == 5)
            self.failUnless(t_9.minutes() == 4)
            self.failUnless(t_9.seconds() == 3)
            self.failUnless(t_9.fractional_seconds() == 9876)
            self.failUnless(t_9.total_seconds() == 18243)

        if posix_time.time_duration.resolution() >= date_time.tenth:
            t_10 = posix_time.time_duration(5, 4, 3, 9)
            self.failUnless(t_10.hours() == 5)
            self.failUnless(t_10.minutes() == 4)
            self.failUnless(t_10.seconds() == 3)
            self.failUnless(t_10.fractional_seconds() == 9)
            self.failUnless(t_10.total_seconds() == 18243)

        if posix_time.time_duration.resolution() >= date_time.milli:
            ms = posix_time.millisec(9)
            if posix_time.time_duration.resolution() == date_time.nano:
                self.failUnless(ms.fractional_seconds() == 9000000)
                self.failUnless(ms.total_seconds() == 0)
                self.failUnless(ms.total_milliseconds() == 9)
                self.failUnless(
                    posix_time.time_duration.ticks_per_second() == 1000000000)
            else:
                self.failUnless(ms.fractional_seconds() == 9000)
                self.failUnless(ms.total_seconds() == 0)
                self.failUnless(ms.total_milliseconds() == 9)

        if hasattr(posix_time, 'nanoseconds'):
            if posix_time.time_duration.resolution() >= date_time.nano:
                ns = posix_time.nanosec(9)

                self.failUnless(ns.fractional_seconds() == 9)
                self.failUnless(ns.total_nanoseconds() == 9)
                self.failUnless(ns.total_microseconds() == 0)

                ns18 = ns + ns
                self.failUnless(ns18.fractional_seconds() == 18)

                ns2 = posix_time.nanosec(1000000000)
                self.failUnless(ns2 == seconds(1))
                self.failUnless(ns2.total_seconds() == 1)

                self.failUnless((nanosec(3) / 2) == nanosec(1))
                self.failUnless(nanosec(3) * 1000 == microsec(3))

        t_11 = posix_time.time_duration(3600, 0, 0)
        self.failUnless(t_11.hours() == 3600)
        self.failUnless(t_11.total_seconds() == 12960000)

        td_12 = posix_time.time_duration(1, 2, 3, 10)

        self.failUnless(td_12.total_seconds() == 3723)

        self.failUnless(posix_time.hours(2) / 2 == posix_time.hours(1))
        self.failUnless(posix_time.hours(3) / 2 == posix_time.minutes(90))

        self.failUnless(
            posix_time.time_duration(3, 0, 0) * 2 == posix_time.hours(6))
        self.failUnless(
            posix_time.hours(3600) * 1000 == posix_time.hours(3600000))

        pi_dur = posix_time.time_duration(date_time.pos_infin)
        ni_dur = posix_time.time_duration(date_time.neg_infin)
        ndt_dur = posix_time.time_duration(date_time.not_a_date_time)
        self.failUnless(pi_dur + ni_dur == ndt_dur)
        self.failUnless(pi_dur / 3 == pi_dur)
        self.failUnless(pi_dur + td_12 == pi_dur)
        self.failUnless(pi_dur - td_12 == pi_dur)
        self.failUnless(-pi_dur == ni_dur)
        self.failUnless(ni_dur < pi_dur)
        self.failUnless(ni_dur < td_12)
        self.failUnless(pi_dur > td_12)

        self.failUnless(to_simple_string(pi_dur) == "+infinity")
        self.failUnless(to_simple_string(ni_dur) == "-infinity")
        self.failUnless(to_simple_string(ndt_dur) == "not-a-date-time")

        t1 = posix_time.ptime(gregorian.date(2001, 7, 14))
        t2 = posix_time.ptime(gregorian.date(2002, 7, 14))
        self.failUnless(365 * 24 == (t2 - t1).hours())
        self.failUnless(365 * 24 * 3600 == (t2 - t1).total_seconds())

        self.failUnless(posix_time.seconds(1).total_milliseconds() == 1000)
        self.failUnless(posix_time.seconds(1).total_microseconds() == 1000000)
        self.failUnless(
            posix_time.seconds(1).total_nanoseconds() == 1000000000)
        self.failUnless(
            posix_time.hours(1).total_milliseconds() == 3600 * 1000)

        tms = 3600 * 1000000 * 1001
        self.failUnless(posix_time.hours(1001).total_microseconds() == tms)
        tms = 3600 * 365 * 24 * 1000
        self.failUnless((t2 - t1).total_milliseconds() == tms)
        tms = 3600 * 365 * 24 * 1000000000
        self.failUnless((t2 - t1).total_nanoseconds() == tms)
        print posix_time.microseconds(
            25).ticks() / posix_time.time_duration.ticks_per_second()