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


Python datetime.fromtimestamp方法代码示例

本文整理汇总了Python中datetime.fromtimestamp方法的典型用法代码示例。如果您正苦于以下问题:Python datetime.fromtimestamp方法的具体用法?Python datetime.fromtimestamp怎么用?Python datetime.fromtimestamp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在datetime的用法示例。


在下文中一共展示了datetime.fromtimestamp方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: scheduled_times

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def scheduled_times(self, earliest_time='now', latest_time='+1h'):
        """Returns the times when this search is scheduled to run.

        By default this method returns the times in the next hour. For different
        time ranges, set *earliest_time* and *latest_time*. For example,
        for all times in the last day use "earliest_time=-1d" and
        "latest_time=now".

        :param earliest_time: The earliest time.
        :type earliest_time: ``string``
        :param latest_time: The latest time.
        :type latest_time: ``string``

        :return: The list of search times.
        """
        response = self.get("scheduled_times",
                            earliest_time=earliest_time,
                            latest_time=latest_time)
        data = self._load_atom_entry(response)
        rec = _parse_atom_entry(data)
        times = [datetime.fromtimestamp(int(t))
                 for t in rec.content.scheduled_times]
        return times 
开发者ID:remg427,项目名称:misp42splunk,代码行数:25,代码来源:client.py

示例2: value_from_message

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def value_from_message(self, message):
        """Convert DateTimeMessage to a datetime.

        Args:
          A DateTimeMessage instance.

        Returns:
          A datetime instance.
        """
        message = super(DateTimeField, self).value_from_message(message)
        if message.time_zone_offset is None:
            return datetime.datetime.utcfromtimestamp(
                message.milliseconds / 1000.0)

        # Need to subtract the time zone offset, because when we call
        # datetime.fromtimestamp, it will add the time zone offset to the
        # value we pass.
        milliseconds = (message.milliseconds -
                        60000 * message.time_zone_offset)

        timezone = util.TimeZoneOffset(message.time_zone_offset)
        return datetime.datetime.fromtimestamp(milliseconds / 1000.0,
                                               tz=timezone) 
开发者ID:google,项目名称:apitools,代码行数:25,代码来源:message_types.py

示例3: value_from_message

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def value_from_message(self, message):
    """Convert DateTimeMessage to a datetime.

    Args:
      A DateTimeMessage instance.

    Returns:
      A datetime instance.
    """
    message = super(DateTimeField, self).value_from_message(message)
    if message.time_zone_offset is None:
      return datetime.datetime.utcfromtimestamp(message.milliseconds / 1000.0)

    # Need to subtract the time zone offset, because when we call
    # datetime.fromtimestamp, it will add the time zone offset to the
    # value we pass.
    milliseconds = (message.milliseconds -
                    60000 * message.time_zone_offset)

    timezone = util.TimeZoneOffset(message.time_zone_offset)
    return datetime.datetime.fromtimestamp(milliseconds / 1000.0,
                                           tz=timezone) 
开发者ID:google,项目名称:protorpc,代码行数:24,代码来源:message_types.py

示例4: value_to_message

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def value_to_message(self, value):
    value = super(DateTimeField, self).value_to_message(value)
    # First, determine the delta from the epoch, so we can fill in
    # DateTimeMessage's milliseconds field.
    if value.tzinfo is None:
      time_zone_offset = 0
      local_epoch = datetime.datetime.utcfromtimestamp(0)
    else:
      time_zone_offset = util.total_seconds(value.tzinfo.utcoffset(value))
      # Determine Jan 1, 1970 local time.
      local_epoch = datetime.datetime.fromtimestamp(-time_zone_offset,
                                                     tz=value.tzinfo)
    delta = value - local_epoch

    # Create and fill in the DateTimeMessage, including time zone if
    # one was specified.
    message = DateTimeMessage()
    message.milliseconds = int(util.total_seconds(delta) * 1000)
    if value.tzinfo is not None:
      utc_offset = value.tzinfo.utcoffset(value)
      if utc_offset is not None:
        message.time_zone_offset = int(
            util.total_seconds(value.tzinfo.utcoffset(value)) / 60)

    return message 
开发者ID:google,项目名称:protorpc,代码行数:27,代码来源:message_types.py

示例5: test_fromtimestamp

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def test_fromtimestamp(self):
        import time

        # Try an arbitrary fixed value.
        year, month, day = 1999, 9, 19
        ts = time.mktime((year, month, day, 0, 0, 0, 0, 0, -1))
        d = self.theclass.fromtimestamp(ts)
        self.assertEqual(d.year, year)
        self.assertEqual(d.month, month)
        self.assertEqual(d.day, day) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:12,代码来源:test_datetime.py

示例6: test_insane_fromtimestamp

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def test_insane_fromtimestamp(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.fromtimestamp,
                              insane) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:10,代码来源:test_datetime.py

示例7: test_today

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def test_today(self):
        import time

        # We claim that today() is like fromtimestamp(time.time()), so
        # prove it.
        for dummy in range(3):
            today = self.theclass.today()
            ts = time.time()
            todayagain = self.theclass.fromtimestamp(ts)
            if today == todayagain:
                break
            # There are several legit reasons that could fail:
            # 1. It recently became midnight, between the today() and the
            #    time() calls.
            # 2. The platform time() has such fine resolution that we'll
            #    never get the same value twice.
            # 3. The platform time() has poor resolution, and we just
            #    happened to call today() right before a resolution quantum
            #    boundary.
            # 4. The system clock got fiddled between calls.
            # In any case, wait a little while and try again.
            time.sleep(0.1)

        # It worked or it didn't.  If it didn't, assume it's reason #2, and
        # let the test pass if they're within half a second of each other.
        if today != todayagain:
            self.assertAlmostEqual(todayagain, today,
                                   delta=timedelta(seconds=0.5)) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:30,代码来源:test_datetime.py

示例8: test_microsecond_rounding

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def test_microsecond_rounding(self):
        # Test whether fromtimestamp "rounds up" floats that are less
        # than one microsecond smaller than an integer.
        self.assertEqual(self.theclass.fromtimestamp(0.9999999),
                         self.theclass.fromtimestamp(1)) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:7,代码来源:test_datetime.py

示例9: test_negative_float_fromtimestamp

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def test_negative_float_fromtimestamp(self):
        # The result is tz-dependent; at least test that this doesn't
        # fail (like it did before bug 1646728 was fixed).
        self.theclass.fromtimestamp(-1.05) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:6,代码来源:test_datetime.py

示例10: test_tzinfo_fromtimestamp

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [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

示例11: test_today

# 需要导入模块: import datetime [as 别名]
# 或者: from datetime import fromtimestamp [as 别名]
def test_today(self):
        import time

        # We claim that today() is like fromtimestamp(time.time()), so
        # prove it.
        for dummy in range(3):
            today = self.theclass.today()
            ts = time.time()
            todayagain = self.theclass.fromtimestamp(ts)
            if today == todayagain:
                break
            # There are several legit reasons that could fail:
            # 1. It recently became midnight, between the today() and the
            #    time() calls.
            # 2. The platform time() has such fine resolution that we'll
            #    never get the same value twice.
            # 3. The platform time() has poor resolution, and we just
            #    happened to call today() right before a resolution quantum
            #    boundary.
            # 4. The system clock got fiddled between calls.
            # In any case, wait a little while and try again.
            time.sleep(0.1)

        # It worked or it didn't.  If it didn't, assume it's reason #2, and
        # let the test pass if they're within half a second of each other.
        self.assertTrue(today == todayagain or
                        abs(todayagain - today) < timedelta(seconds=0.5)) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:29,代码来源:test_datetime.py


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