本文整理汇总了Python中datetime.utcfromtimestamp方法的典型用法代码示例。如果您正苦于以下问题:Python datetime.utcfromtimestamp方法的具体用法?Python datetime.utcfromtimestamp怎么用?Python datetime.utcfromtimestamp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类datetime
的用法示例。
在下文中一共展示了datetime.utcfromtimestamp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: julian_day
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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
示例2: _validate
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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)
示例3: test_utcfromtimestamp
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
def test_utcfromtimestamp(self):
import time
ts = time.time()
expected = time.gmtime(ts)
got = self.theclass.utcfromtimestamp(ts)
self.verify_field_equality(expected, got)
示例4: test_insane_utcfromtimestamp
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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)
示例5: test_negative_float_utcfromtimestamp
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
def test_negative_float_utcfromtimestamp(self):
d = self.theclass.utcfromtimestamp(-1.05)
self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))
示例6: test_utcnow
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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)
示例7: test_tzinfo_fromtimestamp
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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))
示例8: calendar_time
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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')
示例9: test_utcnow
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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)
示例10: test_tzinfo_fromtimestamp
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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))
示例11: conv_timestamp
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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
示例12: test_negative_float_utcfromtimestamp
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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))
示例13: test_utcnow
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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)
示例14: test_tzinfo_fromtimestamp
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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))
示例15: load_redis_dict
# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import utcfromtimestamp [as 别名]
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