def test_ts_to_dt(self): # Verify that ts_to_dt works as expected. value_sec = 1426291200 # 2015-03-14 00:00:00 in UTC value_dt_utc = moment.ts_to_dt(value_sec, moment.get_zone('UTC')) value_dt_aware = moment.ts_to_dt(value_sec, moment.get_zone('America/New_York')) self.assertEqual(value_dt_utc.strftime("%Y-%m-%d %H:%M:%S %Z"), '2015-03-14 00:00:00 UTC') self.assertEqual(value_dt_aware.strftime("%Y-%m-%d %H:%M:%S %Z"), '2015-03-13 20:00:00 EDT')
def test_parse_iso_date(self): tzny = moment.get_zone('America/New_York') iso = moment.parse_iso_date # Note that time components and time zone do NOT affect the returned timestamp. self.assertEqual(iso('2019-01-22'), 1548115200) self.assertEqual(iso('2019-01-22T00:47:39.219071'), 1548115200) self.assertEqual(iso('2019-01-22 00:47:39Z'), 1548115200) self.assertEqual(iso('2019-01-22T00:47:39.219071-05:00'), 1548115200) self.assertEqual(iso('2019-01-22T00:47:39.219071+05:00'), 1548115200)
def test_dt_to_ds(self): # Verify that dt_to_ts works for both naive and aware datetime objects. value_dt = datetime(2015, 3, 14, 0, 0) # In UTC value_sec = 1426291200 tzla = moment.get_zone('America/Los_Angeles') def format_utc(ts): return moment.ts_to_dt(ts, moment.get_zone('UTC')).strftime(fmt) # Check that a naive datetime is interpreted in UTC. self.assertEqual(value_dt.strftime("%Y-%m-%d %H:%M:%S %Z"), '2015-03-14 00:00:00 ') self.assertEqual(moment.dt_to_ts(value_dt), value_sec) # Interpreted in UTC # Get an explicit UTC version and make sure that also works. value_dt_utc = value_dt.replace(tzinfo=moment.TZ_UTC) self.assertEqual(value_dt_utc.strftime(fmt), '2015-03-14 00:00:00 UTC') self.assertEqual(moment.dt_to_ts(value_dt_utc), value_sec) # Get an aware datetime, and make sure that works too. value_dt_aware = moment.ts_to_dt(value_sec, moment.get_zone('America/New_York')) self.assertEqual(value_dt_aware.strftime(fmt), '2015-03-13 20:00:00 EDT') self.assertEqual(moment.dt_to_ts(value_dt_aware), value_sec) # Check that dt_to_ts pays attention to the timezone. # If we interpret midnight in LA time, it's a later timestamp. self.assertEqual(format_utc(moment.dt_to_ts(value_dt, tzla)), '2015-03-14 07:00:00 UTC') # The second argument is ignored if the datetime is aware. self.assertEqual(format_utc(moment.dt_to_ts(value_dt_utc, tzla)), '2015-03-14 00:00:00 UTC') self.assertEqual(format_utc(moment.dt_to_ts(value_dt_aware, tzla)), '2015-03-14 00:00:00 UTC') # If we modify an aware datetime, we may get a new timezone abbreviation. value_dt_aware -= timedelta(days=28) self.assertEqual(value_dt_aware.strftime(fmt), '2015-02-13 20:00:00 EST')
def test_date_to_ts(self): d = date(2015, 3, 14) tzla = moment.get_zone('America/Los_Angeles') def format_utc(ts): return moment.ts_to_dt(ts, moment.get_zone('UTC')).strftime(fmt) self.assertEqual(format_utc(moment.date_to_ts(d)), '2015-03-14 00:00:00 UTC') self.assertEqual(format_utc(moment.date_to_ts(d, tzla)), '2015-03-14 07:00:00 UTC') self.assertEqual( moment.ts_to_dt(moment.date_to_ts(d, tzla), tzla).strftime(fmt), '2015-03-14 00:00:00 PDT')
def test_parse_iso(self): tzny = moment.get_zone('America/New_York') iso = moment.parse_iso self.assertEqual(iso('2011-11-11T11:11:11'), 1321009871.000000) self.assertEqual(iso('2019-01-22T00:47:39.219071-05:00'), 1548136059.219071) self.assertEqual(iso('2019-01-22T00:47:39.219071-0500'), 1548136059.219071) self.assertEqual(iso('2019-01-22T00:47:39.219071', timezone=tzny), 1548136059.219071) self.assertEqual(iso('2019-01-22T00:47:39.219071'), 1548118059.219071) self.assertEqual(iso('2019-01-22T00:47:39.219071Z'), 1548118059.219071) self.assertEqual(iso('2019-01-22T00:47:39.219071Z', timezone=tzny), 1548118059.219071) self.assertEqual(iso('2019-01-22T00:47:39.219'), 1548118059.219) self.assertEqual(iso('2019-01-22T00:47:39'), 1548118059) self.assertEqual(iso('2019-01-22 00:47:39.219071'), 1548118059.219071) self.assertEqual(iso('2019-01-22 00:47:39'), 1548118059) self.assertEqual(iso('2019-01-22'), 1548115200)
def format_utc(ts): return moment.ts_to_dt(ts, moment.get_zone('UTC')).strftime(fmt)
def ts_to_dt_nyc(dt): return moment.ts_to_dt(dt, moment.get_zone('America/New_York'))
def ts_to_dt_utc(dt): return moment.ts_to_dt(dt, moment.get_zone('UTC'))