我希望分析与天气数据相关的交通流量。交通数据有一个 UNIX 时间戳(又名纪元),但我在将时间戳(在天气数据中)转换为纪元时遇到了麻烦。问题是我在挪威,天气数据中的 UTC 时间戳与我不在同一时区(GMT+1)。
我最初的做法
我首先尝试将其转换为纪元并将数据视为 GMT+1 时区。然后我通过减去 UTC 和 GMT+1 之间的秒数差异进行补偿。
方法的问题
我首先意识到这种方法非常原始而且不是很优雅(实际上它充其量只是一个丑陋的黑客)。然而,这里最大的问题是 UTC 和 GMT+1 之间的差异不是恒定的(由于夏令时)。
题
是否有任何可靠的方法可以将 UTC 时间转换为 python 中的 UNIX 时间戳(考虑到我的机器处于 GMT+1)?时间戳采用以下格式:
Y-m-d HH:MM:SS
编辑:尝试过 rmunns 的解决方案:
def convert_UTC_to_epoch(timestamp):
tz_UTC = pytz.timezone('UTC')
time_format = "%Y-%m-%d %H:%M:%S"
naive_timestamp = datetime.datetime.strptime(timestamp, time_format)
aware_timestamp = tz_UTC.localize(naive_timestamp)
epoch = aware_timestamp.strftime("%s")
return (int) (epoch)
Run Code Online (Sandbox Code Playgroud)
这不能正常工作,如下所示:
#Current time at time of the edit is 15:55:00 UTC on June 9th 2014.
>>> diff …Run Code Online (Sandbox Code Playgroud) 我发现 datetime.timestamp() 函数在 Linux 和 Windows 上返回不同的值。这是一个简单的复制方法:
from datetime import date, time, datetime, timedelta
def main():
dt = datetime(2000, 1, 1)
edt = datetime(2006, 12, 31)
fname = 't1.csv'
f = open(fname, 'w')
f.write('date,timestamp\n')
while dt <= edt:
f.write('{0:%Y-%m-%d},{1:.0f}\n'.format(dt, dt.timestamp()))
dt += timedelta(days=1)
f.close()
return 0
Run Code Online (Sandbox Code Playgroud)
这是与 Windows 最后的不同部分:(Windows7 64 + Python3.4.3 64)
...
2006-10-30,1162180800
2006-10-31,1162267200
2006-11-01,1162353600
2006-11-02,1162440000
2006-11-03,1162526400
2006-11-04,1162612800
2006-11-05,1162699200
...
Run Code Online (Sandbox Code Playgroud)
以下是相应的 Linux 输出:(RedHat6 64 + Python 3.4.3 64)
...
2006-10-30,1162184400
2006-10-31,1162270800
2006-11-01,1162357200
2006-11-02,1162443600
2006-11-03,1162530000
2006-11-04,1162616400
2006-11-05,1162702800
...
Run Code Online (Sandbox Code Playgroud)
系统全部使用具有自动 …
python 2.6不支持timedelta.total_seconds().下面的代码适用于python 2.7.有谁知道如何转换为2.6中的等效?
谢谢
import datetime
timestamp = 1414270449
timestamp = int((datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') - datetime.datetime(1970,1,1)).total_seconds())
Run Code Online (Sandbox Code Playgroud)
抱歉,
我在下面有相同的代码.如何在python 2.6中使用实现total_seconds()
(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6`
import datetime
timestamp = '2014-10-24 00:00:00'
timestamp = int((datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') - datetime.datetime(1970,1,1)).total_seconds())
print timestamp
timestamp = '2014-10-24 00:00:00'
timestamp = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') - datetime.datetime(1970,1,1)
print timestamp
Run Code Online (Sandbox Code Playgroud)
谢谢
我试图从应用程序引擎中的数据存储区发送10条最新帖子.
这是数据库:
class Author(ndb.Model):
"""Sub model for representing an author."""
identity = ndb.StringProperty(indexed=False)
email = ndb.StringProperty(indexed=True)
class Message(ndb.Model):
"""A main model for representing an individual Message entry."""
author = ndb.StructuredProperty(Author)
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)
这是代码:
class Query(webapp2.RequestHandler):
def post(self):
message_name = self.request.get('db_name',
DEFAULT_KEY)
message_query = Message.query(ancestor=db_key(
message_name)).order(-Message.date)
messages = message_query.fetch(10)
items = []
for message in message_query:
items.append({'id': message.author.identity,
'email':message.author.email,
'content':message.content,
'date':message.date})
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(items))
Run Code Online (Sandbox Code Playgroud)
和错误消息:
TypeError: datetime.datetime(2015, 3, 21, 15, 43, 58, 248650) is not JSON …Run Code Online (Sandbox Code Playgroud) 我有一个这样的值:
var="11-feb-15:04:02:21"
Run Code Online (Sandbox Code Playgroud)
我需要转换为纪元时间:
我试过这个:
var.strftime('%s')
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
AttributeError: 'str' object has no attribute 'strftime'
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我