如何将ISO格式时间戳转换为UTC并将其与ISO格式时间戳

时间:2016-08-19 02:03:08

标签: python-2.7 date datetime

我正在编写一个程序,用户必须以ISO格式输入日期和时间。

2010-12-03T23:10:00

用户将根据时区输入日期和时间。我需要将此日期和时间与另一个ISO格式的日期和时间进行比较,该日期和时间将采用UTC格式。

如何将用户输入的日期和时间首先转换为UTC?

我可以使用以下代码实现它:

def local_to_utc(local_time):
    fmt = '%Y-%m-%dT%H:%M:%SZ'
    conv2epoch = time.mktime(time.strptime(local_time, fmt))
    epoch2utc = datetime.utcfromtimestamp(conv2epoch).strftime(fmt)
    return epoch2utc

dateutil.parser.parse(local_to_utc('2010-12-03T23:10:00Z'))

有没有更简单的方法?

0 个答案:

没有答案