【问题标题】:What would be the best way to compute OOZIE timestamp difference?计算 OOZIE 时间戳差异的最佳方法是什么?
【发布时间】:2020-11-13 13:05:23
【问题描述】:

我有 2 个来自 OOZIE 的时间戳,格式如下:

2019-07-20T16:34:45.000Z2020-08-20T16:20:15.000Z

如何以秒/分钟/天为单位找到时差?哪个python库可以提供帮助?有什么建议吗?

【问题讨论】:

    标签: python python-3.x time


    【解决方案1】:

    你可以使用 datetime 模块。

    首先,使用 strptime 将每个时间戳转换为日期。它的一般格式是:

    from datetime import datetime
    date1 = datetime.strptime("2019-07-20T16:34:45.000Z","%Y-%m-%dT%H:%M:%S.%fZ")
    date2 = datetime.strptime("2020-08-20T16:20:15.000Z","%Y-%m-%dT%H:%M:%S.%fZ")
    

    (注意:第二个参数可能不准确,具体取决于 OOZIE 的具体情况)。 有关如何构造格式/使用 strptime 的信息,请参阅 thisthis 了解 strptime。

    然后,您可以使用t_diff = date1-date2。这将返回一个 timedelta 对象,具有年、月、周、日等属性。微秒)。它还有一个内置的.total_seconds() 方法。 希望这有帮助!

    【讨论】:

      【解决方案2】:

      我不知道任何图书馆,但你可以自己做。 我正在分享一个可能有效的代码。

      time_stamp1 = input("Time stamp1")
      time_stamp2 = input("Time stamp2")
      yrs = int(time_stamp1[:4]) - int(time_stamp2[:4])
      mnths = int(time_stamp1[5:7]) - int(time_stamp2[5:7])
      days = int(time_stamp1[8:10]) - int(time_stamp1[8:10])
      hrs = int(time_stamp1[11:13]) - int(time_stamp2[11:13])
      min = int(time_stamp1[14:16]) - int(time_stamp1[14:16])
      sec = int(time_stamp1[17:19]) - int(time_stamp1[17:19])
      ms = int(time_stamp1[20:23]) - int(time_stamp1[20:23])
      print(yrs, mnths, days)
      print(hrs, min, sec, ms)
      

      如果您不想知道时间戳 2 是否早于时间戳 1,则可以使用 abs() 函数,反之亦然。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-26
        • 1970-01-01
        • 2011-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多