def test_dst_transition_day_rule(self): rule1 = local_time.partial_date_dst_rule( gregorian.partial_date(30, date_time.Apr) , gregorian.partial_date(30, date_time.Oct)) self.failUnless( rule1.start_day(2001) == gregorian.date(2001, date_time.Apr, 30)) self.failUnless( rule1.end_day(2001) == gregorian.date(2001, date_time.Oct, 30)) rule2 = local_time.first_last_dst_rule( local_time.first_last_dst_rule.start_rule(date_time.Sunday, date_time.Apr) , local_time.first_last_dst_rule.end_rule(date_time.Sunday, date_time.Oct)) self.failUnless( rule2.start_day(2001) == gregorian.date(2001, date_time.Apr, 1)) self.failUnless( rule2.end_day(2001) == gregorian.date(2001, date_time.Oct, 28)) rule3 = local_time.last_last_dst_rule( local_time.last_last_dst_rule.start_rule(date_time.Sunday, date_time.Mar) , local_time.last_last_dst_rule.end_rule(date_time.Sunday, date_time.Oct)) self.failUnless( rule3.start_day(2001) == gregorian.date(2001, date_time.Mar, 25)) self.failUnless( rule3.end_day(2001) == gregorian.date(2001, date_time.Oct, 28) ) nkday = gregorian.nth_kday_of_month rule4 = local_time.nth_last_dst_rule( local_time.nth_last_dst_rule.start_rule(nkday.first, date_time.Sunday, date_time.Mar) , local_time.nth_last_dst_rule.end_rule(date_time.Sunday, date_time.Oct)) self.failUnless( rule4.start_day(2001) == gregorian.date(2001, date_time.Mar, 4)) self.failUnless( rule4.end_day(2001) == gregorian.date(2001, date_time.Oct, 28)) rule5 = local_time.nth_kday_dst_rule( local_time.nth_kday_dst_rule.start_rule(nkday.first, date_time.Sunday, date_time.Mar) , local_time.nth_kday_dst_rule.end_rule(nkday.fourth, date_time.Sunday, date_time.Oct)) self.failUnless( rule5.start_day(2001) == gregorian.date(2001, date_time.Mar, 4)) self.failUnless( rule5.end_day(2001) == gregorian.date(2001, date_time.Oct, 28))
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) ) )
def test_tz_database(self): tz_db = local_time.time_zone_database() tz_db.load_from_file( os.path.join( os.path.split( date_time.__file__ )[0] , 'date_time_zonespec.csv' ) ) self.failUnless( not tz_db.time_zone_from_region("Invalid/name") ) nyc_test = tz_db.time_zone_from_region("America/New_York") self.failUnless( nyc_test ) self.failUnless( nyc_test.std_zone_abbrev() == "EST" ) self.failUnless( nyc_test.std_zone_name() == "Eastern Standard Time" ) self.failUnless( nyc_test.dst_zone_abbrev() == "EDT" ) self.failUnless( nyc_test.dst_zone_name() == "Eastern Daylight Time" ) self.failUnless( nyc_test.base_utc_offset() == posix_time.hours(-5) ) self.failUnless( nyc_test.dst_offset() == posix_time.hours(1) ) self.failUnless( nyc_test.dst_local_start_time(2004) == posix_time.ptime( gregorian.date(2004, date_time.Apr, 4) , posix_time.hours(2) ) ) self.failUnless( nyc_test.dst_local_end_time(2004) == posix_time.ptime( gregorian.date(2004, date_time.Oct, 31) , posix_time.hours(2) ) ) self.failUnless( nyc_test.has_dst() ) phx_test = tz_db.time_zone_from_region("America/Phoenix") self.failUnless( phx_test ) self.failUnless( phx_test.std_zone_abbrev() == "MST" ) self.failUnless( phx_test.std_zone_name() == "Mountain Standard Time" ) self.failUnless( phx_test.dst_zone_abbrev() == "" ) self.failUnless( phx_test.dst_zone_name() == "" ) self.failUnless( phx_test.base_utc_offset() == posix_time.hours(-7) ) self.failUnless( phx_test.dst_offset() == posix_time.hours(0) ) self.failUnless( phx_test.has_dst() == False) eastern = local_time.posix_time_zone("EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00") tz_db.add_record("United States/Eastern", eastern) eastern_test = tz_db.time_zone_from_region("United States/Eastern" ) self.failUnless( eastern_test ) self.failUnless( eastern_test.std_zone_abbrev() == "EST" ) self.failUnless( eastern_test.std_zone_name() == "EST" ) self.failUnless( eastern_test.dst_zone_abbrev() == "EDT" ) self.failUnless( eastern_test.dst_zone_name() == "EDT" ) self.failUnless( eastern_test.base_utc_offset() == posix_time.hours(-5) ) self.failUnless( eastern_test.dst_local_start_time(2004) == posix_time.ptime( gregorian.date(2004, date_time.Apr, 4) , posix_time.hours(2) ) ) self.failUnless( eastern_test.dst_local_end_time(2004) == posix_time.ptime( gregorian.date(2004, date_time.Oct, 31) , posix_time.hours(2) ) ) self.failUnless( eastern_test.has_dst() )
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))))
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))))
def test_period(self): d1 = gregorian.date(2000, date_time.Jan, 1) d2 = gregorian.date(2000, date_time.Jan, 4) p1 = gregorian.date_period(d1, d2) p2 = gregorian.date_period(d1, gregorian.date_duration(3)) self.failUnless(p1 == p2) self.failUnless(p1.begin() == d1) self.failUnless(p1.last() == d2 - gregorian.date_duration(1)) self.failUnless(p1.end() == d2) self.failUnless(p2.length() == gregorian.date_duration(3)) self.failUnless(p1.contains(d1)) self.failUnless(not p1.contains(d2)) p3 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 4), gregorian.date(2000, date_time.Feb, 1)) self.failUnless(not (p1 == p3)) self.failUnless(p1 < p3) self.failUnless(p3 > p1) self.failUnless(p3 != p1) self.failUnless(p1.intersects(p1)) self.failUnless(not p1.intersects(p3)) self.failUnless(not p3.intersects(p1)) p4 = gregorian.date_period(gregorian.date(1999, date_time.Dec, 1), d2) self.failUnless(p1.intersects(p4)) self.failUnless(p4.intersects(p1)) p5 = gregorian.date_period(gregorian.date(1999, date_time.Dec, 1), gregorian.date(2000, date_time.Dec, 31)) self.failUnless(p1.intersects(p5)) self.failUnless(p5.intersects(p1)) p6 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 1), gregorian.date(2000, date_time.Dec, 31)) self.failUnless(p5.contains(p6)) self.failUnless(p6.contains(p6)) self.failUnless(not p6.contains(p5)) fourDays = gregorian.date_duration(4) p1.shift(fourDays) shifted = gregorian.date_period(gregorian.date(2000, date_time.Jan, 5), gregorian.date(2000, date_time.Jan, 8)) self.failUnless(p1 == shifted) i1 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 5), gregorian.date(2000, date_time.Jan, 10)) i2 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 1), gregorian.date(2000, date_time.Jan, 7)) r1 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 5), gregorian.date(2000, date_time.Jan, 7)) self.failUnless(i1.intersection(i2) == r1) self.failUnless(i2.intersection(i1) == r1) i3 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 1), gregorian.date(2000, date_time.Jan, 15)) self.failUnless(i1.intersection(i3) == i1) self.failUnless(i3.intersection(i1) == i1) i4 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 7), gregorian.date(2000, date_time.Jan, 10)) i5 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 6), gregorian.date(2000, date_time.Jan, 9)) self.failUnless(i1.intersection(i5) == i5) self.failUnless(i5.intersection(i1) == i5) self.failUnless(i2.intersection(i4).is_null()) r3 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 1), gregorian.date(2000, date_time.Jan, 10)) self.failUnless(i1.merge(i2) == r3) self.failUnless(i2.merge(i4).is_null()) r4 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 5), gregorian.date(2000, date_time.Jan, 10)) self.failUnless(i1.merge(i5) == r4) self.failUnless(i1.span(i2) == r3) self.failUnless(i2.span(i4) == r3) self.failUnless(i4.span(i2) == r3) self.failUnless(i3.span(i2) == i3) i6 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 1), gregorian.date(2000, date_time.Jan, 2)) self.failUnless(i6.span(i4) == r3) self.failUnless(i4.span(i6) == r3) bf_start = gregorian.date(2000, date_time.Jan, 5) bf_end = gregorian.date(2000, date_time.Jan, 10) bf_before = gregorian.date(2000, date_time.Jan, 4) bf_after = gregorian.date(2000, date_time.Jan, 11) bf_during = gregorian.date(2000, date_time.Jan, 7) bfp1 = gregorian.date_period(bf_start, bf_end) self.failUnless(not bfp1.is_before(bf_start)) self.failUnless(bfp1.is_before(bf_end)) self.failUnless(not bfp1.is_before(bfp1.last())) self.failUnless(not bfp1.is_before(bf_before)) self.failUnless(not bfp1.is_before(bf_during)) self.failUnless(bfp1.is_before(bf_after)) self.failUnless(not bfp1.is_after(bf_start)) self.failUnless(not bfp1.is_after(bf_end)) self.failUnless(not bfp1.is_after(bfp1.last())) self.failUnless(bfp1.is_after(bf_before)) self.failUnless(not bfp1.is_after(bf_during)) self.failUnless(not bfp1.is_after(bf_after)) adj1 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 5), gregorian.date(2000, date_time.Jan, 10)) adj2 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 1), gregorian.date(2000, date_time.Jan, 5)) adj3 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 7), gregorian.date(2000, date_time.Jan, 12)) adj4 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 12), gregorian.date(2000, date_time.Jan, 15)) adj5 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 1), gregorian.date(2000, date_time.Jan, 3)) adj6 = gregorian.date_period(gregorian.date(2000, date_time.Jan, 7), gregorian.date(2000, date_time.Jan, 9)) self.failUnless(adj1.is_adjacent(adj2)) self.failUnless(adj2.is_adjacent(adj1)) self.failUnless(not adj1.is_adjacent(adj3)) self.failUnless(not adj3.is_adjacent(adj1)) self.failUnless(not adj1.is_adjacent(adj4)) self.failUnless(not adj4.is_adjacent(adj1)) self.failUnless(not adj1.is_adjacent(adj5)) self.failUnless(not adj5.is_adjacent(adj1)) self.failUnless(not adj1.is_adjacent(adj6)) self.failUnless(not adj6.is_adjacent(adj1))
def test_date(self): self.failUnless( gregorian.date() == gregorian.date(date_time.not_a_date_time)) d1 = gregorian.date(1900, 1, 1) d2 = gregorian.date(2000, 1, 1) d3 = gregorian.date(1900, 1, 1) d4 = gregorian.date(2000, 12, 31) d4a = gregorian.date(2000, date_time.Dec, 31) self.failUnless(d4 == d4a) self.failUnless(d1.day().as_number() == 1) self.failUnless(d1.month().as_number() == 1) self.failUnless(d1.year().as_number() == 1900) self.failUnless(d4a.month().as_number() == 12) self.failUnless(d1 < d2) self.failUnless(not (d1 < d1)) self.failUnless(d1 <= d1) self.failUnless(d2 > d1) self.failUnless(not (d1 > d1)) self.failUnless(d1 >= d1) self.failUnless(d2 != d1) self.failUnless(d1 == d3) ymd = d1.year_month_day() self.failUnless(ymd.year.as_number() == 1900) self.failUnless(ymd.month.as_number() == 1) self.failUnless(ymd.day.as_number() == 1) answers = [ gregorian.date(1900, date_time.Jan, 1), gregorian.date(1900, date_time.Jan, 4), gregorian.date(1900, date_time.Jan, 7), gregorian.date(1900, date_time.Jan, 10), gregorian.date(1900, date_time.Jan, 13) ] off = gregorian.date_duration(3) d5 = gregorian.date(1900, 1, 1) for i in range(5): self.failUnless(d5 == answers[i]) d5 = d5 + off answers1 = [ gregorian.date(2000, 2, 26), gregorian.date(2000, 2, 28), gregorian.date(2000, date_time.Mar, 1) ] d8 = gregorian.date(2000, date_time.Feb, 26) for i in range(3): self.failUnless(d8 == answers1[i]) d8 += gregorian.days(2) d6 = gregorian.date(2000, 2, 28) d7 = gregorian.date(2000, 3, 1) twoDays = gregorian.date_duration(2) negtwoDays = gregorian.date_duration(-2) zeroDays = gregorian.date_duration(0) self.failUnless(twoDays == d7 - d6) self.failUnless(negtwoDays == d6 - d7) self.failUnless(zeroDays == d6 - d6) self.failUnless(d6 + twoDays == d7) self.failUnless(d7 - twoDays == d6) self.failUnless( gregorian.date(1999, 1, 1) + gregorian.date_duration(365) == gregorian.date(2000, 1, 1)) self.failUnless( gregorian.date(1999, 1, 1) + zeroDays == gregorian.date(1999, 1, 1)) d = gregorian.date(2003, date_time.Oct, 31) dd = gregorian.date_duration(55) d += dd self.failUnless(d == gregorian.date(2003, date_time.Dec, 25)) d -= dd self.failUnless(d == gregorian.date(2003, date_time.Oct, 31)) d = gregorian.date(2003, date_time.Oct, 31) dd1 = gregorian.date_duration(date_time.pos_infin) dd2 = gregorian.date_duration(date_time.neg_infin) dd3 = gregorian.date_duration(date_time.not_a_date_time) self.failUnless(d + dd1 == gregorian.date(date_time.pos_infin)) self.failUnless(d + dd2 == gregorian.date(date_time.neg_infin)) self.failUnless(d + dd3 == gregorian.date(date_time.not_a_date_time)) self.failUnless(d - dd1 == gregorian.date(date_time.neg_infin)) self.failUnless(d - dd2 == gregorian.date(date_time.pos_infin)) self.failUnless(d - dd3 == gregorian.date(date_time.not_a_date_time)) self.failUnless( gregorian.date(date_time.pos_infin) + dd1 == gregorian.date(date_time.pos_infin)) self.failUnless( gregorian.date(date_time.pos_infin) - dd1 == gregorian.date(date_time.not_a_date_time)) self.failUnless( gregorian.date(date_time.neg_infin) + dd1 == gregorian.date(date_time.not_a_date_time)) self.failUnless( gregorian.date(date_time.neg_infin) - dd1 == gregorian.date(date_time.neg_infin)) self.failUnlessRaises(Exception, lambda: gregorian.date(2000, 1, 32)) self.failUnlessRaises(Exception, lambda: gregorian.date(2000, 1, 0)) self.failUnlessRaises(Exception, lambda: gregorian.date(2000, 2, 31)) d15 = gregorian.date(1400, 1, 1) self.failUnless(d15.julian_day() == 2232400) self.failUnless(d15.modjulian_day() == -167601) d16 = gregorian.date(2004, 2, 29) self.failUnless(d16.julian_day() == 2453065) self.failUnless(d16.modjulian_day() == 53064) d31 = gregorian.date(2000, date_time.Jun, 1) self.failUnless(d31.week_number() == 22) d38 = gregorian.date(2001, 1, 1) self.failUnless(d38.day_of_year().as_number() == 1)
def test_greg_durations(self): m1 = gregorian.months(5) m2 = gregorian.months(3) m3 = gregorian.months(1) self.failUnless(gregorian.months(8) == m1 + m2) m1 += m2 self.failUnless(gregorian.months(8) == m1) self.failUnless(gregorian.months(-5) == m2 - m1) m2 -= m1 self.failUnless(gregorian.months(-5) == m2) d1 = gregorian.date(2005, date_time.Jan, 1) self.failUnless( d1 + gregorian.months(-1) == gregorian.date(2004, date_time.Dec, 1)) d3 = gregorian.date(2005, date_time.Dec, 1) self.failUnless( d3 - gregorian.months(-1) == gregorian.date(2006, date_time.Jan, 1)) m1 = gregorian.months(5) m2 = gregorian.months(3) m3 = gregorian.months(10) self.failUnless(gregorian.months(15) == m1 * 3) m1 *= 3 self.failUnless(gregorian.months(15) == m1) self.failUnless(gregorian.months(12) == m2 * 4) self.failUnless(gregorian.months(3) == m3 / 3) m3 /= 3 self.failUnless(gregorian.months(3) == m3) m = gregorian.months(-5) m_pos = gregorian.months(date_time.pos_infin) m_neg = gregorian.months(date_time.neg_infin) m_nadt = gregorian.months(date_time.not_a_date_time) self.failUnless(m + m_pos == m_pos) self.failUnless(m + m_neg == m_neg) self.failUnless(m_pos + m_neg == m_nadt) self.failUnless(m_neg + m_neg == m_neg) self.failUnless(m - m_pos == m_neg) self.failUnless(m - m_neg == m_pos) self.failUnless(m_pos - m_neg == m_pos) self.failUnless(m_pos * -1 == m_neg) self.failUnless(m_pos * 0 == m_nadt) self.failUnless(m_neg / 3 == m_neg) y1 = gregorian.years(2) y2 = gregorian.years(4) self.failUnless(gregorian.months(27) == m3 + y1) m3 += y1 self.failUnless(gregorian.months(27) == m3) self.failUnless(gregorian.months(-21) == m3 - y2) m3 -= y2 self.failUnless(gregorian.months(-21) == m3) d = gregorian.date(2001, date_time.Oct, 31) self.failUnless( gregorian.date(2002, date_time.Feb, 28) == d + gregorian.months(4)) d += gregorian.months(4) self.failUnless(gregorian.date(2002, date_time.Feb, 28) == d) d = gregorian.date(2001, date_time.Oct, 31) self.failUnless( gregorian.date(2001, date_time.Apr, 30) == d - gregorian.months(6)) d -= gregorian.months(6) self.failUnless(gregorian.date(2001, date_time.Apr, 30) == d) y1 = gregorian.years(2) y2 = gregorian.years(4) y3 = gregorian.years(1) self.failUnless(gregorian.years(3) == y3 + y1) y3 += y1 self.failUnless(gregorian.years(3) == y3) self.failUnless(gregorian.years(-1) == y3 - y2) y3 -= y2 self.failUnless(gregorian.years(-1) == y3) y1 = gregorian.years(5) y2 = gregorian.years(3) y3 = gregorian.years(10) self.failUnless(gregorian.years(15) == y1 * 3) y1 *= 3 self.failUnless(gregorian.years(15) == y1) self.failUnless(gregorian.years(3) == y3 / 3) y3 /= 3 self.failUnless(gregorian.years(3) == y3) m = gregorian.years(15) y_pos = gregorian.years(date_time.pos_infin) y_neg = gregorian.years(date_time.neg_infin) y_nadt = gregorian.years(date_time.not_a_date_time) self.failUnless(m + y_pos == y_pos) self.failUnless(m + y_neg == y_neg) self.failUnless(y_pos + y_neg == y_nadt) self.failUnless(y_neg + y_neg == y_neg) self.failUnless(m - y_pos == y_neg) self.failUnless(m - y_neg == y_pos) self.failUnless(y_pos - y_neg == y_pos) self.failUnless(y_pos * -1 == y_neg) self.failUnless(y_pos * 0 == y_nadt) self.failUnless(y_neg / 3 == y_neg) m1 = gregorian.months(5) m2 = gregorian.months(3) self.failUnless(gregorian.months(39) == y2 + m2) self.failUnless(gregorian.months(31) == y2 - m1) d = gregorian.date(2001, date_time.Feb, 28) self.failUnless( gregorian.date(2004, date_time.Feb, 29) == d + gregorian.years(3)) d += gregorian.years(3) self.failUnless(gregorian.date(2004, date_time.Feb, 29) == d) d = gregorian.date(2000, date_time.Feb, 29) self.failUnless( gregorian.date(1994, date_time.Feb, 28) == d - gregorian.years(6)) d -= gregorian.years(6) self.failUnless(gregorian.date(1994, date_time.Feb, 28) == d) w1 = gregorian.weeks(2) w2 = gregorian.weeks(4) w3 = gregorian.weeks(1) pi = gregorian.weeks(date_time.pos_infin) self.failUnless(gregorian.weeks(date_time.pos_infin) == w1 + pi) self.failUnless(gregorian.weeks(3) == w3 + w1) w3 += w1 self.failUnless(gregorian.weeks(3) == w3) self.failUnless(gregorian.weeks(-1) == w3 - w2) w3 -= w2 self.failUnless(gregorian.weeks(-1) == w3) d = gregorian.days(10) self.failUnless(gregorian.days(31) == d + gregorian.weeks(3)) d += gregorian.weeks(3) self.failUnless(gregorian.days(31) == d) d = gregorian.days(10) self.failUnless(gregorian.days(-32) == d - gregorian.weeks(6)) d -= gregorian.weeks(6) self.failUnless(gregorian.days(-32) == d) d = gregorian.date(2001, date_time.Feb, 28) self.failUnless( gregorian.date(2001, date_time.Mar, 21) == d + gregorian.weeks(3)) d += gregorian.weeks(3) self.failUnless(gregorian.date(2001, date_time.Mar, 21) == d) d = gregorian.date(2001, date_time.Feb, 28) self.failUnless( gregorian.date(2001, date_time.Jan, 17) == d - gregorian.weeks(6)) d -= gregorian.weeks(6) self.failUnless(gregorian.date(2001, date_time.Jan, 17) == d) d = gregorian.date(2000, date_time.Oct, 31) d2 = d + gregorian.months(4) + gregorian.years(2) d3 = d + gregorian.years(2) + gregorian.months(4) self.failUnless(gregorian.date(2003, date_time.Feb, 28) == d2) self.failUnless(gregorian.date(2003, date_time.Feb, 28) == d3) m = gregorian.years(2) + gregorian.months(4) - gregorian.months( 4) - gregorian.years(2) self.failUnless(m.number_of_months() == 0)
def test_generators(self): pd1 = gregorian.partial_date(1, date_time.Jan) d = pd1.get_date(2000) self.failUnless(pd1.to_string() == "0") self.failUnless(gregorian.date(2000, 1, 1) == d) pd2 = gregorian.partial_date(1, date_time.Feb) self.failUnless(pd1 == pd1) self.failUnless(not (pd1 == pd2)) self.failUnless(not (pd2 == pd1)) self.failUnless(not (pd1 < pd1)) self.failUnless(pd1 < pd2) self.failUnless(not (pd2 < pd1)) lastkday = gregorian.last_day_of_the_week_in_month lsif = lastkday(date_time.Sunday, date_time.Feb) self.failUnless( gregorian.date(2002, date_time.Feb, 24) == lsif.get_date(2002)) self.failUnless(lsif.to_string() == "M2.5.0") firstkday = gregorian.first_day_of_the_week_in_month fsif = firstkday(date_time.Sunday, date_time.Feb) self.failUnless( gregorian.date(2002, date_time.Feb, 3) == fsif.get_date(2002)) self.failUnless(fsif.to_string() == "M2.1.0") firstkdayafter = gregorian.first_day_of_the_week_after fkaf = firstkdayafter(date_time.Monday) self.failUnless( gregorian.date(2002, date_time.Feb, 4) == fkaf.get_date( gregorian.date(2002, date_time.Feb, 1))) self.failUnless('2002-Feb-04' == to_simple_string( fkaf.get_date(gregorian.date(2002, date_time.Feb, 1)))) firstkdaybefore = gregorian.first_day_of_the_week_before fkbf = firstkdaybefore(date_time.Monday) self.failUnless('2002-Feb-11' == to_simple_string( fkaf.get_date(gregorian.date(2002, date_time.Feb, 10)))) self.failUnless( gregorian.date(2002, date_time.Feb, 4) == fkbf.get_date( gregorian.date(2002, date_time.Feb, 10))) nthkdayofmonth = gregorian.nth_day_of_the_week_in_month nkd1 = nthkdayofmonth(nthkdayofmonth.third, date_time.Sunday, date_time.Jul) self.failUnless( gregorian.date(1969, date_time.Jul, 20) == nkd1.get_date(1969)) self.failUnless(nkd1.to_string() == "M7.3.0") saturday = gregorian.date(2003, date_time.Feb, 8) sund = gregorian.greg_weekday(date_time.Sunday) self.failUnless( gregorian.days_until_weekday(saturday, sund) == gregorian.days(1)) self.failUnless( gregorian.days_before_weekday(saturday, sund) == gregorian.days(6)) self.failUnless( gregorian.next_weekday(saturday, sund) == gregorian.date( 2003, date_time.Feb, 9)) self.failUnless( gregorian.previous_weekday(saturday, sund) == gregorian.date( 2003, date_time.Feb, 2))
def test_custom_time_zone( self ): rule1 = local_time.partial_date_dst_rule( gregorian.partial_date(30, date_time.Apr) , gregorian.partial_date(30, date_time.Oct)) rule2 = local_time.first_last_dst_rule( local_time.first_last_dst_rule.start_rule( date_time.Sunday, date_time.Apr) , local_time.first_last_dst_rule.end_rule( date_time.Sunday, date_time.Oct)) rule3 = local_time.last_last_dst_rule( local_time.last_last_dst_rule.start_rule(date_time.Sunday, date_time.Mar) , local_time.last_last_dst_rule.end_rule(date_time.Sunday, date_time.Oct)) rule4 = None pst = local_time.time_zone_names( "Pacific Standard Time" , "PST" , "Pacific Daylight Time" , "PDT" ) mst = local_time.time_zone_names( "Mountain Standard Time" , "MST" , "" , "" ) of = local_time.dst_adjustment_offsets( posix_time.hours(1) , posix_time.hours(2) , posix_time.hours(2)) of2 = local_time.dst_adjustment_offsets( posix_time.hours(0) , posix_time.hours(0) , posix_time.hours(0)) tz1 = local_time.custom_time_zone(pst, posix_time.hours(-8), of, rule1) tz2 = local_time.custom_time_zone(pst, posix_time.hours(-8), of, rule2) tz3 = local_time.custom_time_zone(pst, posix_time.hours(-8), of, rule3) tz4 = local_time.custom_time_zone(mst, posix_time.hours(-7), of2, rule4) self.failUnless( tz1.dst_zone_abbrev() == "PDT" ) self.failUnless( tz1.std_zone_abbrev() == "PST" ) self.failUnless( tz1.std_zone_name() == "Pacific Standard Time" ) self.failUnless( tz1.dst_zone_name() == "Pacific Daylight Time" ) self.failUnless( tz1.dst_offset() == posix_time.hours(1) ) self.failUnless( tz1.base_utc_offset() == posix_time.hours(-8) ) self.failUnless( tz1.has_dst() ) self.failUnless( tz1.dst_local_start_time(2003) == posix_time.ptime( gregorian.date(2003, date_time.Apr,30) , posix_time.hours(2) ) ) self.failUnless( tz1.dst_local_end_time(2003) == posix_time.ptime( gregorian.date(2003, date_time.Oct,30) , posix_time.hours(2) ) ) self.failUnless( tz1.to_posix_string() == "PST-08PDT+01,120/02:00,303/02:00") self.failUnless( tz2.to_posix_string() == "PST-08PDT+01,M4.1.0/02:00,M10.5.0/02:00") self.failUnless( tz3.to_posix_string() == "PST-08PDT+01,M3.5.0/02:00,M10.5.0/02:00") self.failUnless( tz4.to_posix_string() == "MST-07") self.failUnless( not tz4.has_dst() ) self.failUnless( tz4.dst_local_start_time(2005) == posix_time.ptime( date_time.not_a_date_time ) ) self.failUnless( tz4.dst_local_end_time(2005) == posix_time.ptime( date_time.not_a_date_time ) )
def test_greg_durations(self): m1 = gregorian.months(5) m2 = gregorian.months(3) m3 = gregorian.months(1) self.failUnless( gregorian.months(8) == m1 + m2) m1 += m2 self.failUnless( gregorian.months(8) == m1) self.failUnless( gregorian.months(-5) == m2 - m1) m2 -= m1; self.failUnless( gregorian.months(-5) == m2) d1 = gregorian.date(2005, date_time.Jan, 1) self.failUnless( d1 + gregorian.months(-1) == gregorian.date(2004, date_time.Dec, 1) ) d3 = gregorian.date(2005, date_time.Dec, 1); self.failUnless( d3 - gregorian.months(-1) == gregorian.date(2006,date_time.Jan,1)) m1 = gregorian.months(5) m2 = gregorian.months(3) m3 = gregorian.months(10) self.failUnless( gregorian.months(15) == m1 * 3) m1 *= 3 self.failUnless( gregorian.months(15) == m1) self.failUnless( gregorian.months(12) == m2 * 4) self.failUnless( gregorian.months(3) == m3 / 3) m3 /= 3 self.failUnless( gregorian.months(3) == m3) m = gregorian.months(-5) m_pos = gregorian.months(date_time.pos_infin) m_neg = gregorian.months(date_time.neg_infin) m_nadt = gregorian.months(date_time.not_a_date_time) self.failUnless( m + m_pos == m_pos) self.failUnless( m + m_neg == m_neg) self.failUnless( m_pos + m_neg == m_nadt) self.failUnless( m_neg + m_neg == m_neg) self.failUnless( m - m_pos == m_neg) self.failUnless( m - m_neg == m_pos) self.failUnless( m_pos - m_neg == m_pos) self.failUnless( m_pos * -1 == m_neg) self.failUnless( m_pos * 0 == m_nadt) self.failUnless( m_neg / 3 == m_neg) y1 = gregorian.years(2) y2 = gregorian.years(4) self.failUnless( gregorian.months(27) == m3 + y1) m3 += y1 self.failUnless( gregorian.months(27) == m3) self.failUnless( gregorian.months(-21) == m3 - y2) m3 -= y2 self.failUnless( gregorian.months(-21) == m3) d = gregorian.date(2001, date_time.Oct, 31); self.failUnless( gregorian.date(2002, date_time.Feb, 28) == d + gregorian.months(4)) d += gregorian.months(4) self.failUnless( gregorian.date(2002, date_time.Feb, 28) == d); d = gregorian.date(2001, date_time.Oct, 31); self.failUnless( gregorian.date(2001, date_time.Apr, 30) == d - gregorian.months(6)); d -= gregorian.months(6); self.failUnless( gregorian.date(2001, date_time.Apr, 30) == d); y1 = gregorian.years(2) y2 = gregorian.years(4) y3 = gregorian.years(1) self.failUnless( gregorian.years(3) == y3 + y1) y3 += y1 self.failUnless( gregorian.years(3) == y3) self.failUnless( gregorian.years(-1) == y3 - y2) y3 -= y2 self.failUnless( gregorian.years(-1) == y3) y1 = gregorian.years(5) y2 = gregorian.years(3) y3 = gregorian.years(10) self.failUnless( gregorian.years(15) == y1 * 3) y1 *= 3 self.failUnless( gregorian.years(15) == y1) self.failUnless( gregorian.years(3) == y3 / 3) y3 /= 3 self.failUnless( gregorian.years(3) == y3) m = gregorian.years(15) y_pos = gregorian.years(date_time.pos_infin) y_neg = gregorian.years(date_time.neg_infin) y_nadt = gregorian.years(date_time.not_a_date_time) self.failUnless( m + y_pos == y_pos) self.failUnless( m + y_neg == y_neg) self.failUnless( y_pos + y_neg == y_nadt) self.failUnless( y_neg + y_neg == y_neg) self.failUnless( m - y_pos == y_neg) self.failUnless( m - y_neg == y_pos) self.failUnless( y_pos - y_neg == y_pos) self.failUnless( y_pos * -1 == y_neg) self.failUnless( y_pos * 0 == y_nadt) self.failUnless( y_neg / 3 == y_neg) m1 = gregorian.months(5) m2 = gregorian.months(3) self.failUnless( gregorian.months(39) == y2 + m2) self.failUnless( gregorian.months(31) == y2 - m1) d = gregorian.date(2001, date_time.Feb, 28) self.failUnless( gregorian.date(2004, date_time.Feb, 29) == d + gregorian.years(3)) d += gregorian.years(3) self.failUnless( gregorian.date(2004, date_time.Feb, 29) == d) d = gregorian.date(2000, date_time.Feb, 29) self.failUnless( gregorian.date(1994, date_time.Feb, 28) == d - gregorian.years(6)) d -= gregorian.years(6) self.failUnless( gregorian.date(1994, date_time.Feb, 28) == d) w1 = gregorian.weeks(2) w2 = gregorian.weeks(4) w3 = gregorian.weeks(1) pi = gregorian.weeks(date_time.pos_infin) self.failUnless( gregorian.weeks(date_time.pos_infin) == w1 + pi) self.failUnless( gregorian.weeks(3) == w3 + w1) w3 += w1 self.failUnless( gregorian.weeks(3) == w3) self.failUnless( gregorian.weeks(-1) == w3 - w2) w3 -= w2 self.failUnless( gregorian.weeks(-1) == w3) d = gregorian.days(10) self.failUnless( gregorian.days(31) == d + gregorian.weeks(3)) d += gregorian.weeks(3); self.failUnless( gregorian.days(31) == d) d = gregorian.days(10) self.failUnless( gregorian.days(-32) == d - gregorian.weeks(6)) d -= gregorian.weeks(6) self.failUnless( gregorian.days(-32) == d) d = gregorian.date(2001, date_time.Feb, 28) self.failUnless( gregorian.date(2001, date_time.Mar, 21) == d + gregorian.weeks(3)) d += gregorian.weeks(3) self.failUnless( gregorian.date(2001, date_time.Mar, 21) == d) d = gregorian.date(2001, date_time.Feb, 28); self.failUnless( gregorian.date(2001, date_time.Jan, 17) == d - gregorian.weeks(6)) d -= gregorian.weeks(6) self.failUnless( gregorian.date(2001, date_time.Jan, 17) == d) d = gregorian.date(2000, date_time.Oct, 31) d2 = d + gregorian.months(4) + gregorian.years(2) d3 = d + gregorian.years(2) + gregorian.months(4) self.failUnless( gregorian.date(2003, date_time.Feb,28) == d2) self.failUnless( gregorian.date(2003,date_time.Feb,28) == d3) m = gregorian.years(2) + gregorian.months(4) - gregorian.months(4) - gregorian.years(2) self.failUnless( m.number_of_months() == 0)
import os from pyboost import date_time from pyboost.date_time import gregorian from pyboost.date_time import posix_time from pyboost.date_time import local_time from pyboost.date_time import to_simple_string # Date programming weekstart = gregorian.date(2002, date_time.Feb, 1) print "weekstart: ", to_simple_string(weekstart) weekend = weekstart + gregorian.weeks(1) print "weekend: ", to_simple_string(weekend) today = gregorian.day_clock.local_day() d2 = weekstart + gregorian.days(5) if d2 >= today: # comparison operator pass thisWeek = gregorian.date_period(today, d2) if thisWeek.contains(today): pass # date generator functions d5 = gregorian.next_weekday(today, date_time.Sunday) # 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)
def test_greg_duration_operators(self): p = posix_time.ptime( gregorian.date(2001, date_time.Oct, 31), posix_time.hours(5)) self.failUnless( posix_time.ptime( gregorian.date(2002, date_time.Feb, 28) , posix_time.hours(5)) == p + gregorian.months(4) ) p += gregorian.months(4) self.failUnless( posix_time.ptime(gregorian.date(2002, date_time.Feb, 28), posix_time.hours(5)) == p) p = posix_time.ptime( gregorian.date(2001, date_time.Oct, 31), posix_time.hours(5)) self.failUnless( posix_time.ptime(gregorian.date(2001, date_time.Apr, 30), posix_time.hours(5)) == p - gregorian.months(6)) p -= gregorian.months(6) self.failUnless( posix_time.ptime(gregorian.date(2001, date_time.Apr, 30) , posix_time.hours(5)) == p) p = posix_time.ptime(gregorian.date(2001, date_time.Feb, 28), posix_time.hours(5)) self.failUnless( posix_time.ptime( gregorian.date(2004, date_time.Feb, 29) , posix_time.hours(5)) == p + gregorian.years(3)) p += gregorian.years(3) self.failUnless( posix_time.ptime( gregorian.date(2004, date_time.Feb, 29) , posix_time.hours(5)) == p ) p = posix_time.ptime(gregorian.date(2000, date_time.Feb, 29), posix_time.hours(5)) self.failUnless( posix_time.ptime(gregorian.date(1998, date_time.Feb, 28), posix_time.hours(5)) == p - gregorian.years(2)) p -= gregorian.years(2) self.failUnless( posix_time.ptime(gregorian.date(1998, date_time.Feb, 28), posix_time.hours(5)) == p)
def test_date(self): self.failUnless( gregorian.date() == gregorian.date( date_time.not_a_date_time ) ) d1 = gregorian.date( 1900, 1, 1 ) d2 = gregorian.date( 2000, 1, 1 ) d3 = gregorian.date( 1900, 1, 1 ) d4 = gregorian.date( 2000, 12, 31 ) d4a = gregorian.date( 2000, date_time.Dec, 31 ) self.failUnless( d4 == d4a ) self.failUnless( d1.day().as_number() == 1 ) self.failUnless( d1.month().as_number() == 1 ) self.failUnless( d1.year().as_number() == 1900 ) self.failUnless( d4a.month().as_number() == 12 ) self.failUnless( d1 < d2 ) self.failUnless( not ( d1 < d1 ) ) self.failUnless( d1 <= d1 ) self.failUnless( d2 > d1 ) self.failUnless( not ( d1 > d1 ) ) self.failUnless( d1 >= d1 ) self.failUnless( d2 != d1 ) self.failUnless( d1 == d3 ) ymd = d1.year_month_day() self.failUnless( ymd.year.as_number() == 1900 ) self.failUnless( ymd.month.as_number() == 1 ) self.failUnless( ymd.day.as_number() == 1 ) answers = [ gregorian.date( 1900, date_time.Jan, 1 ) , gregorian.date( 1900, date_time.Jan, 4 ) , gregorian.date( 1900, date_time.Jan, 7 ) , gregorian.date( 1900, date_time.Jan, 10 ) , gregorian.date( 1900, date_time.Jan, 13 ) ] off = gregorian.date_duration( 3 ) d5 = gregorian.date( 1900, 1, 1 ) for i in range(5): self.failUnless( d5 == answers[i] ) d5 = d5 + off answers1 = [ gregorian.date(2000, 2 ,26) , gregorian.date(2000, 2 ,28) , gregorian.date(2000, date_time.Mar ,1 )] d8 = gregorian.date(2000, date_time.Feb, 26) for i in range(3): self.failUnless( d8 == answers1[i] ) d8 += gregorian.days(2) d6 = gregorian.date(2000,2,28) d7 = gregorian.date(2000,3,1) twoDays = gregorian.date_duration(2) negtwoDays = gregorian.date_duration(-2) zeroDays = gregorian.date_duration(0) self.failUnless( twoDays == d7 - d6 ) self.failUnless( negtwoDays == d6 - d7 ) self.failUnless( zeroDays == d6 - d6 ) self.failUnless( d6 + twoDays == d7 ) self.failUnless( d7 - twoDays == d6 ) self.failUnless( gregorian.date(1999,1,1) + gregorian.date_duration(365) == gregorian.date(2000,1,1) ) self.failUnless( gregorian.date(1999,1,1) + zeroDays == gregorian.date(1999,1,1) ) d = gregorian.date(2003, date_time.Oct, 31) dd = gregorian.date_duration(55) d += dd self.failUnless( d == gregorian.date(2003, date_time.Dec,25) ) d -= dd; self.failUnless( d == gregorian.date(2003, date_time.Oct,31) ) d = gregorian.date(2003,date_time.Oct, 31) dd1 = gregorian.date_duration( date_time.pos_infin) dd2 = gregorian.date_duration( date_time.neg_infin) dd3 = gregorian.date_duration( date_time.not_a_date_time) self.failUnless( d + dd1 == gregorian.date(date_time.pos_infin)) self.failUnless( d + dd2 == gregorian.date(date_time.neg_infin)) self.failUnless( d + dd3 == gregorian.date(date_time.not_a_date_time)) self.failUnless( d - dd1 == gregorian.date(date_time.neg_infin)) self.failUnless( d - dd2 == gregorian.date(date_time.pos_infin)) self.failUnless( d - dd3 == gregorian.date(date_time.not_a_date_time)) self.failUnless( gregorian.date(date_time.pos_infin) + dd1 == gregorian.date(date_time.pos_infin)) self.failUnless( gregorian.date(date_time.pos_infin) - dd1 == gregorian.date(date_time.not_a_date_time)) self.failUnless( gregorian.date(date_time.neg_infin) + dd1 == gregorian.date(date_time.not_a_date_time)) self.failUnless( gregorian.date(date_time.neg_infin) - dd1 == gregorian.date(date_time.neg_infin)) self.failUnlessRaises( Exception, lambda: gregorian.date( 2000, 1, 32 ) ) self.failUnlessRaises( Exception, lambda: gregorian.date( 2000, 1, 0 ) ) self.failUnlessRaises( Exception, lambda: gregorian.date( 2000, 2, 31 ) ) d15 = gregorian.date(1400,1,1) self.failUnless( d15.julian_day() == 2232400) self.failUnless( d15.modjulian_day() == -167601) d16 = gregorian.date(2004,2,29) self.failUnless( d16.julian_day() == 2453065) self.failUnless( d16.modjulian_day() == 53064) d31 = gregorian.date(2000, date_time.Jun, 1) self.failUnless( d31.week_number() == 22) d38 = gregorian.date(2001, 1, 1) self.failUnless( d38.day_of_year().as_number() == 1)
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()
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()))
def test_greg_duration_operators(self): p = posix_time.ptime(gregorian.date(2001, date_time.Oct, 31), posix_time.hours(5)) self.failUnless( posix_time.ptime(gregorian.date(2002, date_time.Feb, 28), posix_time.hours(5)) == p + gregorian.months(4)) p += gregorian.months(4) self.failUnless( posix_time.ptime(gregorian.date(2002, date_time.Feb, 28), posix_time.hours(5)) == p) p = posix_time.ptime(gregorian.date(2001, date_time.Oct, 31), posix_time.hours(5)) self.failUnless( posix_time.ptime(gregorian.date(2001, date_time.Apr, 30), posix_time.hours(5)) == p - gregorian.months(6)) p -= gregorian.months(6) self.failUnless( posix_time.ptime(gregorian.date(2001, date_time.Apr, 30), posix_time.hours(5)) == p) p = posix_time.ptime(gregorian.date(2001, date_time.Feb, 28), posix_time.hours(5)) self.failUnless( posix_time.ptime(gregorian.date(2004, date_time.Feb, 29), posix_time.hours(5)) == p + gregorian.years(3)) p += gregorian.years(3) self.failUnless( posix_time.ptime(gregorian.date(2004, date_time.Feb, 29), posix_time.hours(5)) == p) p = posix_time.ptime(gregorian.date(2000, date_time.Feb, 29), posix_time.hours(5)) self.failUnless( posix_time.ptime(gregorian.date(1998, date_time.Feb, 28), posix_time.hours(5)) == p - gregorian.years(2)) p -= gregorian.years(2) self.failUnless( posix_time.ptime(gregorian.date(1998, date_time.Feb, 28), posix_time.hours(5)) == p)
def test_period(self): d1 = gregorian.date(2000,date_time.Jan,1) d2 = gregorian.date(2000,date_time.Jan,4) p1 = gregorian.date_period(d1,d2) p2 = gregorian.date_period(d1,gregorian.date_duration(3) ) self.failUnless( p1 == p2) self.failUnless( p1.begin() == d1) self.failUnless( p1.last() == d2-gregorian.date_duration(1) ) self.failUnless( p1.end() == d2) self.failUnless( p2.length() == gregorian.date_duration(3) ) self.failUnless( p1.contains(d1) ) self.failUnless( not p1.contains(d2) ) p3 = gregorian.date_period( gregorian.date(2000,date_time.Jan,4) , gregorian.date(2000,date_time.Feb,1) ) self.failUnless( not (p1 == p3) ) self.failUnless( p1 < p3) self.failUnless( p3 > p1) self.failUnless( p3 != p1) self.failUnless( p1.intersects(p1) ) self.failUnless( not p1.intersects(p3) ) self.failUnless( not p3.intersects(p1) ) p4 = gregorian.date_period(gregorian.date(1999,date_time.Dec,1), d2) self.failUnless( p1.intersects(p4) ) self.failUnless( p4.intersects(p1) ) p5 = gregorian.date_period(gregorian.date(1999,date_time.Dec,1) , gregorian.date(2000,date_time.Dec,31) ) self.failUnless( p1.intersects(p5) ) self.failUnless( p5.intersects(p1) ) p6 = gregorian.date_period(gregorian.date(2000,date_time.Jan,1) ,gregorian.date(2000,date_time.Dec,31) ) self.failUnless( p5.contains(p6) ) self.failUnless( p6.contains(p6) ) self.failUnless( not p6.contains(p5) ) fourDays = gregorian.date_duration(4) p1.shift(fourDays) shifted = gregorian.date_period(gregorian.date(2000,date_time.Jan,5) , gregorian.date(2000,date_time.Jan,8) ) self.failUnless( p1 == shifted) i1 = gregorian.date_period(gregorian.date(2000,date_time.Jan,5) , gregorian.date(2000,date_time.Jan,10) ) i2 = gregorian.date_period(gregorian.date(2000,date_time.Jan,1) , gregorian.date(2000,date_time.Jan,7) ) r1 = gregorian.date_period(gregorian.date(2000,date_time.Jan,5) , gregorian.date(2000,date_time.Jan,7) ) self.failUnless( i1.intersection(i2) == r1) self.failUnless( i2.intersection(i1) == r1) i3 = gregorian.date_period(gregorian.date(2000,date_time.Jan,1) , gregorian.date(2000,date_time.Jan,15) ) self.failUnless( i1.intersection(i3) == i1) self.failUnless( i3.intersection(i1) == i1) i4 = gregorian.date_period(gregorian.date(2000,date_time.Jan,7), gregorian.date(2000,date_time.Jan,10) ) i5 = gregorian.date_period(gregorian.date(2000,date_time.Jan,6) , gregorian.date(2000,date_time.Jan,9) ) self.failUnless( i1.intersection(i5) == i5) self.failUnless( i5.intersection(i1) == i5) self.failUnless( i2.intersection(i4).is_null() ) r3 = gregorian.date_period(gregorian.date(2000,date_time.Jan,1), gregorian.date(2000,date_time.Jan,10) ) self.failUnless( i1.merge(i2) == r3) self.failUnless( i2.merge(i4).is_null() ) r4 = gregorian.date_period(gregorian.date(2000,date_time.Jan,5), gregorian.date(2000,date_time.Jan,10) ) self.failUnless( i1.merge(i5) == r4) self.failUnless( i1.span(i2) == r3) self.failUnless( i2.span(i4) == r3) self.failUnless( i4.span(i2) == r3) self.failUnless( i3.span(i2) == i3) i6 = gregorian.date_period(gregorian.date(2000,date_time.Jan,1), gregorian.date(2000,date_time.Jan,2) ) self.failUnless( i6.span(i4) == r3) self.failUnless( i4.span(i6) == r3) bf_start = gregorian.date(2000,date_time.Jan,5) bf_end = gregorian.date(2000,date_time.Jan,10) bf_before = gregorian.date(2000,date_time.Jan,4) bf_after = gregorian.date(2000,date_time.Jan,11) bf_during = gregorian.date(2000, date_time.Jan, 7) bfp1 = gregorian.date_period(bf_start, bf_end) self.failUnless( not bfp1.is_before(bf_start) ) self.failUnless( bfp1.is_before(bf_end) ) self.failUnless( not bfp1.is_before(bfp1.last() )) self.failUnless( not bfp1.is_before(bf_before) ) self.failUnless( not bfp1.is_before(bf_during) ) self.failUnless( bfp1.is_before(bf_after) ) self.failUnless( not bfp1.is_after(bf_start) ) self.failUnless( not bfp1.is_after(bf_end) ) self.failUnless( not bfp1.is_after(bfp1.last() )) self.failUnless( bfp1.is_after(bf_before) ) self.failUnless( not bfp1.is_after(bf_during) ) self.failUnless( not bfp1.is_after(bf_after) ) adj1 = gregorian.date_period( gregorian.date(2000,date_time.Jan,5) , gregorian.date(2000,date_time.Jan,10) ) adj2 = gregorian.date_period (gregorian.date(2000,date_time.Jan,1), gregorian.date(2000,date_time.Jan,5) ) adj3 = gregorian.date_period(gregorian.date(2000,date_time.Jan,7), gregorian.date(2000,date_time.Jan,12) ) adj4 = gregorian.date_period(gregorian.date(2000,date_time.Jan,12), gregorian.date(2000,date_time.Jan,15) ) adj5 = gregorian.date_period(gregorian.date(2000,date_time.Jan,1), gregorian.date(2000,date_time.Jan,3) ) adj6 = gregorian.date_period(gregorian.date(2000,date_time.Jan,7), gregorian.date(2000,date_time.Jan,9) ) self.failUnless( adj1.is_adjacent(adj2) ) self.failUnless( adj2.is_adjacent(adj1) ) self.failUnless( not adj1.is_adjacent(adj3) ) self.failUnless( not adj3.is_adjacent(adj1) ) self.failUnless( not adj1.is_adjacent(adj4) ) self.failUnless( not adj4.is_adjacent(adj1) ) self.failUnless( not adj1.is_adjacent(adj5) ) self.failUnless( not adj5.is_adjacent(adj1) ) self.failUnless( not adj1.is_adjacent(adj6) ) self.failUnless( not adj6.is_adjacent(adj1) )
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()))
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) ) )
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()
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")
import os from pyboost import date_time from pyboost.date_time import gregorian from pyboost.date_time import posix_time from pyboost.date_time import local_time from pyboost.date_time import to_simple_string #Date programming weekstart = gregorian.date(2002, date_time.Feb, 1) print 'weekstart: ', to_simple_string(weekstart) weekend = weekstart + gregorian.weeks(1) print 'weekend: ', to_simple_string(weekend) today = gregorian.day_clock.local_day() d2 = weekstart + gregorian.days(5) if d2 >= today: #comparison operator pass thisWeek = gregorian.date_period(today, d2) if thisWeek.contains(today): pass #date generator functions d5 = gregorian.next_weekday(today, date_time.Sunday) #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,
def test_generators(self): pd1 =gregorian.partial_date(1, date_time.Jan) d = pd1.get_date(2000) self.failUnless( pd1.to_string() == "0" ) self.failUnless( gregorian.date(2000,1,1) == d) pd2 = gregorian.partial_date(1, date_time.Feb) self.failUnless( pd1 == pd1) self.failUnless( not(pd1 == pd2) ) self.failUnless( not(pd2 == pd1) ) self.failUnless( not(pd1 < pd1) ) self.failUnless( pd1 < pd2) self.failUnless( not(pd2 < pd1) ) lastkday = gregorian.last_day_of_the_week_in_month lsif = lastkday( date_time.Sunday, date_time.Feb) self.failUnless( gregorian.date(2002, date_time.Feb,24) == lsif.get_date(2002)) self.failUnless( lsif.to_string() == "M2.5.0" ) firstkday = gregorian.first_day_of_the_week_in_month fsif = firstkday(date_time.Sunday, date_time.Feb) self.failUnless( gregorian.date(2002, date_time.Feb,3) == fsif.get_date(2002)) self.failUnless( fsif.to_string() == "M2.1.0" ) firstkdayafter = gregorian.first_day_of_the_week_after fkaf = firstkdayafter(date_time.Monday) self.failUnless( gregorian.date(2002, date_time.Feb,4) == fkaf.get_date( gregorian.date(2002,date_time.Feb,1))) self.failUnless( '2002-Feb-04' == to_simple_string( fkaf.get_date( gregorian.date(2002,date_time.Feb,1)))) firstkdaybefore = gregorian.first_day_of_the_week_before fkbf = firstkdaybefore(date_time.Monday) self.failUnless( '2002-Feb-11' == to_simple_string(fkaf.get_date(gregorian.date(2002,date_time.Feb,10)))) self.failUnless( gregorian.date(2002,date_time.Feb,4) == fkbf.get_date(gregorian.date(2002,date_time.Feb,10))) nthkdayofmonth = gregorian.nth_day_of_the_week_in_month; nkd1 = nthkdayofmonth(nthkdayofmonth.third, date_time.Sunday, date_time.Jul) self.failUnless( gregorian.date(1969, date_time.Jul, 20) == nkd1.get_date(1969)) self.failUnless( nkd1.to_string() == "M7.3.0" ) saturday = gregorian.date(2003,date_time.Feb,8) sund = gregorian.greg_weekday(date_time.Sunday) self.failUnless( gregorian.days_until_weekday(saturday, sund) == gregorian.days(1)) self.failUnless( gregorian.days_before_weekday(saturday, sund) == gregorian.days(6)) self.failUnless( gregorian.next_weekday(saturday, sund)== gregorian.date(2003,date_time.Feb,9)) self.failUnless( gregorian.previous_weekday(saturday, sund)== gregorian.date(2003,date_time.Feb,2))