相关疑难解决方法(0)

Python strptime()和时区?

我有一个使用IPDDump创建的Blackberry IPD备份的CSV转储文件.这里的日期/时间字符串看起来像这样(EST澳大利亚时区在哪里):

Tue Jun 22 07:46:22 EST 2010
Run Code Online (Sandbox Code Playgroud)

我需要能够在Python中解析这个日期.起初,我尝试使用strptime()datettime中的函数.

>>> datetime.datetime.strptime('Tue Jun 22 12:10:20 2010 EST', '%a %b %d %H:%M:%S %Y %Z')
Run Code Online (Sandbox Code Playgroud)

但是,由于某种原因,返回的datetime对象似乎没有任何tzinfo关联它.

我在这个页面上读到的显然是datetime.strptime默默地丢弃了tzinfo,但是,我检查了文档,我找不到任何有关此处记录的效果.

我已经能够使用第三方Python库dateutil来解析日期,但是我仍然很好奇我是如何使用内置strptime()错误的?有什么方法可以strptime()很好地与时区玩吗?

python timezone datetime

141
推荐指数
4
解决办法
14万
查看次数

使用strptime将带有offset的时间戳转换为datetime obj

我正在尝试使用strptime方法将格式为"2012-07-24T23:14:29-07:00"的时间戳转换为python中的datetime对象.问题在于结束时的时间偏移(-07:00).没有偏移我可以成功做到

time_str = "2012-07-24T23:14:29"

time_obj=datetime.datetime.strptime(time_str,'%Y-%m-%dT%H:%M:%S')
Run Code Online (Sandbox Code Playgroud)

但随着我尝试的抵消

time_str = "2012-07-24T23:14:29-07:00"

time_obj=datetime.datetime.strptime(time_str,'%Y-%m-%dT%H:%M:%S-%z').
Run Code Online (Sandbox Code Playgroud)

但是它给出了一个Value错误,说"z"是一个糟糕的指令.

任何解决方案的想法?

python datetime timestamp rfc3339

49
推荐指数
3
解决办法
3万
查看次数

在Python中将ISO 8601日期时间转换为秒

我想把两次加在一起.ISO 8601时间戳是'1984-06-02T19:05:00.000Z',我想将其转换为秒.我尝试使用Python模块iso8601,但它只是一个解析器.

有什么建议?

python time iso8601 rfc3339 python-2.7

19
推荐指数
2
解决办法
2万
查看次数

Converting a formatted time string to milliseconds

I am trying to convert '2015-09-15T17:13:29.380Z' to milliseconds.

At first I used:

time.mktime(
  datetime.datetime.strptime(
    "2015-09-15T17:13:29.380Z",
    "%Y-%m-%dT%H:%M:%S.%fZ"
  ).timetuple()
)
Run Code Online (Sandbox Code Playgroud)

I got 1442330009.0 - with no microseconds. I think time.mktime rounds the number to the nearest second.

In the end I did:

origTime = '2015-09-15T17:13:29.380Z'
tupleTime = datetime.datetime.strptime(origTime, "%Y-%m-%dT%H:%M:%S.%fZ")
microsecond = tupleTime.microsecond
updated = float(time.mktime(tupleTime.timetuple())) + (microsecond * 0.000001)
Run Code Online (Sandbox Code Playgroud)

Is there a better way to do this and how do I work with the timezone?

python time datetime

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

python ×4

datetime ×3

rfc3339 ×2

time ×2

iso8601 ×1

python-2.7 ×1

timestamp ×1

timezone ×1