简体   繁体   English

比较Python中的两个时间戳字符串

[英]Comparing two timestamp strings in Python

I have JSON file which has timestamps in it.The timestamp in the format shown below. 我有一个带有时间戳的JSON文件。时间戳的格式如下所示。

timestamp1 Fri Mar 27 15:25:24 NZDT 2015 Fri Mar 27 15:25:24 NZDT 2015

timestamp2 Fri Mar 27 15:23:01 NZDT 2015 timestamp2 Fri Mar 27 15:23:01 NZDT 2015

Between the above two timestamps above timestamp1 is at a later time than timestamp2. 在以上两个时间戳之间,timestamp1之上的时间晚于timestamp2。

How can I compare these timestamps in a python program,so that among hundreds of timestamps extracted from a JSON file I get the latest timestamp.I have written the program to extract the timestamp people.I don't know how to compare them. 我该如何在python程序中比较这些时间戳,以便在从JSON文件提取的数百个时间戳中获得最新的时间戳。我编写了该程序来提取时间戳人。

# Import the os module, for the os.walk function
import os
import json

# Set the directory you want to start from
rootDir = '/Workspace/events_parts'
for dirName, subdirList, fileList in os.walk(rootDir):
    print('Found directory: %s' % dirName)
    for fname in fileList:
        print('\t%s' % fname)
        fname='events_parts/'+fname
        with open(fname, 'r+') as f:
            json_data = json.load(f)
            ts = json_data['Timestamp']
            print(ts)

You need to convert your timestamp strings into datetime objects. 您需要将时间戳字符串转换为datetime对象。 Normally, I'd recommend that you use the strptime . 通常,我建议您使用strptime However, you have a timezone in your string ( NZDT ) and the documentation says: 但是,您的字符串( NZDT )中有一个时区, 文档中说:

Support for the %Z directive is based on the values contained in tzname and whether daylight is true. 对%Z指令的支持基于tzname中包含的值以及daylight是否为true。 Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones). 因此,它是特定于平台的,除了识别始终已知的UTC和GMT(并被视为非夏时制时区)。

Instead, we'll utilize the python-dateutil package . 相反,我们将使用python-dateutil

pip install python-dateutil

From here, we can utilize the parse function to get a datetime object. 从这里,我们可以利用解析函数来获取日期时间对象。 With this, you can perform any comparisons you need. 这样,您可以执行所需的任何比较。

>>> from dateutil.parser import parse
>>> t1 = "Fri Mar 27 15:25:24 NZDT 2015"
>>> t2 = "Fri Mar 27 15:23:01 NZDT 2015"
>>> d1 = parse(t1)
>>> d2 = parse(t2)
>>> d1 > d2
True

If New Zealand Daylight time (NZDT) is common for all your data then you can use this method. 如果所有数据都使用新西兰夏令时间(NZDT),则可以使用此方法。 (Only if NZDT is common) (仅在NZDT为通用的情况下)

#for python3
import time

timestamp1 = 'Fri Mar 27 15:25:24 NZDT 2015'
timestamp2 = 'Fri Mar 27 15:23:01 NZDT 2015'

pattern = '%a %b %d %H:%M:%S NZDT %Y'

epoch_timestamp1 = int(time.mktime(time.strptime(timestamp1, pattern)))
epoch_timestamp2 = int(time.mktime(time.strptime(timestamp2, pattern)))


if epoch_timestamp1 > epoch_timestamp2:
  print ("Timestamp1 is the latest")
else:
  print ("Timestamp2 is the latest")

Output 输出量

Timestamp1 is the latest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM