- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
有谁知道为什么 python 的 dateutil 在解析 datetime 字段时会反转 GMT 偏移量的符号?
显然这个特征是 known outcome不仅是 dateutil,还有其他解析函数。但这会导致不正确日期时间结果,除非应用了预处理 hack:
from dateutil import parser
jsDT = 'Fri Jan 02 2015 03:04:05.678910 GMT-0800'
python_datetime = parser.parse(jsDT)
print(python_datetime)
>>> 2015-01-02 03:04:05.678910+08:00
jsDT = 'Fri Jan 02 2015 03:04:05.678910 GMT-0800'
if '-' in jsDT:
jsDT = jsDT.replace('-','+')
elif '+' in jsDT:
jsDT = jsDT.replace('+','-')
python_datetime = parser.parse(jsDT)
print(python_datetime)
>>> 2015-01-02 03:04:05.678910-08:00
最佳答案
dateutil
似乎在这里使用了 POSIX 风格的符号。它与 Python 无关。其他软件也可以。来自 the tz database :
# We use POSIX-style signs in the Zone names and the output abbreviations,
# even though this is the opposite of what many people expect.
# POSIX has positive signs west of Greenwich, but many people expect
# positive signs east of Greenwich. For example, TZ='Etc/GMT+4' uses
# the abbreviation "GMT+4" and corresponds to 4 hours behind UT
# (i.e. west of Greenwich) even though many people would expect it to
# mean 4 hours ahead of UT (i.e. east of Greenwich).
The tz database is used almost everywhere .
例子:
$ TZ=Etc/GMT-8 date +%z
+0800
您可能期望不同的时区:
>>> from datetime import datetime
>>> import pytz
>>> pytz.timezone('America/Los_Angeles').localize(datetime(2015, 1, 2, 3, 4, 5, 678910), is_dst=None).strftime('%Y-%m-%d %H:%M:%S.%f %Z%z')
'2015-01-02 03:04:05.678910 PST-0800'
注意:PST
,不是GMT
。
虽然 dateutil
使用 POSIX 风格的标志,甚至是 PST
时区缩写:
>>> from dateutil.parser import parse
>>> str(parse('2015-01-02 03:04:05.678910 PST-0800'))
'2015-01-02 03:04:05.678910+08:00'
datetime.strptime()
在 Python 3 中“正确”解释它:
$ TZ=America/Los_Angeles python3
...
>>> from datetime import datetime
>>> str(datetime.strptime('2015-01-02 03:04:05.678910 PST-0800', '%Y-%m-%d %H:%M:%S.%f %Z%z'))
'2015-01-02 03:04:05.678910-08:00'
注意标志。
尽管由于 POSIX 风格的符号而造成混淆; dateutil
行为不太可能改变。请参见 dateutil
错误:"GMT+1" is parsed as "GMT-1"和@Lennart Regebro 的回复:
Parsing GTM+1 this way is actually a part of the Posix specification. This is therefore a feature, and not a bug.
看看如何TZ
environment variable is defined in the POSIX specification , glibc 使用 similar definition .
目前尚不清楚为什么 dateutil
使用 POSIX TZ
类语法来解释时间字符串中的时区信息。语法不完全相同,例如,POSIX 语法需要一个分号:hh[:mm[:ss]]
在您的输入中不存在的 utc 偏移量。
关于python - dateutil 反转时区偏移符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31078749/
我在玩dateutil module在 Python 2.7.3 中。我只是想使用: import dateutil dateutil.parser.parse("01-02-2013") 但我得到了
Java 文档中不清楚 DateUtils.ceiling 之间的区别是什么和 DateUtils.truncate是。 java文档错了吗?有人可以澄清一下吗? ceiling public sta
尝试使用 dateutil 解析未知格式的日期,但没有找到任何记录的方法? 代码: import dateutil print(dateutil.parser.parse("24.05.2017"))
我是 Python 新手。有人可以建议我如何处理错误 "ImportError: matplotlib requires dateutil"。我已经经历了类似的问题,但我已经按照那里提到的步骤下载了文
我在使用 python dateutil.zoneinfo 模块时遇到问题。注意: 损坏的 Ubuntu 机器是 ( Ubuntu 11.04 ) 工作的 Ubuntu 机器是 ( Ubuntu 11
我在这里遗漏了一些显然很简单的东西,但我不能说什么:我需要你的新眼光来告诉我:) $ pip install python-dateutil Requirement already satisfied
我试图在调用DateUtil.addDays的天数中使用一个变量,但是它不起作用。我是否缺少一些简单的东西,或者只是行不通? //Example: x = 10 //this int is the
如果我有以下字符串列表: a = ['Loc_RaffertytoLong_2004_02_21', 'Loc_RaffertytoLong_2004_02_22', 'Loc_Raffertyt
我正在尝试使用 apache-commons3 中的 DateUtils,但无法理解它所依赖的时区: Date date = DateUtils.truncate(date, Calendar.DAT
我正在使用 java DateUtils类 public boolean checkDate(String dateInString, String format){ try {
我在下面的示例代码中收到以下错误。我不确定导致错误的原因或原因,因为此代码过去运行良好。我正在使用 Python 2.7 AttributeError: 'module' object has no
r = [['21-09-1995', 3], ['22-11-1995', 2] , ['07-01-1988', 6], ['test', 4], ['12-12-2001', 5]] 有谁知道如
我使用 dateutil 解析日期得到以下堆栈跟踪: >>> dateutil.parser.parse('Sept 18 2014', fuzzy=True).date() Traceback (m
Python dateutil 正确解析字符串,但字符串的秒部分除外。 In [1]: from dateutil import parser In [2]: parser.parse("05/09/
我正在尝试解析从电子邮件标题中获取的以下日期字符串: from dateutil import parser d1 = parser.parse('Tue, 28 Jun 2011 01:46:52
我正在尝试将字符串类型的参数转换为日期时间。我正在使用 dateUtil 库 from dateutil import parser myDate_string="2001/9/1 12:00:0
我试图在执行 preparedStatement 之前更改服务器的时间。服务器时间是 EDT,我想在 preparedStatement 中使用它之前添加 6 个小时。 目前我收到这个错误: Date
我正在尝试查看日期列表是否为有效日期。我正在使用 dateutil 库,但我得到了奇怪的结果。例如,当我尝试以下操作时: import dateutil.parser as parser x = '1
有谁知道为什么 python 的 dateutil 在解析 datetime 字段时会反转 GMT 偏移量的符号? 显然这个特征是 known outcome不仅是 dateutil,还有其他解析函数
我正在使用 sixohsix 库从 Twitter API 解析一些推文数据。我正在尝试将推文的日期转换为我的语言环境: from pytz import timezone from dateutil
我是一名优秀的程序员,十分优秀!