def test_tzinfo(self): # Verify that tzinfo works correctly. ts1 = 294217199000 # In EST ts2 = 294217200000 # In EDT (spring forward, we skip ahead by 1 hour) utc_dt1 = datetime(1979, 4, 29, 6, 59, 59) utc_dt2 = datetime(1979, 4, 29, 7, 0, 0) self.assertEqual( moment.tz(ts1).datetime().strftime(fmt), '1979-04-29 06:59:59 UTC') self.assertEqual( moment.tz(ts2).datetime().strftime(fmt), '1979-04-29 07:00:00 UTC') # Verify that we get correct time zone variation depending on DST status. nyc_dt1 = moment.tz(ts1, 'America/New_York').datetime() nyc_dt2 = moment.tz(ts2, 'America/New_York').datetime() self.assertEqual(nyc_dt1.strftime(fmt), '1979-04-29 01:59:59 EST') self.assertEqual(nyc_dt2.strftime(fmt), '1979-04-29 03:00:00 EDT') # Make sure we can get timestamps back from these datatimes. self.assertEqual(moment.dt_to_ts(nyc_dt1) * 1000, ts1) self.assertEqual(moment.dt_to_ts(nyc_dt2) * 1000, ts2) # Verify that the datetime objects we get produce correct time zones in terms of DST when we # manipulate them. NOTE: it is a bit unexpected that we add 1hr + 1sec rather than just 1sec, # but it seems like that is how Python datetime works. Note that timezone does get switched # correctly between EDT and EST. self.assertEqual(nyc_dt1 + timedelta(seconds=3601), nyc_dt2) self.assertEqual(nyc_dt2 - timedelta(seconds=3601), nyc_dt1) self.assertEqual((nyc_dt1 + timedelta(seconds=3601)).strftime(fmt), '1979-04-29 03:00:00 EDT') self.assertEqual((nyc_dt2 - timedelta(seconds=3601)).strftime(fmt), '1979-04-29 01:59:59 EST')
def test_standard_entry(self): name = "America/New_York" data = self.new_york for entry in data: date = entry[0] timestamp = entry[1] m = moment.tz(date).tz(name) mts = moment.tz(timestamp, name) self.assertMatches(entry, m) self.assertMatches(entry, mts) error_data = self.new_york_errors for entry in error_data: name = entry[0] date = entry[1] error = entry[2] self.assertRaises(error, moment.tz, date, name)
def test_zone_entry(self): name = "America/Los_Angeles" data = self.los_angeles for entry in data: date = entry[0] timestamp = entry[1] m = moment.tz(date, name) self.assertMatches(entry, m)
def test_zone(self): name = "America/New_York" tzinfo = moment.tzinfo(name) data = self.new_york for entry in data: date = entry[0] ts = entry[1] abbr = entry[2] offset = entry[3] dt = moment.tz(ts, name).datetime() self.assertEqual(dt.tzname(), abbr) self.assertEqual(dt.utcoffset(), timedelta(minutes=-offset))
def parse(date_string, parse_format, zonelabel='UTC', override_current_date=None): """Parse a date string via a moment.js style parse format and a timezone string. Supported tokens are documented above. Returns seconds since epoch""" if parse_format in FORMAT_CACHE: # Check if parse_format has been cache, and retrieve if so parser = FORMAT_CACHE[parse_format] else: # e.g. "MM-YY" -> "(?P<mm>\d{1,2})-(?P<yy>\d{2})" # Note that DATE_TOKENS is ordered so that the longer letter chains are recognized first tokens = DATE_TOKENS_REGEX.split(parse_format) tokens = [ DATE_TOKENS[t] if t in DATE_TOKENS else SEP_REGEX.sub(SEP, t) for t in tokens ] # Compile new token string ignoring case (for month names) parser = re.compile(''.join(tokens), re.I) FORMAT_CACHE[parse_format] = parser match = parser.match(date_string) # Throw error if matching failed if match is None: raise Exception("Failed to parse %s with %s" % (date_string, parse_format)) # Create datetime from the results of parsing current_date = override_current_date or moment.CURRENT_DATE m = match.groupdict() dt = datetime(year=getYear(m, current_date.year), month=getMonth(m, current_date.month), day=int(m['D']) if ('D' in m) else current_date.day, hour=getHour(m), minute=int(m['m']) if ('m' in m) else 0, second=int(m['s']) if ('s' in m) else 0, microsecond=getMicrosecond(m)) # Parses the datetime with the given timezone to return the seconds since EPOCH return moment.tz(dt, zonelabel).timestamp_s()