Python datetime.utcfromtimestamp() Examples
The following are 30 code examples for showing how to use datetime.utcfromtimestamp(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
datetime
, or try the search function
.
Example 1
Project: sun-position Author: s-bear File: sunposition.py License: MIT License | 6 votes |
def julian_day(dt): """Convert UTC datetimes or UTC timestamps to Julian days Parameters ---------- dt : array_like UTC datetime objects or UTC timestamps (as per datetime.utcfromtimestamp) Returns ------- jd : ndarray datetimes converted to fractional Julian days """ dts = np.array(dt) if len(dts.shape) == 0: return _sp.julian_day(dt) jds = np.empty(dts.shape) for i,d in enumerate(dts.flat): jds.flat[i] = _sp.julian_day(d) return jds
Example 2
Project: zschema Author: zmap File: leaves.py License: Apache License 2.0 | 6 votes |
def _validate(self, name, value, path=_NO_ARG): try: if isinstance(value, datetime.datetime): dt = value elif isinstance(value, int): dt = datetime.datetime.utcfromtimestamp(value) else: dt = dateutil.parser.parse(value) except (ValueError, TypeError): # Either `datetime.utcfromtimestamp` or `dateutil.parser.parse` above # may raise on invalid input. m = "%s: %s is not valid timestamp" % (name, str(value)) raise DataValidationException(m, path=path) dt = DateTime._ensure_tz_aware(dt) if dt > self._max_value_dt: m = "%s: %s is greater than allowed maximum (%s)" % (name, str(value), str(self._max_value_dt)) raise DataValidationException(m, path=path) if dt < self._min_value_dt: m = "%s: %s is less than allowed minimum (%s)" % (name, str(value), str(self._min_value_dt)) raise DataValidationException(m, path=path)
Example 3
Project: ironpython2 Author: IronLanguages File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_utcfromtimestamp(self): import time ts = time.time() expected = time.gmtime(ts) got = self.theclass.utcfromtimestamp(ts) self.verify_field_equality(expected, got)
Example 4
Project: ironpython2 Author: IronLanguages File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_insane_utcfromtimestamp(self): # It's possible that some platform maps time_t to double, # and that this test will fail there. This test should # exempt such platforms (provided they return reasonable # results!). for insane in -1e200, 1e200: self.assertRaises(ValueError, self.theclass.utcfromtimestamp, insane)
Example 5
Project: ironpython2 Author: IronLanguages File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_negative_float_utcfromtimestamp(self): d = self.theclass.utcfromtimestamp(-1.05) self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))
Example 6
Project: ironpython2 Author: IronLanguages File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_utcnow(self): import time # Call it a success if utcnow() and utcfromtimestamp() are within # a second of each other. tolerance = timedelta(seconds=1) for dummy in range(3): from_now = self.theclass.utcnow() from_timestamp = self.theclass.utcfromtimestamp(time.time()) if abs(from_timestamp - from_now) <= tolerance: break # Else try again a few times. self.assertLessEqual(abs(from_timestamp - from_now), tolerance)
Example 7
Project: ironpython2 Author: IronLanguages File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_tzinfo_fromtimestamp(self): import time meth = self.theclass.fromtimestamp ts = time.time() # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up). base = meth(ts) # Try with and without naming the keyword. off42 = FixedOffset(42, "42") another = meth(ts, off42) again = meth(ts, tz=off42) self.assertIs(another.tzinfo, again.tzinfo) self.assertEqual(another.utcoffset(), timedelta(minutes=42)) # Bad argument with and w/o naming the keyword. self.assertRaises(TypeError, meth, ts, 16) self.assertRaises(TypeError, meth, ts, tzinfo=16) # Bad keyword name. self.assertRaises(TypeError, meth, ts, tinfo=off42) # Too many args. self.assertRaises(TypeError, meth, ts, off42, off42) # Too few args. self.assertRaises(TypeError, meth) # Try to make sure tz= actually does some conversion. timestamp = 1000000000 utcdatetime = datetime.utcfromtimestamp(timestamp) # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take. # But on some flavor of Mac, it's nowhere near that. So we can't have # any idea here what time that actually is, we can only test that # relative changes match. utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero tz = FixedOffset(utcoffset, "tz", 0) expected = utcdatetime + utcoffset got = datetime.fromtimestamp(timestamp, tz) self.assertEqual(expected, got.replace(tzinfo=None))
Example 8
Project: sun-position Author: s-bear File: sunposition.py License: MIT License | 5 votes |
def calendar_time(dt): try: x = dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond return x except AttributeError: try: return _sp.calendar_time(datetime.utcfromtimestamp(dt)) #will raise OSError if dt is not acceptable except: raise TypeError('dt must be datetime object or POSIX timestamp')
Example 9
Project: BinderFilter Author: dxwu File: test_datetime.py License: MIT License | 5 votes |
def test_utcfromtimestamp(self): import time ts = time.time() expected = time.gmtime(ts) got = self.theclass.utcfromtimestamp(ts) self.verify_field_equality(expected, got)
Example 10
Project: BinderFilter Author: dxwu File: test_datetime.py License: MIT License | 5 votes |
def test_insane_utcfromtimestamp(self): # It's possible that some platform maps time_t to double, # and that this test will fail there. This test should # exempt such platforms (provided they return reasonable # results!). for insane in -1e200, 1e200: self.assertRaises(ValueError, self.theclass.utcfromtimestamp, insane)
Example 11
Project: BinderFilter Author: dxwu File: test_datetime.py License: MIT License | 5 votes |
def test_negative_float_utcfromtimestamp(self): d = self.theclass.utcfromtimestamp(-1.05) self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))
Example 12
Project: BinderFilter Author: dxwu File: test_datetime.py License: MIT License | 5 votes |
def test_utcnow(self): import time # Call it a success if utcnow() and utcfromtimestamp() are within # a second of each other. tolerance = timedelta(seconds=1) for dummy in range(3): from_now = self.theclass.utcnow() from_timestamp = self.theclass.utcfromtimestamp(time.time()) if abs(from_timestamp - from_now) <= tolerance: break # Else try again a few times. self.assertTrue(abs(from_timestamp - from_now) <= tolerance)
Example 13
Project: BinderFilter Author: dxwu File: test_datetime.py License: MIT License | 5 votes |
def test_tzinfo_fromtimestamp(self): import time meth = self.theclass.fromtimestamp ts = time.time() # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up). base = meth(ts) # Try with and without naming the keyword. off42 = FixedOffset(42, "42") another = meth(ts, off42) again = meth(ts, tz=off42) self.assertTrue(another.tzinfo is again.tzinfo) self.assertEqual(another.utcoffset(), timedelta(minutes=42)) # Bad argument with and w/o naming the keyword. self.assertRaises(TypeError, meth, ts, 16) self.assertRaises(TypeError, meth, ts, tzinfo=16) # Bad keyword name. self.assertRaises(TypeError, meth, ts, tinfo=off42) # Too many args. self.assertRaises(TypeError, meth, ts, off42, off42) # Too few args. self.assertRaises(TypeError, meth) # Try to make sure tz= actually does some conversion. timestamp = 1000000000 utcdatetime = datetime.utcfromtimestamp(timestamp) # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take. # But on some flavor of Mac, it's nowhere near that. So we can't have # any idea here what time that actually is, we can only test that # relative changes match. utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero tz = FixedOffset(utcoffset, "tz", 0) expected = utcdatetime + utcoffset got = datetime.fromtimestamp(timestamp, tz) self.assertEqual(expected, got.replace(tzinfo=None))
Example 14
Project: oss-ftp Author: aliyun File: test_datetime.py License: MIT License | 5 votes |
def test_utcfromtimestamp(self): import time ts = time.time() expected = time.gmtime(ts) got = self.theclass.utcfromtimestamp(ts) self.verify_field_equality(expected, got)
Example 15
Project: oss-ftp Author: aliyun File: test_datetime.py License: MIT License | 5 votes |
def test_insane_utcfromtimestamp(self): # It's possible that some platform maps time_t to double, # and that this test will fail there. This test should # exempt such platforms (provided they return reasonable # results!). for insane in -1e200, 1e200: self.assertRaises(ValueError, self.theclass.utcfromtimestamp, insane)
Example 16
Project: oss-ftp Author: aliyun File: test_datetime.py License: MIT License | 5 votes |
def test_negative_float_utcfromtimestamp(self): d = self.theclass.utcfromtimestamp(-1.05) self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))
Example 17
Project: oss-ftp Author: aliyun File: test_datetime.py License: MIT License | 5 votes |
def test_utcnow(self): import time # Call it a success if utcnow() and utcfromtimestamp() are within # a second of each other. tolerance = timedelta(seconds=1) for dummy in range(3): from_now = self.theclass.utcnow() from_timestamp = self.theclass.utcfromtimestamp(time.time()) if abs(from_timestamp - from_now) <= tolerance: break # Else try again a few times. self.assertLessEqual(abs(from_timestamp - from_now), tolerance)
Example 18
Project: oss-ftp Author: aliyun File: test_datetime.py License: MIT License | 5 votes |
def test_tzinfo_fromtimestamp(self): import time meth = self.theclass.fromtimestamp ts = time.time() # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up). base = meth(ts) # Try with and without naming the keyword. off42 = FixedOffset(42, "42") another = meth(ts, off42) again = meth(ts, tz=off42) self.assertIs(another.tzinfo, again.tzinfo) self.assertEqual(another.utcoffset(), timedelta(minutes=42)) # Bad argument with and w/o naming the keyword. self.assertRaises(TypeError, meth, ts, 16) self.assertRaises(TypeError, meth, ts, tzinfo=16) # Bad keyword name. self.assertRaises(TypeError, meth, ts, tinfo=off42) # Too many args. self.assertRaises(TypeError, meth, ts, off42, off42) # Too few args. self.assertRaises(TypeError, meth) # Try to make sure tz= actually does some conversion. timestamp = 1000000000 utcdatetime = datetime.utcfromtimestamp(timestamp) # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take. # But on some flavor of Mac, it's nowhere near that. So we can't have # any idea here what time that actually is, we can only test that # relative changes match. utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero tz = FixedOffset(utcoffset, "tz", 0) expected = utcdatetime + utcoffset got = datetime.fromtimestamp(timestamp, tz) self.assertEqual(expected, got.replace(tzinfo=None))
Example 19
Project: archon Author: economicnetwork File: models.py License: MIT License | 5 votes |
def conv_timestamp(ts, exchange): target_format = '%Y-%m-%dT%H:%M:%S' if exchange==exc.CRYPTOPIA: #local = pytz.timezone("Europe/London") #tsf = datetime.datetime.fromtimestamp(ts) tsf = datetime.datetime.utcfromtimestamp(ts) #local_dt = local.localize(tsf, is_dst=None) utc_dt = tsf.astimezone(pytz.utc) utc_dt = utc_dt + datetime.timedelta(hours=4) #dt = utc_dt.strftime(date_broker_format) tsf = utc_dt.strftime(target_format) return tsf elif exchange==exc.BITTREX: ts = ts.split('.')[0] tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S') utc=pytz.UTC utc_dt = tsf.astimezone(pytz.utc) utc_dt = utc_dt + datetime.timedelta(hours=4) tsf = utc_dt.strftime(target_format) return tsf elif exchange==exc.KUCOIN: #dt = conv_timestamp(t/1000,exchange) tsf = datetime.datetime.utcfromtimestamp(ts/1000) #tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S') utc=pytz.UTC utc_dt = tsf.astimezone(pytz.utc) utc_dt = utc_dt + datetime.timedelta(hours=4) #tsf = utc_dt.strftime() tsf = utc_dt.strftime(target_format) return tsf elif exchange==exc.BINANCE: tsf = datetime.datetime.utcfromtimestamp(int(ts/1000)) utc=pytz.UTC utc_dt = tsf.astimezone(pytz.utc) utc_dt = utc_dt + datetime.timedelta(hours=4) tsf = utc_dt.strftime(target_format) return tsf
Example 20
Project: gcblue Author: gcblue File: test_datetime.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_utcfromtimestamp(self): import time ts = time.time() expected = time.gmtime(ts) got = self.theclass.utcfromtimestamp(ts) self.verify_field_equality(expected, got)
Example 21
Project: gcblue Author: gcblue File: test_datetime.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_insane_utcfromtimestamp(self): # It's possible that some platform maps time_t to double, # and that this test will fail there. This test should # exempt such platforms (provided they return reasonable # results!). for insane in -1e200, 1e200: self.assertRaises(ValueError, self.theclass.utcfromtimestamp, insane)
Example 22
Project: gcblue Author: gcblue File: test_datetime.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_negative_float_utcfromtimestamp(self): d = self.theclass.utcfromtimestamp(-1.05) self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))
Example 23
Project: gcblue Author: gcblue File: test_datetime.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_utcnow(self): import time # Call it a success if utcnow() and utcfromtimestamp() are within # a second of each other. tolerance = timedelta(seconds=1) for dummy in range(3): from_now = self.theclass.utcnow() from_timestamp = self.theclass.utcfromtimestamp(time.time()) if abs(from_timestamp - from_now) <= tolerance: break # Else try again a few times. self.assertLessEqual(abs(from_timestamp - from_now), tolerance)
Example 24
Project: gcblue Author: gcblue File: test_datetime.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_tzinfo_fromtimestamp(self): import time meth = self.theclass.fromtimestamp ts = time.time() # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up). base = meth(ts) # Try with and without naming the keyword. off42 = FixedOffset(42, "42") another = meth(ts, off42) again = meth(ts, tz=off42) self.assertIs(another.tzinfo, again.tzinfo) self.assertEqual(another.utcoffset(), timedelta(minutes=42)) # Bad argument with and w/o naming the keyword. self.assertRaises(TypeError, meth, ts, 16) self.assertRaises(TypeError, meth, ts, tzinfo=16) # Bad keyword name. self.assertRaises(TypeError, meth, ts, tinfo=off42) # Too many args. self.assertRaises(TypeError, meth, ts, off42, off42) # Too few args. self.assertRaises(TypeError, meth) # Try to make sure tz= actually does some conversion. timestamp = 1000000000 utcdatetime = datetime.utcfromtimestamp(timestamp) # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take. # But on some flavor of Mac, it's nowhere near that. So we can't have # any idea here what time that actually is, we can only test that # relative changes match. utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero tz = FixedOffset(utcoffset, "tz", 0) expected = utcdatetime + utcoffset got = datetime.fromtimestamp(timestamp, tz) self.assertEqual(expected, got.replace(tzinfo=None))
Example 25
Project: medicare-demo Author: ofermend File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_utcfromtimestamp(self): import time ts = time.time() expected = time.gmtime(ts) got = self.theclass.utcfromtimestamp(ts) self.verify_field_equality(expected, got)
Example 26
Project: medicare-demo Author: ofermend File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_insane_utcfromtimestamp(self): # It's possible that some platform maps time_t to double, # and that this test will fail there. This test should # exempt such platforms (provided they return reasonable # results!). for insane in -1e200, 1e200: self.assertRaises(ValueError, self.theclass.utcfromtimestamp, insane)
Example 27
Project: medicare-demo Author: ofermend File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_negative_float_utcfromtimestamp(self): # Windows doesn't accept negative timestamps if os.name == "nt": return d = self.theclass.utcfromtimestamp(-1.05) self.assertEquals(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))
Example 28
Project: medicare-demo Author: ofermend File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_utcnow(self): import time # Call it a success if utcnow() and utcfromtimestamp() are within # a second of each other. tolerance = timedelta(seconds=1) for dummy in range(3): from_now = self.theclass.utcnow() from_timestamp = self.theclass.utcfromtimestamp(time.time()) if abs(from_timestamp - from_now) <= tolerance: break # Else try again a few times. self.failUnless(abs(from_timestamp - from_now) <= tolerance)
Example 29
Project: medicare-demo Author: ofermend File: test_datetime.py License: Apache License 2.0 | 5 votes |
def test_tzinfo_fromtimestamp(self): import time meth = self.theclass.fromtimestamp ts = time.time() # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up). base = meth(ts) # Try with and without naming the keyword. off42 = FixedOffset(42, "42") another = meth(ts, off42) again = meth(ts, tz=off42) self.failUnless(another.tzinfo is again.tzinfo) self.assertEqual(another.utcoffset(), timedelta(minutes=42)) # Bad argument with and w/o naming the keyword. self.assertRaises(TypeError, meth, ts, 16) self.assertRaises(TypeError, meth, ts, tzinfo=16) # Bad keyword name. self.assertRaises(TypeError, meth, ts, tinfo=off42) # Too many args. self.assertRaises(TypeError, meth, ts, off42, off42) # Too few args. self.assertRaises(TypeError, meth) # Try to make sure tz= actually does some conversion. timestamp = 1000000000 utcdatetime = datetime.utcfromtimestamp(timestamp) # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take. # But on some flavor of Mac, it's nowhere near that. So we can't have # any idea here what time that actually is, we can only test that # relative changes match. utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero tz = FixedOffset(utcoffset, "tz", 0) expected = utcdatetime + utcoffset got = datetime.fromtimestamp(timestamp, tz) self.assertEqual(expected, got.replace(tzinfo=None))
Example 30
Project: conifer Author: Rhizome-Conifer File: serializefakeredis.py License: Apache License 2.0 | 5 votes |
def load_redis_dict(self, obj): new_dict = {} redis_type = obj.get('__redis_type') exp = None for key, value in obj.items(): if key == '__redis_type': continue key = key.encode('utf-8') if redis_type == 'exp' and isinstance(value, list): if isinstance(value[1], int): exp = datetime.utcfromtimestamp(value[1]) else: exp = None value = value[0] if isinstance(value, str): value = value.encode('utf-8') elif isinstance(value, list): value = self.load_redis_set(value) elif isinstance(value, dict): value = self.load_redis_dict(value) if redis_type == 'exp': new_dict[key] = (value, exp) else: new_dict[key] = value if redis_type == 'zset': redis_dict = _ZSet() elif redis_type == 'hash': redis_dict = _Hash() elif redis_type == 'exp': redis_dict = _ExpiringDict() else: raise Exception('Invalid redis_dict: ' + str(redis_type)) redis_dict._dict = new_dict return redis_dict