def test_datetime_000_10(self): """Test method 'MythTV.datetime.fromtimestamp()' with given time-zone""" # MythTV's datetime implementation: utctz = datetime.UTCTZ() ct3 = datetime.now(tz=utctz).timestamp() #print(ct3, type(ct3)) t9 = datetime.utcnow().timestamp() #print(t9, type(t9)) self.assertTrue( (abs(ct3 - self.cts) < 0.01), "wrong implementation of 'datetime.now(tz=utctz).timestamp()'") self.assertTrue( (abs(t9 - self.cts) < 0.01), "wrong implementation of 'datetime.now(tz=utctz).timestamp()'") tzlocal = datetime.localTZ() cf2 = datetime.fromtimestamp(ct3, tz=tzlocal) #print(cf2, type(cf2)) t10 = datetime.now() self.assertTrue(isinstance(cf2.tzinfo, MythTV.utility.dt.posixtzinfo), "No posixtzinfo for 'datetime.fromtimestamp()") self.assertTrue( ((cf2 - t10) < timedelta(microseconds=100)), "wrong implementation of 'datetime.now(tz=utctz).timestamp()'")
def test_datetime_002_09(self): """Test 'timestamp (i.e. seconds from epoch)' to 'UTC' conversion in respect to daylight saving time of 'America/Anchorage' using 'datetime.fromtimestamp(,tz=datetime.UTCTZ)' method. """ for i, t in enumerate(self.t_timestamp_list): t_ts_utc = datetime.fromtimestamp(t, tz=datetime.UTCTZ()) t_org = t_ts_utc.isoformat() self.assertTrue(isinstance(t_ts_utc, datetime)) # check if conversion works: self.assertEqual(t_org, self.t_timestamp_iso_utc_list[i]) # check for correct types: self.assertTrue(isinstance(t_ts_utc, datetime)) self.assertTrue('posixtzinfo' in repr(t_ts_utc.tzinfo))
def test_datetime_002_08(self): """Test 'timestamp (i.e. seconds from epoch)' to 'UTC' conversion in respect to daylight saving time of 'America/Anchorage' using 'datetime.fromtimestamp()' method. Note: This is using the local timezone for conversion. """ for i, t in enumerate(self.t_timestamp_list): t_ts = datetime.fromtimestamp(t) t_org = t_ts.isoformat() self.assertTrue(isinstance(t_ts, datetime)) # check if conversion works: self.assertEqual(t_org, self.t_timestamp_utc_list[i]) # check for correct types: self.assertTrue(isinstance(t_ts, datetime)) self.assertTrue('posixtzinfo' in repr(t_ts.tzinfo))
def test_datetime_000_09(self): """Test method 'MythTV.datetime.fromtimestamp()' without given time-zone""" ct0 = time.time() #print(ct0,type(ct0)) try: ct1 = _pytzdt.now().timestamp() # python 3.x except AttributeError: # python2 does not have a 'datetime.timestamp': ct1 = time.time() # python 2.7 self.assertTrue(( abs(ct1 - ct0) < 0.01 ), "Timestamp values different in 'datetime.timestamp' and 'time.timestamp'" ) #print(ct1, type(ct1)) # MythTV's datetime implementation: ct2 = datetime.now().timestamp() #print(ct2, type(ct2)) # 1590849753.5594988 <class 'float'> self.assertTrue( (abs(ct2 - ct1) < 0.01), "Timestamp values different in 'MythTV' and 'datetime'") self.assertTrue( (abs(ct2 - self.cts) < 0.1), "Timestamp values different in 'MythTV' and linux 'date +%s.%N'") cf1 = datetime.fromtimestamp(ct2) #print(cf1, type(cf1)) # 2020-05-30 17:23:51.551984+02:00 <class 'MythTV.utility.dt.datetime'> self.assertTrue(isinstance(cf1.tzinfo, MythTV.utility.dt.posixtzinfo), "No posixtzinfo for 'datetime.fromtimestamp()") t8 = datetime.now() #print(t8, type(t8)) self.assertTrue(( abs(t8 - cf1) < timedelta(seconds=1) ), "'datetime.fromtimestamp()' values different in 'MythTV' and linux 'date +%s.%N'" )