def test_strftime_pre_1900(self): """tests that we can strftime on times before 1900 (patching 2.7 bug)""" # half way through the cannons, on the battle day overture_date = datetime_tz.datetime_tz(1812, 9, 10, 12, 7, 30, tzinfo=pytz.timezone("Europe/Moscow")) overture_datestr = overture_date.strftime("%Y-%m-%d %H:%M:%S+%Z") if sys.version_info.major < 3: assert overture_datestr == "1812-09-10 12:07:30+MSK" else: # Back then, there were no timezones... assert overture_datestr == "1812-09-10 12:07:30+LMT" overture_timetuple = overture_date.utctimetuple() overture_timestr = time.strftime("%Y-%m-%d %H:%M:%S", overture_timetuple) assert overture_timestr == "1812-09-10 09:37:30" # also test after 1900 to make sure that's as before renzetti_date = overture_date.replace(year=1912) renzetti_datestr = renzetti_date.strftime("%Y-%m-%d %H:%M:%S+%Z") assert renzetti_datestr == "1912-09-10 12:07:30+MMT" renzetti_timetuple = renzetti_date.utctimetuple() renzetti_timestr = time.strftime("%Y-%m-%d %H:%M:%S", renzetti_timetuple) assert renzetti_timestr == "1912-09-10 09:37:30" # and let's handle the 0-99 and 100-999 cases that are different on different python versions rufus_date = overture_date.replace(year=12) rufus_datestr = rufus_date.strftime("%Y-%m-%d %H:%M:%S+%Z") if sys.version_info.major < 3: assert rufus_datestr == "12-09-10 12:07:30+MSK" else: assert rufus_datestr == "12-09-10 12:07:30+LMT" rufus_timetuple = rufus_date.utctimetuple() rufus_timestr = time.strftime("%Y-%m-%d %H:%M:%S", rufus_timetuple) assert rufus_timestr == "12-09-10 09:37:30" ordono_date = overture_date.replace(year=912) ordono_datestr = ordono_date.strftime("%Y-%m-%d %H:%M:%S+%Z") if sys.version_info.major < 3: assert ordono_datestr == "912-09-10 12:07:30+MSK" else: assert ordono_datestr == "912-09-10 12:07:30+LMT" ordono_timetuple = ordono_date.utctimetuple() ordono_timestr = time.strftime("%Y-%m-%d %H:%M:%S", ordono_timetuple) assert ordono_timestr == "912-09-10 09:37:30" # just for fun, and to ensure year matching doesn't match the wrong stuff repetitive_date = datetime_tz.datetime_tz(1111,11,11,11,11,11, tzinfo=pytz.UTC) repetitive_datestr = repetitive_date.strftime("%d%Y%m%H%Y%M%S1%Y1%y") assert repetitive_datestr == "11111111111111111111111111" repetitive_timetuple = repetitive_date.utctimetuple() repetitive_timestr = time.strftime("1%Y1%d%m%H%Y%M%S%Y%y", repetitive_timetuple) assert repetitive_timestr == "11111111111111111111111111" # broken with 4-char skip broken_date = datetime_tz.datetime_tz(1119,11,19) broken_datestr = broken_date.strftime("%d%Y") assert broken_datestr == "191119" broken_timetuple = broken_date.utctimetuple() broken_timestr = time.strftime("%y%Y", broken_timetuple) assert broken_timestr == "191119"
def test_compare_less(): a = datetime.datetime(2012,3,4,3,2,3) b = datetime_tz.datetime_tz(2012,3,4,1,2,3) assert b < a assert b <= a a = datetime.datetime(2012,3,3,3,2,3) assert a < b assert a <= b
def test_compare_greater(): a = datetime.datetime(2012,3,4,3,2,3) b = datetime_tz.datetime_tz(2012,3,4,1,2,3) assert a > b assert a >= b a = datetime.datetime(2012,3,3,3,2,3) assert b > a assert b >= a
def test_instantiation(): a = datetime.datetime(2012, 3, 4, 3, 2, 3) c = None try: c = datetime_tz.datetime_tz(a) except TypeError as e: if not e.message == "Must specify a timezone!": c = False assert c is None
def test_compare_less(): a = datetime.datetime(2012,3,4,3,2,3) if not PY2: a = localize(a) b = datetime_tz.datetime_tz(2012,3,4,1,2,3) assert b < a assert b <= a a = datetime.datetime(2012,3,3,3,2,3) if not PY2: a = localize(a) assert a < b assert a <= b
def test_compare_greater(): a = datetime.datetime(2012,3,4,3,2,3) if not PY2: a = localize(a) b = datetime_tz.datetime_tz(2012,3,4,1,2,3) assert a > b assert a >= b a = datetime.datetime(2012,3,3,3,2,3) if not PY2: a = localize(a) assert b > a assert b >= a
def timetuple_to_datetime(t): """Convert a timetuple to a datetime object. """ return datetime_tz.datetime_tz(*(t[:6]))
def test_compare_notequal(): a = datetime.datetime(2012,3,4,3,2,3) b = datetime_tz.datetime_tz(2012,3,4,1,2,3) assert a != b assert b != a
def test_compare_tz_second(): a = datetime.datetime(2012,3,4,1,2,3) b = datetime_tz.datetime_tz(2012,3,4,1,2,3) assert b == a
def test_compare_tz_first(): a = datetime.datetime(2012,3,4,1,2,3) b = datetime_tz.datetime_tz(2012,3,4,1,2,3) assert a == b
def test_compare_tz_second(): a = datetime.datetime(2012,3,4,1,2,3) if not PY2: a = localize(a) b = datetime_tz.datetime_tz(2012,3,4,1,2,3) assert b == a
def test_compare_tz_first(): a = datetime.datetime(2012,3,4,1,2,3) if not PY2: a = localize(a) b = datetime_tz.datetime_tz(2012,3,4,1,2,3) assert a == b
def test_strftime_pre_1900(self): """tests that we can strftime on times before 1900 (patching 2.7 bug)""" # half way through the cannons, on the battle day overture_date = datetime_tz.datetime_tz( 1812, 9, 10, 12, 7, 30, tzinfo=pytz.timezone("Europe/Moscow")) overture_datestr = overture_date.strftime("%Y-%m-%d %H:%M:%S+%Z") if sys.version_info.major < 3: assert overture_datestr == "1812-09-10 12:07:30+MSK" else: # Back then, there were no timezones... assert overture_datestr == "1812-09-10 12:07:30+LMT" overture_timetuple = overture_date.utctimetuple() overture_timestr = time.strftime("%Y-%m-%d %H:%M:%S", overture_timetuple) assert overture_timestr == "1812-09-10 09:37:30" # also test after 1900 to make sure that's as before renzetti_date = overture_date.replace(year=1912) renzetti_datestr = renzetti_date.strftime("%Y-%m-%d %H:%M:%S+%Z") assert renzetti_datestr == "1912-09-10 12:07:30+MMT" renzetti_timetuple = renzetti_date.utctimetuple() renzetti_timestr = time.strftime("%Y-%m-%d %H:%M:%S", renzetti_timetuple) assert renzetti_timestr == "1912-09-10 09:37:30" # and let's handle the 0-99 and 100-999 cases that are different on different python versions rufus_date = overture_date.replace(year=12) rufus_datestr = rufus_date.strftime("%Y-%m-%d %H:%M:%S+%Z") if sys.version_info.major < 3: assert rufus_datestr == "12-09-10 12:07:30+MSK" else: assert rufus_datestr == "12-09-10 12:07:30+LMT" rufus_timetuple = rufus_date.utctimetuple() rufus_timestr = time.strftime("%Y-%m-%d %H:%M:%S", rufus_timetuple) assert rufus_timestr == "12-09-10 09:37:30" ordono_date = overture_date.replace(year=912) ordono_datestr = ordono_date.strftime("%Y-%m-%d %H:%M:%S+%Z") if sys.version_info.major < 3: assert ordono_datestr == "912-09-10 12:07:30+MSK" else: assert ordono_datestr == "912-09-10 12:07:30+LMT" ordono_timetuple = ordono_date.utctimetuple() ordono_timestr = time.strftime("%Y-%m-%d %H:%M:%S", ordono_timetuple) assert ordono_timestr == "912-09-10 09:37:30" # just for fun, and to ensure year matching doesn't match the wrong stuff repetitive_date = datetime_tz.datetime_tz(1111, 11, 11, 11, 11, 11, tzinfo=pytz.UTC) repetitive_datestr = repetitive_date.strftime("%d%Y%m%H%Y%M%S1%Y1%y") assert repetitive_datestr == "11111111111111111111111111" repetitive_timetuple = repetitive_date.utctimetuple() repetitive_timestr = time.strftime("1%Y1%d%m%H%Y%M%S%Y%y", repetitive_timetuple) assert repetitive_timestr == "11111111111111111111111111" # broken with 4-char skip broken_date = datetime_tz.datetime_tz(1119, 11, 19) broken_datestr = broken_date.strftime("%d%Y") assert broken_datestr == "191119" broken_timetuple = broken_date.utctimetuple() broken_timestr = time.strftime("%y%Y", broken_timetuple) assert broken_timestr == "191119"