def testUtcNow(self): datetime_tz.localtz_set("US/Pacific") d = datetime_tz.datetime_tz.utcnow() self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo, pytz.utc)
def testBadDates(self): # For example, 1:30am on 27th Oct 2002 happened twice in the US/Eastern # timezone when the clocks where put back at the end of Daylight Savings # Time. # This could be 2002-10-27 01:30:00 EDT-0400 # or 2002-10-27 01:30:00 EST-0500 loc_dt = datetime.datetime(2002, 10, 27, 1, 30, 00) self.assertRaises(pytz.AmbiguousTimeError, datetime_tz.datetime_tz, loc_dt, pytz.timezone("US/Eastern")) # Check we can use is_dst to disambiguate. loc_dt = datetime.datetime(2002, 10, 27, 1, 30, 00) dt2 = datetime_tz.datetime_tz( loc_dt, pytz.timezone("US/Eastern"), is_dst=False) self.assertEqual(dt2.strftime(FMT), "2002-10-27 01:30:00 EST-0500") dt1 = datetime_tz.datetime_tz( loc_dt, pytz.timezone("US/Eastern"), is_dst=True) self.assertEqual(dt1.strftime(FMT), "2002-10-27 01:30:00 EDT-0400") # Similarly, 2:30am on 7th April 2002 never happened at all in the # US/Eastern timezone, as the clock where put forward at 2:00am skipping the # entire hour. loc_dt = datetime.datetime(2002, 4, 7, 2, 30, 00) raiseme = (pytz.AmbiguousTimeError, pytz.NonExistentTimeError) self.assertRaises(raiseme, datetime_tz.datetime_tz, loc_dt, pytz.timezone("US/Eastern")) self.assertRaises(raiseme, datetime_tz.datetime_tz, loc_dt, pytz.timezone("US/Eastern"), is_dst=True) self.assertRaises(raiseme, datetime_tz.datetime_tz, loc_dt, pytz.timezone("US/Eastern"), is_dst=False) raiseme = (pytz.AmbiguousTimeError, pytz.NonExistentTimeError) self.assertRaises(raiseme, datetime_tz.datetime_tz, loc_dt, pytz.timezone("US/Eastern")) self.assertRaises(raiseme, datetime_tz.datetime_tz, loc_dt, pytz.timezone("US/Eastern"), is_dst=True) self.assertRaises(raiseme, datetime_tz.datetime_tz, loc_dt, pytz.timezone("US/Eastern"), is_dst=False) # But make sure the cases still work when it"s "now" @staticmethod def utcnowmockedt(): return datetime_tz.datetime_tz(2002, 10, 27, 5, 30, tzinfo=pytz.utc) datetime_tz.localtz_set("US/Eastern") self.mocked("datetime_tz.datetime_tz.utcnow", utcnowmockedt) self.assertEqual(datetime_tz.datetime_tz.now().strftime(FMT), "2002-10-27 01:30:00 EDT-0400") @staticmethod def utcnowmockest(): return datetime_tz.datetime_tz(2002, 10, 27, 6, 30, tzinfo=pytz.utc) datetime_tz.localtz_set("US/Eastern") self.mocked("datetime_tz.datetime_tz.utcnow", utcnowmockest) self.assertEqual(datetime_tz.datetime_tz.now().strftime(FMT), "2002-10-27 01:30:00 EST-0500")
def testUtcFromTimestamp(self): datetime_tz.localtz_set("US/Pacific") for timestamp in -100000000, -1, 0, 1, 1233300000: d = datetime_tz.datetime_tz.utcfromtimestamp(timestamp) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo, pytz.utc) self.assertEqual(d.totimestamp(), timestamp)
def testNow(self): datetime_tz.localtz_set("US/Pacific") d = datetime_tz.datetime_tz.now() self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, pytz.timezone("US/Pacific").zone) tz = pytz.timezone("Australia/Sydney") d = datetime_tz.datetime_tz.now(tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone)
def testFromTimestamp(self): datetime_tz.localtz_set("US/Pacific") for timestamp in -100000000, -1, 0, 1, 1233300000: d = datetime_tz.datetime_tz.fromtimestamp(timestamp) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, pytz.timezone("US/Pacific").zone) self.assertEqual(d.totimestamp(), timestamp) # Changing the timezone should have no effect on the timestamp produced. d = d.astimezone("UTC") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo, pytz.utc) self.assertEqual(d.totimestamp(), timestamp)
def testSmartParse(self): datetime_tz.localtz_set("Australia/Sydney") tz = pytz.timezone("US/Pacific") now = datetime_tz.datetime_tz(2008, 12, 5, tzinfo=tz) tommorrow = now+datetime.timedelta(days=1) @staticmethod def now_fake(tzinfo): if tz is tzinfo: return now else: assert False self.mocked("datetime_tz.datetime_tz.now", now_fake) d = datetime_tz.datetime_tz.smartparse("now", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("today", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now) self.assertEqual(d.tzinfo.zone, tz.zone) # test that it's not case sensitive d = datetime_tz.datetime_tz.smartparse("ToDay", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("NOW", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("yesterday", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("tommorrow", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, tommorrow) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("a second ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(seconds=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1 second ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(seconds=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 seconds ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(seconds=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1 minute ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(minutes=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 minutes ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(minutes=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1 hour ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(hours=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 hours ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(hours=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 days ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 days 5 hours ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=2, hours=5)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 days and a hour ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=2, hours=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("4 daYs AND A SECond aGO", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=4, seconds=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1 day and a hour ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=1, hours=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("an hour and a day ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=1, hours=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1d 2h ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=1, hours=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2h5m32s ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(hours=2, minutes=5, seconds=32)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1y 2 month ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-dateutil.relativedelta.relativedelta( years=1, months=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 months and 3m ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-dateutil.relativedelta.relativedelta( months=2, minutes=3)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("3m4months1y ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-dateutil.relativedelta.relativedelta( years=1, months=4, minutes=3)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("3m4months and 1y ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-dateutil.relativedelta.relativedelta( years=1, months=4, minutes=3)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertRaises(ValueError, datetime_tz.datetime_tz.smartparse, "5 billion years ago", tz) self.assertRaises(ValueError, datetime_tz.datetime_tz.smartparse, "5 ago", tz) # FIXME: These below should actually test the equivalence d = datetime_tz.datetime_tz.smartparse("start of today", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now.replace(hour=0, minute=0, second=0, microsecond=0)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("start of tommorrow", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual( d, tommorrow.replace(hour=0, minute=0, second=0, microsecond=0)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("start of yesterday", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("end of today", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("end of tommorrow", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual( d, tommorrow.replace( hour=23, minute=59, second=59, microsecond=999999)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("end of yesterday", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) # FIXME: These above should actually test the equivalence self.mocked.tearDown() # Test datetime string with timezone information, # also provide timezone argument d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00-05:00", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.FixedOffset(-300)) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00+0800", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00+08:00") self.assertEqual(d.tzinfo, pytz.FixedOffset(480)) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00 EST", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00 EST-05:00", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) d = datetime_tz.datetime_tz.smartparse("Mon Nov 09 23:00:00 EST 2009", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) # Test datetime string with timezone information, # no more timezone argument d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00-05:00") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.FixedOffset(-300)) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00+0800") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00+08:00") self.assertEqual(d.tzinfo, pytz.FixedOffset(480)) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00 EST") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00 EST-05:00") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) d = datetime_tz.datetime_tz.smartparse("Mon Nov 09 23:00:00 EST 2009") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) # UTC, nice and easy d = datetime_tz.datetime_tz.smartparse("Tue Jul 03 06:00:01 UTC 2010") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2010-07-03 06:00:01+00:00") self.assertEqual(d.tzinfo, pytz.timezone("UTC")) # Try Pacific standard time d = datetime_tz.datetime_tz.smartparse("2002-10-27 01:20:00 EST") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2002-10-27 01:20:00-05:00") self.assertEqual(d.tzinfo.zone, pytz.timezone("US/Eastern").zone) d = datetime_tz.datetime_tz.smartparse("2002-10-27 01:20:00 EDT") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2002-10-27 01:20:00-04:00") self.assertEqual(d.tzinfo.zone, pytz.timezone("US/Eastern").zone) # Using Oslon timezones means you end up with ambigious dates. # 2002-10-27 01:30:00 US/Eastern # could be either, # 2002-10-27 01:30:00 EDT-0400 # 2002-10-27 01:30:00 EST-0500 try: d = datetime_tz.datetime_tz.smartparse( "Tue Jul 03 06:00:01 US/Pacific 2010") self.assert_(False) except ValueError: pass # Make sure we get exceptions when invalid timezones are used. try: d = datetime_tz.datetime_tz.smartparse("Mon Nov 09 23:00:00 Random 2009") self.assert_(False) except ValueError: pass try: d = datetime_tz.datetime_tz.smartparse("Mon Nov 09 23:00:00 XXX 2009") self.assert_(False) except ValueError: pass ########################################################################### toparse = datetime_tz.datetime_tz(2008, 06, 5) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y/%m/%d")) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y-%m-%d")) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y%m%d")) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse( toparse.strftime("start of %d, %B %Y")) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual( d, toparse.replace(hour=0, minute=0, second=0, microsecond=0)) toparse = datetime_tz.datetime_tz(2008, 12, 5, tzinfo=tz) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y/%m/%d"), tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y-%m-%d"), tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y%m%d"), tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse( toparse.strftime("start of %d, %B %Y"), tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertEqual( d, toparse.replace(hour=0, minute=0, second=0, microsecond=0))
def testCreation(self): # Create with the local timezone datetime_tz.localtz_set(pytz.utc) d0 = datetime_tz.datetime_tz(2008, 10, 1) self.assert_(isinstance(d0, datetime_tz.datetime_tz)) self.assertEqual(d0.tzinfo, pytz.utc) # Creation with string timezone d1 = datetime_tz.datetime_tz(2008, 10, 1, tzinfo="UTC") self.assert_(isinstance(d1, datetime_tz.datetime_tz)) self.assertEqual(d1.tzinfo, pytz.utc) # Creation with tzinfo object d2 = datetime_tz.datetime_tz(2008, 10, 1, tzinfo=pytz.utc) self.assert_(isinstance(d2, datetime_tz.datetime_tz)) self.assertEqual(d1.tzinfo, pytz.utc) # Creation from a datetime_tz object d3 = datetime_tz.datetime_tz(d1) self.assert_(isinstance(d3, datetime_tz.datetime_tz)) self.assertEqual(d1.tzinfo, d3.tzinfo) # Creation from a datetime object d4 = datetime.datetime.now() d4 = pytz.timezone("Australia/Sydney").localize(d4) d5 = datetime_tz.datetime_tz(d4) self.assert_(isinstance(d3, datetime_tz.datetime_tz)) self.assertEqual(d4.tzinfo, d5.tzinfo) # Creation from a naive datetime object not in DST d6 = datetime.datetime(2008, 12, 5) try: d7 = datetime_tz.datetime_tz(d6) self.fail("Was able to create from a naive datetime without a timezone") except TypeError: pass d7 = datetime_tz.datetime_tz(d6, "US/Pacific") self.assert_(isinstance(d7, datetime_tz.datetime_tz)) self.assertEqual(d7.tzinfo.zone, "US/Pacific") self.assertEqual(d7.tzinfo._dst, datetime.timedelta(0)) # Creation from a naive datetime object in DST d6 = datetime.datetime(2008, 7, 13) try: d7 = datetime_tz.datetime_tz(d6) self.fail("Was able to create from a naive datetime without a timezone") except TypeError: pass d7 = datetime_tz.datetime_tz(d6, "US/Pacific") self.assert_(isinstance(d7, datetime_tz.datetime_tz)) self.assertEqual(d7.tzinfo.zone, "US/Pacific") self.assertEqual(d7.tzinfo._dst, datetime.timedelta(0, 3600)) datetime_tz.localtz_set(pytz.utc) d0 = datetime_tz.datetime_tz(2008, 10, 1) self.assert_(isinstance(d0, datetime_tz.datetime_tz)) self.assertEqual(d0.tzinfo, pytz.utc) d1 = datetime_tz.datetime_tz(d1) self.assert_(isinstance(d1, datetime_tz.datetime_tz)) self.assertEqual(d1.tzinfo, pytz.utc) self.assertEqual(d0, d1) self.assertFalse(d0 is d1)
def setUp(self): # All tests are assumed to be in Australia/Sydney unless otherwise # specified. datetime_tz.localtz_set("Australia/Sydney") self.mocked = MockMe()
def testSmartParse(self): datetime_tz.localtz_set("Australia/Sydney") tz = pytz.timezone("US/Pacific") now = datetime_tz.datetime_tz(2008, 12, 5, tzinfo=tz) tomorrow = now+datetime.timedelta(days=1) @staticmethod def now_fake(tzinfo): if tz is tzinfo: return now else: assert False self.mocked("datetime_tz.datetime_tz.now", now_fake) d = datetime_tz.datetime_tz.smartparse("now", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("today", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now) self.assertEqual(d.tzinfo.zone, tz.zone) # test that it's not case sensitive d = datetime_tz.datetime_tz.smartparse("ToDay", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("NOW", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("yesterday", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("tomorrow", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, tomorrow) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("a second ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(seconds=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1 second ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(seconds=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 seconds ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(seconds=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1 minute ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(minutes=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 minutes ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(minutes=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1 hour ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(hours=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 hours ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(hours=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 days ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 days 5 hours ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=2, hours=5)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 days and a hour ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=2, hours=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("4 daYs AND A SECond aGO", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=4, seconds=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1 day and a hour ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=1, hours=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("an hour and a day ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=1, hours=1)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1d 2h ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(days=1, hours=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2h5m32s ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-datetime.timedelta(hours=2, minutes=5, seconds=32)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("1y 2 month ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-dateutil.relativedelta.relativedelta( years=1, months=2)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("2 months and 3m ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-dateutil.relativedelta.relativedelta( months=2, minutes=3)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("3m4months1y ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-dateutil.relativedelta.relativedelta( years=1, months=4, minutes=3)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("3m4months and 1y ago", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now-dateutil.relativedelta.relativedelta( years=1, months=4, minutes=3)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertRaises(ValueError, datetime_tz.datetime_tz.smartparse, "5 billion years ago", tz) self.assertRaises(ValueError, datetime_tz.datetime_tz.smartparse, "5 ago", tz) # FIXME: These below should actually test the equivalence d = datetime_tz.datetime_tz.smartparse("start of today", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, now.replace(hour=0, minute=0, second=0, microsecond=0)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("start of tomorrow", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual( d, tomorrow.replace(hour=0, minute=0, second=0, microsecond=0)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("start of yesterday", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("end of today", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("end of tomorrow", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual( d, tomorrow.replace( hour=23, minute=59, second=59, microsecond=999999)) self.assertEqual(d.tzinfo.zone, tz.zone) d = datetime_tz.datetime_tz.smartparse("end of yesterday", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) # FIXME: These above should actually test the equivalence self.mocked.tearDown() # Test datetime string with timezone information, # also provide timezone argument d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00-05:00", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.FixedOffset(-300)) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00+0800", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00+08:00") self.assertEqual(d.tzinfo, pytz.FixedOffset(480)) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00 EST", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00 EST-05:00", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) d = datetime_tz.datetime_tz.smartparse("Mon Nov 09 23:00:00 EST 2009", tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) # Test datetime string with timezone information, # no more timezone argument d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00-05:00") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.FixedOffset(-300)) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00+0800") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00+08:00") self.assertEqual(d.tzinfo, pytz.FixedOffset(480)) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00 EST") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) d = datetime_tz.datetime_tz.smartparse("2009-11-09 23:00:00 EST-05:00") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) d = datetime_tz.datetime_tz.smartparse("Mon Nov 09 23:00:00 EST 2009") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2009-11-09 23:00:00-05:00") self.assertEqual(d.tzinfo, pytz.timezone("US/Eastern")) # UTC, nice and easy d = datetime_tz.datetime_tz.smartparse("Tue Jul 03 06:00:01 UTC 2010") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2010-07-03 06:00:01+00:00") self.assertEqual(d.tzinfo, pytz.timezone("UTC")) # Try Pacific standard time d = datetime_tz.datetime_tz.smartparse("2002-10-27 01:20:00 EST") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2002-10-27 01:20:00-05:00") self.assertEqual(d.tzinfo.zone, pytz.timezone("US/Eastern").zone) d = datetime_tz.datetime_tz.smartparse("2002-10-27 01:20:00 EDT") self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(str(d), "2002-10-27 01:20:00-04:00") self.assertEqual(d.tzinfo.zone, pytz.timezone("US/Eastern").zone) # Using Oslon timezones means you end up with ambigious dates. # 2002-10-27 01:30:00 US/Eastern # could be either, # 2002-10-27 01:30:00 EDT-0400 # 2002-10-27 01:30:00 EST-0500 try: d = datetime_tz.datetime_tz.smartparse( "Tue Jul 03 06:00:01 US/Pacific 2010") self.assert_(False) except ValueError: pass # Make sure we get exceptions when invalid timezones are used. try: d = datetime_tz.datetime_tz.smartparse("Mon Nov 09 23:00:00 Random 2009") self.assert_(False) except ValueError: pass try: d = datetime_tz.datetime_tz.smartparse("Mon Nov 09 23:00:00 XXX 2009") self.assert_(False) except ValueError: pass ########################################################################### toparse = datetime_tz.datetime_tz(2008, 6, 5) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y/%m/%d")) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y-%m-%d")) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y%m%d")) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse( toparse.strftime("start of %d, %B %Y")) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual( d, toparse.replace(hour=0, minute=0, second=0, microsecond=0)) toparse = datetime_tz.datetime_tz(2008, 12, 5, tzinfo=tz) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y/%m/%d"), tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y-%m-%d"), tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse(toparse.strftime("%Y%m%d"), tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertEqual(d, toparse) d = datetime_tz.datetime_tz.smartparse( toparse.strftime("start of %d, %B %Y"), tz) self.assert_(isinstance(d, datetime_tz.datetime_tz)) self.assertEqual(d.tzinfo.zone, tz.zone) self.assertEqual( d, toparse.replace(hour=0, minute=0, second=0, microsecond=0))