예제 #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) ) )
예제 #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))))
예제 #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))))
예제 #4
0
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
nyc_arrival = phx_arrival.local_time_in(nyc_tz, posix_time.time_duration())
print "New York arrival: ", nyc_arrival.to_string(
)  #//2005-Apr-03 06:30:00 EDT
예제 #5
0
# 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
nyc_arrival = phx_arrival.local_time_in(nyc_tz, posix_time.time_duration())
print "New York arrival: ", nyc_arrival.to_string()  # //2005-Apr-03 06:30:00 EDT
예제 #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()        
예제 #7
0
    def test_time(self):
        td_pi = posix_time.time_duration(date_time.pos_infin)
        td_ni = posix_time.time_duration(date_time.neg_infin)
        td_ndt = posix_time.time_duration(date_time.not_a_date_time)
        dd_pi = gregorian.date_duration(date_time.pos_infin)
        dd_ni = gregorian.date_duration(date_time.neg_infin)
        dd_ndt = gregorian.date_duration(date_time.not_a_date_time)
        d_pi = gregorian.date(date_time.pos_infin)
        d_ni = gregorian.date(date_time.neg_infin)
        d_ndt = gregorian.date(date_time.not_a_date_time)
        td = posix_time.time_duration(1,2,3,4)
        dd = gregorian.date_duration(1234);
        d = gregorian.date(2003, date_time.Oct,31)

        self.failUnless(  posix_time.ptime() == posix_time.ptime(date_time.not_a_date_time) )
        
        s = "+infinity"
        p_sv1 = posix_time.ptime(date_time.pos_infin);
        self.failUnless(  to_simple_string(p_sv1) == s )
        result = p_sv1 + dd
        self.failUnless( to_iso_string(result) == s )
        self.failUnless( result.is_special() )
        result = p_sv1 - dd
        self.failUnless( to_iso_string(result) == s)
        result = p_sv1 - dd_ni
        self.failUnless( to_iso_string(result) == s)
        p_sv2 = posix_time.ptime(date_time.neg_infin)

        s = "-infinity"
        self.failUnless( to_iso_string(p_sv2) == s)
        result = p_sv2 - td_pi
        self.failUnless( to_iso_string(result) == s )
        p_sv3 = posix_time.ptime(date_time.not_a_date_time)
        self.failUnless(  p_sv3.is_special() )
        s = "not-a-date-time";
        self.failUnless(  to_iso_string(p_sv3) == s)
        result = p_sv3 + td
        self.failUnless(  to_iso_string(result) == s)
        result = p_sv3 - td
        self.failUnless(  to_iso_string(result) == s)
        result = p_sv2 + td_pi
        self.failUnless(  to_iso_string(result) == s)
        result = p_sv1 + dd_ni
        self.failUnless(  to_iso_string(result) == s)
        result = p_sv1 + dd_ndt
        self.failUnless(  to_iso_string(result) == s)

        p_sv1 = posix_time.ptime(d_pi, td)
        s = "+infinity"
        self.failUnless( to_simple_string(p_sv1) == s)
        p_sv2 = posix_time.ptime(d_ni, td)
        s = "-infinity";
        self.failUnless( to_iso_string(p_sv2) == s)
        p_sv3 = posix_time.ptime(d_ndt, td)
        s = "not-a-date-time";
        self.failUnless( to_iso_string(p_sv3) == s)

        p_sv1 = posix_time.ptime(d_ndt, td)
        s = "not-a-date-time"
        self.failUnless( to_simple_string(p_sv1) == s)
        p_sv2 = posix_time.ptime(d, td_ndt)
        self.failUnless( to_iso_string(p_sv2) == s)
        p_sv3 = posix_time.ptime(d_pi, td_ni)
        self.failUnless( to_iso_string(p_sv3) == s)

        p_sv1 = posix_time.ptime(d, td_pi)
        pt = posix_time.ptime(d,td)
        s = "+infinity"
        self.failUnless( to_simple_string(p_sv1) == s)
        self.failUnless( to_simple_string(p_sv1.date()) == s)
        p_sv2 = posix_time.ptime(d, td_ni)
        
        s = "-infinity"
        self.failUnless( to_iso_string(p_sv2) == s)
        self.failUnless( to_simple_string(p_sv2.time_of_day()) == s)
        p_sv3 = posix_time.ptime(d, td_ndt)
        s = "not-a-date-time"
        self.failUnless( to_iso_string(p_sv3) == s)
        self.failUnless( to_simple_string(p_sv3.date()) == s)
        self.failUnless( to_simple_string(p_sv3.time_of_day()) == s)
        self.failUnless( p_sv2 < p_sv1)
        self.failUnless( p_sv2 < pt)
        self.failUnless( pt < p_sv1)
        self.failUnless( p_sv2 <= p_sv2)
        self.failUnless( p_sv2 <= pt)
        self.failUnless( p_sv1 >= pt)
        self.failUnless( p_sv1 != p_sv2)
        self.failUnless( p_sv3 != p_sv2)
        self.failUnless( pt != p_sv1)
        
        self.failUnless( p_sv1.is_infinity() and p_sv1.is_pos_infinity())
        self.failUnless( p_sv2.is_infinity() and p_sv2.is_neg_infinity())
        self.failUnless( not p_sv3.is_infinity() and p_sv3.is_not_a_date_time())
        
        self.failUnless( p_sv1 + dd == p_sv1)
        self.failUnless( pt - dd_pi == p_sv2)
        self.failUnless( pt - dd_ndt == p_sv3)
        
        self.failUnless( p_sv2 + td == p_sv2)
        self.failUnless( pt - td_ni == p_sv1)
        self.failUnless( pt + td_ndt == p_sv3)
        self.failUnless( pt - p_sv1 == td_ni)
        self.failUnless( pt - p_sv2 == td_pi)
        self.failUnless( pt - p_sv3 == td_ndt)
        self.failUnless( p_sv2 - p_sv2 == td_ndt)

        d = gregorian.date(2001, date_time.Dec,1)
        td = posix_time.time_duration(5,4,3)
        t1 = posix_time.ptime(d, td)
        self.failUnless( t1.date() == d)
        self.failUnless( t1.time_of_day() == td)
        self.failUnless( posix_time.ptime( gregorian.date(2005,10,30), posix_time.hours(25)) 
                         == posix_time.ptime(gregorian.date(2005,10,31), posix_time.hours(1)))
        t2 = posix_time.ptime(d, td)
        t3 = posix_time.ptime(d, td)
        self.failUnless( t3.date() == d)
        self.failUnless( t3.time_of_day() == td)
        self.failUnless( t1 == t3)
        d2 = gregorian.date(2001,date_time.Jan,1)
        t4 = posix_time.ptime(d2, td)
        self.failUnless( not (t1 == t4))
        td1 = posix_time.time_duration(5,4,0)
        t5 = posix_time.ptime(d, td1)
        self.failUnless( not (t1 == t5))
        self.failUnless( t1 != t5);
        
        self.failUnless( not (t1 < t1) )
        self.failUnless( t4 < t1)
        self.failUnless( t5 < t1)
        self.failUnless( t1 <= t1)
        self.failUnless( t1 >= t1)
        
        twodays = gregorian.date_duration(2)
        t6 = t1 + twodays
        d3 = gregorian.date(2001,date_time.Dec,3)
        self.failUnless( t6 == posix_time.ptime(d3,td) )
        t7 = t1 - twodays
        self.failUnless( t7 == posix_time.ptime(gregorian.date(2001,date_time.Nov,29),td) )
        
        t6b = posix_time.ptime( gregorian.date(2003, date_time.Oct,31)
                                , posix_time.time_duration(10,0,0,0))
        t6b += gregorian.date_duration(55)
        self.failUnless( t6b == posix_time.ptime(gregorian.date(2003,date_time.Dec,25)
                                                 , posix_time.time_duration(10,0,0,0)))
        t6b += posix_time.hours(6)
        self.failUnless( t6b == posix_time.ptime(gregorian.date(2003,date_time.Dec,25)
                                                 , posix_time.time_duration(16,0,0,0)))
        t6b -= gregorian.date_duration(55)
        self.failUnless( t6b == posix_time.ptime(gregorian.date(2003,date_time.Oct,31)
                                                 , posix_time.time_duration(16,0,0,0)))
        t6b -= posix_time.hours(6)
        self.failUnless( t6b == posix_time.ptime(gregorian.date(2003,date_time.Oct,31)
                                                 , posix_time.time_duration(10,0,0,0)));
        t6b += posix_time.hours(25);
        self.failUnless( t6b ==
              posix_time.ptime(gregorian.date(2003,date_time.Nov,1), posix_time.time_duration(11,0,0,0)));
        t6b -= posix_time.hours(49);
        self.failUnless( t6b == posix_time.ptime(gregorian.date(2003,date_time.Oct,30)
                                                 , posix_time.time_duration(10,0,0,0)));

        td2 = posix_time.time_duration(1,2,3)
        t8 = posix_time.ptime(gregorian.date(2001,date_time.Dec,1))
        t9 = t8 + td2 
        t10 = posix_time.ptime(gregorian.date(2001,date_time.Dec,1)
                               ,posix_time.time_duration(1,2,3))
        
        self.failUnless( t9 == t10)
        t9 = posix_time.ptime(gregorian.date(2001,date_time.Dec,1)
                              , posix_time.time_duration(12,0,0)); 
        td3 = posix_time.time_duration(-4,0,0)
        self.failUnless( t9 + td3 
                         == posix_time.ptime(gregorian.date(2001,date_time.Dec,1)
                                             , posix_time.time_duration(8,0,0)) )
        td3 = posix_time.time_duration(24,0,0)
        self.failUnless( t8+td3 == posix_time.ptime(gregorian.date(2001,date_time.Dec,2) ) )
        td4 = posix_time.time_duration(24,0,1)
        self.failUnless( t8+td4 == posix_time.ptime(gregorian.date(2001,date_time.Dec,2)
                                                    , posix_time.time_duration(0,0,1)));

        td5 = posix_time.time_duration(168,0,1)
        self.failUnless( t8+td5 == posix_time.ptime(gregorian.date(2001,date_time.Dec,8)
                                                    , posix_time.time_duration(0,0,1)))
        
        t11 = posix_time.ptime(gregorian.date(2001,date_time.Dec,1)
                               , posix_time.time_duration(12,0,0))
        td6 = posix_time.time_duration(12,0,1)
        t12 = t11-td6
        self.failUnless( t12 == posix_time.ptime(gregorian.date(2001,date_time.Nov,30)
                                                 , posix_time.time_duration(23,59,59)))
        self.failUnless( t11-posix_time.time_duration(13,0,0)
                         == posix_time.ptime(gregorian.date(2001,date_time.Nov,30)
                                             , posix_time.time_duration(23,0,0)))
        self.failUnless( t11-posix_time.time_duration(-13,0,0)
                         == posix_time.ptime(gregorian.date(2001,date_time.Dec,2)
                                             , posix_time.time_duration(1,0,0)))
        
        t13 = posix_time.ptime(d, posix_time.hours(3))
        t14 = posix_time.ptime(d, posix_time.hours(4))
        t14a = posix_time.ptime(d+gregorian.date_duration(1), posix_time.hours(4))
        
        self.failUnless( t14-t13 == posix_time.hours(1))
        
        self.failUnless( t13 - t14 == posix_time.hours(-1))
        self.failUnless( t14a-t14 == posix_time.hours(24))
        
        t15 = posix_time.ptime(d, posix_time.time_duration(0,0,0,1))
        t16 = posix_time.ptime(d, posix_time.time_duration(0,0,0,2))
        self.failUnless( t15 + posix_time.time_duration.unit() == t16)
        self.failUnless( t16 - posix_time.time_duration.unit() == t15)
                
        self.failUnless( posix_time.ptime(date_time.min_date_time)
                         == posix_time.ptime(gregorian.date(1400,1,1)
                                             , posix_time.time_duration(0,0,0,0)) )
        
        self.failUnless( posix_time.ptime(date_time.max_date_time)
                         == posix_time.ptime(gregorian.date(9999,12,31)
                                             , posix_time.hours(24)- posix_time.time_duration.unit()))
예제 #8
0
    def test_local_time( self ):
        az_tz = local_time.posix_time_zone("MST-07")
        ny_tz = local_time.posix_time_zone("EST-05EDT,M4.1.0,M10.5.0")
        sydney = local_time.posix_time_zone("EST+10EST,M10.5.0,M3.5.0/03:00")
        null_tz = None
        d = gregorian.date(2003, 12, 20)
        h = posix_time.hours(12)
        t = posix_time.ptime(d,h)
        az_time = local_time.local_date_time(t, az_tz ) 
        
        self.failUnless( az_time.zone().std_zone_abbrev() == "MST") 
        self.failUnless( az_time.zone().base_utc_offset() == posix_time.hours(-7) )
        self.failUnless( az_time.zone().has_dst() == False )
        self.failUnless( az_time.is_dst() == False )
        self.failUnless( az_time.to_string() == "2003-Dec-20 05:00:00 MST" )
        
        az_time2 = local_time.local_date_time(t, az_tz ) 
        
        self.failUnless( az_time2.is_dst() == False )
        self.failUnless( az_time2.to_string() == "2003-Dec-20 05:00:00 MST" )
        self.failUnless( az_time2.zone().has_dst() == False )
        self.failUnless( az_time2.zone().base_utc_offset() == posix_time.hours(-7) )
                
        svpt = posix_time.ptime(date_time.not_a_date_time)
        sv_time = local_time.local_date_time(svpt, ny_tz)
        self.failUnless( sv_time.is_not_a_date_time() )
        self.failUnless( sv_time.to_string() == "not-a-date-time" )
        self.failUnless( sv_time.is_dst() == False )
        sv_time2 = local_time.local_date_time( date_time.pos_infin, None )
        self.failUnless( sv_time2.is_pos_infinity() )
        self.failUnless( sv_time2.to_string() == "+infinity" )
        self.failUnless( sv_time2.is_dst() == False )
        sv_time2 += gregorian.days(12) 
        self.failUnless( sv_time2.is_pos_infinity() )

        sv_time3 = local_time.local_date_time( date_time.max_date_time, ny_tz )
        self.failUnless( sv_time3.to_string() == "9999-Dec-31 18:59:59.999999999 EST" 
                         or sv_time3.to_string() == "9999-Dec-31 18:59:59.999999 EST" )
        
        sv_time4 = local_time.local_date_time(date_time.min_date_time, None)
        self.failUnless( sv_time4.to_string() == "1400-Jan-01 00:00:00 UTC" )
        
        d = gregorian.date(2004, date_time.Apr, 4)
        td = posix_time.time_duration(2,30,0) 
        calcop = local_time.local_date_time(d, td, ny_tz, local_time.local_date_time.NOT_DATE_TIME_ON_ERROR)
        self.failUnless( calcop.is_not_a_date_time() )

        d = gregorian.date(2004, date_time.Apr, 4)
        td = posix_time.time_duration(2,30,0) 
        self.failUnlessRaises( Exception, local_time.local_date_time, d, td, ny_tz, local_time.local_date_time.EXCEPTION_ON_ERROR)
        
        az_time3 = local_time.local_date_time(d, h, az_tz, False)
        self.failUnless( az_time3.zone().std_zone_abbrev() == "MST") 
        self.failUnless( az_time3.zone().base_utc_offset() == posix_time.hours(-7) )
        self.failUnless( az_time3.zone().has_dst() == False )
        self.failUnless( az_time3.is_dst() == False )
        self.failUnless( az_time3.to_string() == "2004-Apr-04 12:00:00 MST" )
        
        null_tz_time = local_time.local_date_time(d, h, null_tz, True)
        
        self.failUnless( null_tz_time.is_dst() == False )
        self.failUnless( null_tz_time.to_string() == "2004-Apr-04 12:00:00 UTC" )
        
        self.failUnlessRaises( Exception
                               , local_time.local_date_time
                               , gregorian.date(2004,date_time.Apr,4)
                               , posix_time.time_duration(2,30,0)
                               , ny_tz
                               , True )
          
        d = gregorian.date(2004, date_time.Apr,4)
        td = posix_time.time_duration(1,15,0) 
        lt1 = local_time.local_date_time(d,td,ny_tz,False)
        lt2 = local_time.local_date_time( posix_time.ptime(d
                                                           , posix_time.time_duration(6,15,0) )
                                          , ny_tz)
        self.failUnless( lt1.utc_time() == lt2.utc_time( ))
        self.failUnless( lt1.is_dst() == False )
        self.failUnless( lt2.is_dst() == False )
        lt1 += posix_time.hours(1)
        lt2 += posix_time.hours(1)
        self.failUnless( lt1.is_dst() == True)
        self.failUnless( lt2.is_dst() == True)
    
        d = gregorian.date(2004,date_time.Oct,31)
        td = posix_time.time_duration(1,15,0) 
        lt1 = local_time.local_date_time(d,td,ny_tz,True)
        lt2 = local_time.local_date_time(posix_time.ptime(d
                                                          , posix_time.time_duration(5,15,0))
                                         , ny_tz)
        self.failUnless( lt1.utc_time() == lt2.utc_time() )
        self.failUnless( lt1.is_dst() == True )
        self.failUnless( lt2.is_dst() == True )
        lt1 += posix_time.hours(1)
        lt2 += posix_time.hours(1)
        self.failUnless( lt1.is_dst() == False )
        self.failUnless( lt2.is_dst() == False )
        
        ny_time = az_time.local_time_in(ny_tz, posix_time.time_duration(0,0,0))
        self.failUnless( ny_time.zone().std_zone_abbrev() == "EST" )
        self.failUnless( ny_time.zone().base_utc_offset() == posix_time.hours(-5) )
        self.failUnless( ny_time.zone().has_dst() == True )
        self.failUnless( ny_time.to_string() == "2003-Dec-20 07:00:00 EST" )
        ny_time += posix_time.hours(3)
        self.failUnless( ny_time.to_string() == "2003-Dec-20 10:00:00 EST" )
        ny_time += gregorian.days(3)
        self.failUnless( ny_time.to_string() == "2003-Dec-23 10:00:00 EST" )

        d = gregorian.date(2003, date_time.Aug, 28)
        sv_pt = posix_time.ptime(date_time.pos_infin)
        sv_lt = local_time.local_date_time(sv_pt, ny_tz)
        utc_pt = posix_time.ptime(d, posix_time.hours(12))
        
        utc_lt = local_time.local_date_time(utc_pt, null_tz)           
        az_lt = local_time.local_date_time(d, posix_time.hours(5), az_tz, False)  
        ny_lt = local_time.local_date_time(d, posix_time.hours(8), ny_tz, True)   
        au_lt = local_time.local_date_time(d, posix_time.hours(22), sydney, False)
        
        self.failUnless( az_lt == utc_lt ) 
        self.failUnless( ny_lt == utc_lt ) 
        self.failUnless( au_lt == utc_lt ) 
        self.failUnless( au_lt <= utc_lt ) 
        self.failUnless( au_lt >= utc_lt ) 
        self.failUnless( az_lt == ny_lt )  
        self.failUnless( az_lt < ny_lt + posix_time.seconds(1) ) 
        self.failUnless( az_lt + posix_time.seconds(1) >  ny_lt ) 
        self.failUnless( az_lt <= ny_lt ) 
        self.failUnless( az_lt >=  ny_lt ) 
        self.failUnless( az_lt + posix_time.seconds(1) !=  ny_lt ) 
        
        au_lt += posix_time.hours(1)
        self.failUnless( au_lt != utc_lt ) 
        self.failUnless( utc_lt <= au_lt )  
        self.failUnless( au_lt >= utc_lt ) 
        self.failUnless( utc_lt < au_lt ) 
        self.failUnless( au_lt > utc_lt )
        au_lt -= posix_time.hours(1)
        self.failUnless( au_lt == utc_lt ) 
        
        self.failUnless( (az_lt + gregorian.days(2)).to_string() == "2003-Aug-30 05:00:00 MST")
        self.failUnless( (az_lt - gregorian.days(2)).to_string() == "2003-Aug-26 05:00:00 MST")
        
        az_lt += gregorian.days(2)
        self.failUnless( az_lt.to_string() == "2003-Aug-30 05:00:00 MST")
        az_lt -= gregorian.days(2)
        self.failUnless( az_lt.to_string() == "2003-Aug-28 05:00:00 MST")
        self.failUnless( (az_lt + posix_time.hours(2)).to_string() == "2003-Aug-28 07:00:00 MST")
        self.failUnless( (az_lt - posix_time.hours(2)).to_string() == "2003-Aug-28 03:00:00 MST")
        
        self.failUnless( sv_lt > au_lt) 
        sv_lt2 = sv_lt + gregorian.days(2)
        self.failUnless( sv_lt2 == sv_lt)

        m = gregorian.months(2)
        y = gregorian.years(2)
        self.failUnless( (az_lt + m).to_string() == "2003-Oct-28 05:00:00 MST")
        az_lt += m
        self.failUnless( az_lt.to_string() == "2003-Oct-28 05:00:00 MST")
        self.failUnless( (az_lt - m).to_string() == "2003-Aug-28 05:00:00 MST")
        az_lt -= m
        self.failUnless( az_lt.to_string() == "2003-Aug-28 05:00:00 MST")
        self.failUnless( (az_lt + y).to_string() == "2005-Aug-28 05:00:00 MST")
        az_lt += y
        self.failUnless( az_lt.to_string() == "2005-Aug-28 05:00:00 MST")
        self.failUnless( (az_lt - y).to_string() == "2003-Aug-28 05:00:00 MST")
        az_lt -= y
        self.failUnless( az_lt.to_string() == "2003-Aug-28 05:00:00 MST")
예제 #9
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) ) )
예제 #10
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()
예제 #11
0
    def test_time(self):
        td_pi = posix_time.time_duration(date_time.pos_infin)
        td_ni = posix_time.time_duration(date_time.neg_infin)
        td_ndt = posix_time.time_duration(date_time.not_a_date_time)
        dd_pi = gregorian.date_duration(date_time.pos_infin)
        dd_ni = gregorian.date_duration(date_time.neg_infin)
        dd_ndt = gregorian.date_duration(date_time.not_a_date_time)
        d_pi = gregorian.date(date_time.pos_infin)
        d_ni = gregorian.date(date_time.neg_infin)
        d_ndt = gregorian.date(date_time.not_a_date_time)
        td = posix_time.time_duration(1, 2, 3, 4)
        dd = gregorian.date_duration(1234)
        d = gregorian.date(2003, date_time.Oct, 31)

        self.failUnless(
            posix_time.ptime() == posix_time.ptime(date_time.not_a_date_time))

        s = "+infinity"
        p_sv1 = posix_time.ptime(date_time.pos_infin)
        self.failUnless(to_simple_string(p_sv1) == s)
        result = p_sv1 + dd
        self.failUnless(to_iso_string(result) == s)
        self.failUnless(result.is_special())
        result = p_sv1 - dd
        self.failUnless(to_iso_string(result) == s)
        result = p_sv1 - dd_ni
        self.failUnless(to_iso_string(result) == s)
        p_sv2 = posix_time.ptime(date_time.neg_infin)

        s = "-infinity"
        self.failUnless(to_iso_string(p_sv2) == s)
        result = p_sv2 - td_pi
        self.failUnless(to_iso_string(result) == s)
        p_sv3 = posix_time.ptime(date_time.not_a_date_time)
        self.failUnless(p_sv3.is_special())
        s = "not-a-date-time"
        self.failUnless(to_iso_string(p_sv3) == s)
        result = p_sv3 + td
        self.failUnless(to_iso_string(result) == s)
        result = p_sv3 - td
        self.failUnless(to_iso_string(result) == s)
        result = p_sv2 + td_pi
        self.failUnless(to_iso_string(result) == s)
        result = p_sv1 + dd_ni
        self.failUnless(to_iso_string(result) == s)
        result = p_sv1 + dd_ndt
        self.failUnless(to_iso_string(result) == s)

        p_sv1 = posix_time.ptime(d_pi, td)
        s = "+infinity"
        self.failUnless(to_simple_string(p_sv1) == s)
        p_sv2 = posix_time.ptime(d_ni, td)
        s = "-infinity"
        self.failUnless(to_iso_string(p_sv2) == s)
        p_sv3 = posix_time.ptime(d_ndt, td)
        s = "not-a-date-time"
        self.failUnless(to_iso_string(p_sv3) == s)

        p_sv1 = posix_time.ptime(d_ndt, td)
        s = "not-a-date-time"
        self.failUnless(to_simple_string(p_sv1) == s)
        p_sv2 = posix_time.ptime(d, td_ndt)
        self.failUnless(to_iso_string(p_sv2) == s)
        p_sv3 = posix_time.ptime(d_pi, td_ni)
        self.failUnless(to_iso_string(p_sv3) == s)

        p_sv1 = posix_time.ptime(d, td_pi)
        pt = posix_time.ptime(d, td)
        s = "+infinity"
        self.failUnless(to_simple_string(p_sv1) == s)
        self.failUnless(to_simple_string(p_sv1.date()) == s)
        p_sv2 = posix_time.ptime(d, td_ni)

        s = "-infinity"
        self.failUnless(to_iso_string(p_sv2) == s)
        self.failUnless(to_simple_string(p_sv2.time_of_day()) == s)
        p_sv3 = posix_time.ptime(d, td_ndt)
        s = "not-a-date-time"
        self.failUnless(to_iso_string(p_sv3) == s)
        self.failUnless(to_simple_string(p_sv3.date()) == s)
        self.failUnless(to_simple_string(p_sv3.time_of_day()) == s)
        self.failUnless(p_sv2 < p_sv1)
        self.failUnless(p_sv2 < pt)
        self.failUnless(pt < p_sv1)
        self.failUnless(p_sv2 <= p_sv2)
        self.failUnless(p_sv2 <= pt)
        self.failUnless(p_sv1 >= pt)
        self.failUnless(p_sv1 != p_sv2)
        self.failUnless(p_sv3 != p_sv2)
        self.failUnless(pt != p_sv1)

        self.failUnless(p_sv1.is_infinity() and p_sv1.is_pos_infinity())
        self.failUnless(p_sv2.is_infinity() and p_sv2.is_neg_infinity())
        self.failUnless(not p_sv3.is_infinity() and p_sv3.is_not_a_date_time())

        self.failUnless(p_sv1 + dd == p_sv1)
        self.failUnless(pt - dd_pi == p_sv2)
        self.failUnless(pt - dd_ndt == p_sv3)

        self.failUnless(p_sv2 + td == p_sv2)
        self.failUnless(pt - td_ni == p_sv1)
        self.failUnless(pt + td_ndt == p_sv3)
        self.failUnless(pt - p_sv1 == td_ni)
        self.failUnless(pt - p_sv2 == td_pi)
        self.failUnless(pt - p_sv3 == td_ndt)
        self.failUnless(p_sv2 - p_sv2 == td_ndt)

        d = gregorian.date(2001, date_time.Dec, 1)
        td = posix_time.time_duration(5, 4, 3)
        t1 = posix_time.ptime(d, td)
        self.failUnless(t1.date() == d)
        self.failUnless(t1.time_of_day() == td)
        self.failUnless(
            posix_time.ptime(gregorian.date(2005, 10, 30), posix_time.hours(
                25)) == posix_time.ptime(gregorian.date(2005, 10, 31),
                                         posix_time.hours(1)))
        t2 = posix_time.ptime(d, td)
        t3 = posix_time.ptime(d, td)
        self.failUnless(t3.date() == d)
        self.failUnless(t3.time_of_day() == td)
        self.failUnless(t1 == t3)
        d2 = gregorian.date(2001, date_time.Jan, 1)
        t4 = posix_time.ptime(d2, td)
        self.failUnless(not (t1 == t4))
        td1 = posix_time.time_duration(5, 4, 0)
        t5 = posix_time.ptime(d, td1)
        self.failUnless(not (t1 == t5))
        self.failUnless(t1 != t5)

        self.failUnless(not (t1 < t1))
        self.failUnless(t4 < t1)
        self.failUnless(t5 < t1)
        self.failUnless(t1 <= t1)
        self.failUnless(t1 >= t1)

        twodays = gregorian.date_duration(2)
        t6 = t1 + twodays
        d3 = gregorian.date(2001, date_time.Dec, 3)
        self.failUnless(t6 == posix_time.ptime(d3, td))
        t7 = t1 - twodays
        self.failUnless(t7 == posix_time.ptime(
            gregorian.date(2001, date_time.Nov, 29), td))

        t6b = posix_time.ptime(gregorian.date(2003, date_time.Oct, 31),
                               posix_time.time_duration(10, 0, 0, 0))
        t6b += gregorian.date_duration(55)
        self.failUnless(
            t6b == posix_time.ptime(gregorian.date(2003, date_time.Dec, 25),
                                    posix_time.time_duration(10, 0, 0, 0)))
        t6b += posix_time.hours(6)
        self.failUnless(
            t6b == posix_time.ptime(gregorian.date(2003, date_time.Dec, 25),
                                    posix_time.time_duration(16, 0, 0, 0)))
        t6b -= gregorian.date_duration(55)
        self.failUnless(
            t6b == posix_time.ptime(gregorian.date(2003, date_time.Oct, 31),
                                    posix_time.time_duration(16, 0, 0, 0)))
        t6b -= posix_time.hours(6)
        self.failUnless(
            t6b == posix_time.ptime(gregorian.date(2003, date_time.Oct, 31),
                                    posix_time.time_duration(10, 0, 0, 0)))
        t6b += posix_time.hours(25)
        self.failUnless(
            t6b == posix_time.ptime(gregorian.date(2003, date_time.Nov, 1),
                                    posix_time.time_duration(11, 0, 0, 0)))
        t6b -= posix_time.hours(49)
        self.failUnless(
            t6b == posix_time.ptime(gregorian.date(2003, date_time.Oct, 30),
                                    posix_time.time_duration(10, 0, 0, 0)))

        td2 = posix_time.time_duration(1, 2, 3)
        t8 = posix_time.ptime(gregorian.date(2001, date_time.Dec, 1))
        t9 = t8 + td2
        t10 = posix_time.ptime(gregorian.date(2001, date_time.Dec, 1),
                               posix_time.time_duration(1, 2, 3))

        self.failUnless(t9 == t10)
        t9 = posix_time.ptime(gregorian.date(2001, date_time.Dec, 1),
                              posix_time.time_duration(12, 0, 0))
        td3 = posix_time.time_duration(-4, 0, 0)
        self.failUnless(
            t9 +
            td3 == posix_time.ptime(gregorian.date(2001, date_time.Dec, 1),
                                    posix_time.time_duration(8, 0, 0)))
        td3 = posix_time.time_duration(24, 0, 0)
        self.failUnless(
            t8 +
            td3 == posix_time.ptime(gregorian.date(2001, date_time.Dec, 2)))
        td4 = posix_time.time_duration(24, 0, 1)
        self.failUnless(
            t8 +
            td4 == posix_time.ptime(gregorian.date(2001, date_time.Dec, 2),
                                    posix_time.time_duration(0, 0, 1)))

        td5 = posix_time.time_duration(168, 0, 1)
        self.failUnless(
            t8 +
            td5 == posix_time.ptime(gregorian.date(2001, date_time.Dec, 8),
                                    posix_time.time_duration(0, 0, 1)))

        t11 = posix_time.ptime(gregorian.date(2001, date_time.Dec, 1),
                               posix_time.time_duration(12, 0, 0))
        td6 = posix_time.time_duration(12, 0, 1)
        t12 = t11 - td6
        self.failUnless(
            t12 == posix_time.ptime(gregorian.date(2001, date_time.Nov, 30),
                                    posix_time.time_duration(23, 59, 59)))
        self.failUnless(t11 -
                        posix_time.time_duration(13, 0, 0) == posix_time.ptime(
                            gregorian.date(2001, date_time.Nov, 30),
                            posix_time.time_duration(23, 0, 0)))
        self.failUnless(
            t11 - posix_time.time_duration(-13, 0, 0) == posix_time.ptime(
                gregorian.date(2001, date_time.Dec, 2),
                posix_time.time_duration(1, 0, 0)))

        t13 = posix_time.ptime(d, posix_time.hours(3))
        t14 = posix_time.ptime(d, posix_time.hours(4))
        t14a = posix_time.ptime(d + gregorian.date_duration(1),
                                posix_time.hours(4))

        self.failUnless(t14 - t13 == posix_time.hours(1))

        self.failUnless(t13 - t14 == posix_time.hours(-1))
        self.failUnless(t14a - t14 == posix_time.hours(24))

        t15 = posix_time.ptime(d, posix_time.time_duration(0, 0, 0, 1))
        t16 = posix_time.ptime(d, posix_time.time_duration(0, 0, 0, 2))
        self.failUnless(t15 + posix_time.time_duration.unit() == t16)
        self.failUnless(t16 - posix_time.time_duration.unit() == t15)

        self.failUnless(
            posix_time.ptime(date_time.min_date_time) == posix_time.ptime(
                gregorian.date(1400, 1, 1), posix_time.time_duration(
                    0, 0, 0, 0)))

        self.failUnless(
            posix_time.ptime(date_time.max_date_time) == posix_time.ptime(
                gregorian.date(9999, 12, 31),
                posix_time.hours(24) - posix_time.time_duration.unit()))