【问题标题】:In Python, compare a specific time on a day with the current time in epoch在 Python 中,将一天中的特定时间与纪元中的当前时间进行比较
【发布时间】:2016-05-18 11:03:45
【问题描述】:

我正在尝试将当前时间 (A) 与当天的某个时间 (B) 进行比较。随后,根据该比较的结果,我想返回特定时间的 POSIX 时间,明天 (C) 或后天 (D)。

举例说明:

假设我们的当前时间 (A) 是 12:00。 我想将其与 (B) 进行比较,即今天 20:00。

由于 A

另一个例子:

现在假设当前时间 (A) 是 21:00。 我仍然想将其与 (B) 进行比较,即今天 20:00。

由于 A > B,我想在明天 之后的第二天 13:00 返回,再次格式化为 POSIX 时间。

我一直在尝试使用 timedatetime 库来完成这项工作,但是在使用 time 时我很难找到 B 而在使用 datetime 时我找不到方法将 C 和 D 作为 POSIX 时间返回。

我是否正确确定了我应该使用哪些库?如果是,我缺少什么?

【问题讨论】:

    标签: python datetime time


    【解决方案1】:

    有两个问题:

    • 根据相对于20:00 的当前时间确定您是否需要“明天13:00”“后天13:00”
    • 将结果例如 "13:00 tomorrow" 转换为 POSIX 时间

    第一个很简单:

    #!/usr/bin/env python
    import datetime as DT
    
    current_time = DT.datetime.now()
    one_or_two = 1 if current_time.time() < DT.time(20, 0) else 2
    target_date = current_time.date() + DT.timedelta(days=one_or_two)
    target_time = DT.datetime.combine(target_date, DT.time(13, 0))
    

    注意:00:00 被认为小于20:00。您可能希望使用时间间隔,例如,20:00-8:008:00-20:00,到 find out whether the current time is in between


    第二个问题与How do I convert local time to UTC in Python? 基本相同。在一般情况下,没有确切的答案,例如,相同的本地时间可能出现两次,也可能完全丢失——使用什么答案取决于具体的应用程序。在我对python converting string in localtime to UTC epoch timestamp 的回复中查看更多详细信息。

    考虑到从现在到目标时间之间可能的 DST 转换或本地 UTC 偏移的其他变化(例如,“后天”),以获得正确的结果:

    import pytz    # $ pip install pytz
    import tzlocal # $ pip install tzlocal
    
    epoch = DT.datetime(1970,1,1, tzinfo=pytz.utc)
    local_timezone = tzlocal.getlocalzone()
    timezone_aware_dt = local_timezone.localize(target_time, is_dst=None)
    posix_time = (timezone_aware_dt - epoch).total_seconds()
    

    注意:is_dst=None 用于断言给定的本地时间存在且明确,例如,在您的本地时间 13:00 没有 DST 转换。

    如果time.mktime() 可以访问您平台上的历史时区数据库(或者您只是不关心本地 UTC 偏移量的变化),那么您可以仅使用 stdlib 找到“纪元”时间:

    import time
    
    unix_time = time.mktime(target_time.timetuple())
    

    您可以在Find if 24 hrs have passed between datetimes - Python 中阅读有关它何时失败以及如何解决它的更多详细信息。

    或者更多关于在 Python 中查找 POSIX 时间戳的信息可以在Converting datetime.date to UTC timestamp in Python 中找到。

    【讨论】:

      【解决方案2】:
      import datetime
      
      A = datetime.datetime.now()
      # A = A.replace(hour=21, minute=5)  # This line simlulates "after 21:00"
      B = A.replace(hour=20, minute=0, second=0, microsecond=0)  # 20:00 today
      
      if A < B:
          print A.replace(hour=13, minute=0, second=0, microsecond=0) + datetime.timedelta(days=1)  # 13:00 tomorrow
      else:
          print A.replace(hour=13, minute=0, second=0, microsecond=0) + datetime.timedelta(days=2)  # 13:00 the day after
      

      在 Python 3 中你可以返回

      X.timestamp()
      

      在 Python 2 中你可以使用

      def timestamp(dt):
          return (dt - datetime.datetime(1970, 1, 1)).total_seconds()
      
      timestamp(X)
      

      【讨论】:

      • 非常接近,但我认为最后一步应该是给B.replace(hour=13)加1或2天,而不是A.replace(hours=13)。这将为您提供13:00:00 的时间,而不是A 中的任何分钟、秒和微秒值。
      • 现在试一试,我确实注意到 Python 2 部分中似乎有一个额外的 )
      • 这几乎可以工作,但并不完全。除非我将其更改为 A = A... 它不会更新 A,这意味着它不起作用。一旦一切正常,我会立即接受答案。
      • 我不关注...A = A...?您需要在哪里更新A
      • timestamp() 函数如果输入是本地时间(如问题所示)是错误的,除非本地时区是 utc。
      猜你喜欢
      • 1970-01-01
      • 2016-03-02
      • 2010-09-19
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 2021-10-03
      相关资源
      最近更新 更多