当前位置: 首页>>代码示例>>Python>>正文


Python datetime.utcfromtimestamp方法代码示例

本文整理汇总了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 
开发者ID:s-bear,项目名称:sun-position,代码行数:23,代码来源:sunposition.py

示例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) 
开发者ID:zmap,项目名称:zschema,代码行数:24,代码来源:leaves.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:9,代码来源:test_datetime.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:10,代码来源:test_datetime.py

示例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)) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:5,代码来源:test_datetime.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:15,代码来源:test_datetime.py

示例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)) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:36,代码来源:test_datetime.py

示例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') 
开发者ID:s-bear,项目名称:sun-position,代码行数:11,代码来源:sunposition.py

示例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) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:15,代码来源:test_datetime.py

示例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)) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:36,代码来源:test_datetime.py

示例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 
开发者ID:economicnetwork,项目名称:archon,代码行数:41,代码来源:models.py

示例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)) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:8,代码来源:test_datetime.py

示例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) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:15,代码来源:test_datetime.py

示例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)) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:36,代码来源:test_datetime.py

示例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 
开发者ID:Rhizome-Conifer,项目名称:conifer,代码行数:42,代码来源:serializefakeredis.py


注:本文中的datetime.utcfromtimestamp方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。