python中的Dateutil解析错误返回错误的值 [英] Dateutil parse bug in python returns the wrong value

查看:67
本文介绍了python中的Dateutil解析错误返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了许多可能的方法来解析 python 时间.使用解析似乎是唯一可行的方法.虽然尝试使用 datetime.strptime 会导致错误,因为 %z 没有使用 python 2.7.但是使用 parse.parse 会错误地识别时区.

I have looked at many possible ways to parse python times. Using parse seems link the only method that should work. While trying to use datetime.strptime causes an error because %z does not work with python 2.7. But using parse.parse incorrectly recognizes the time zone.

我解析了 Fri Nov 9 09:04:02 2012 -0500Fri Nov 9 09:04:02 2012 -0800 并在Unix 时间.1352480642

I parse both Fri Nov 9 09:04:02 2012 -0500 and Fri Nov 9 09:04:02 2012 -0800 and get the exact same timestamp in unix time. 1352480642

  • 我的python 2.7.10版本
  • 我的 dateutil 1.5 版本

这是我运行测试的代码.

Here is my code that runs the test.

#!/usr/bin/python
import time
from dateutil import parser

def get_timestamp(time_string):
    timing = parser.parse(time_string)
    return time.mktime(timing.timetuple())

test_time1 = "Fri Nov 9 09:04:02 2012 -0500"
test_time2 = "Fri Nov 9 09:04:02 2012 -0800"
print get_timestamp(test_time1)
print get_timestamp(test_time2)

输出

1352480642.0
1352480642.0

预期输出

1352469842.0
1352480642.0

推荐答案

这与解析器无关,您将仅从 mktime() 中看到相同的行为,因为 datetime.timetuple() 没有任何时区偏移信息,mktime()localtime 的倒数.您可以通过在调用 timetuple() 之前将其转换为 localtime 来纠正此问题:

This has nothing to do with the parser, you'll see the same behavior just from mktime() alone, since datetime.timetuple() doesn't have any time zone offset information, and mktime() is the inverse of localtime. You can correct this by converting it to localtime before calling timetuple():

from time import mktime
from datetime import datetime
from dateutil import tz

dt_base = datetime(2012, 11, 9, 9, 4, 2)

dt_est = dt_base.replace(tzinfo=tz.tzoffset('EST', -5 * 3600))
dt_pst = dt_base.replace(tzinfo=tz.tzoffset('PST', -8 * 3600))

def print_mktime(dt):
    print(mktime(dt.timetuple()))

# Run in UTC
print_mktime(dt_est)   # 1352469842.0
print_mktime(dt_pst)   # 1352469842.0

# Convert to local time zone first first
print_mktime(dt_est.astimezone(tz.tzlocal())) # 1352469842.0
print_mktime(dt_pst.astimezone(tz.tzlocal())) # 1352480642.0

请注意,time() 文档上有一个图表代码>(python 2.x 文档)告诉你如何在这些表示之间进行转换:

Note that there is a chart on the documentation for time() (python 2.x docs) that tells you how to convert between these representations:

From                        To                           Use
---------------------------------------------------------------------------
seconds since the epoch   | struct_time in UTC        |  gmtime()
seconds since the epoch   | struct_time in local time |  localtime()
struct_time in UTC        | seconds since the epoch   |  calendar.timegm()
struct_time in local time | seconds since the epoch   |  mktime()

我个人的偏好是将解析的日期转换为 UTC,在这种情况下 calendar.timegm() 将是合适的函数:

My personal preference would be to convert the parsed date to UTC, in which case calendar.timegm() would be the appropriate function:

from calendar import timegm
def print_timegm(dt):
    print(timegm(dt.timetuple()))

print_timegm(dt_est.astimezone(tz.tzutc())) # 1352469842.0
print_timegm(dt_pst.astimezone(tz.tzutc())) # 1352480642.0

这篇关于python中的Dateutil解析错误返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆