2012-01-26 75 views
3

我無法從Facebook以UTC格式結束時間獲取事件啓動。將Facebook事件開始/結束時間轉換爲UTC

我使用Facebook的Python庫,我運行這些查詢檢索事件:

fb_events = graph.get_connections(fb_user["id"], "events", args=["date_format=U"]) 
for item in fb_events['data']: 
    curr_event = graph.get_object(item['id'], args=["date_format=U"]) 

代碼的最後一行是我真正得到包含開始和結束時間的事件。 arg:"date_format=U"應該在unix時間中指定時間格式,該時間格式已經是UTC,但是,該代碼在當地時間仍然會返回正常的Facebook時間戳。

如何以UTC時間獲取此信息,或者在某個時間跨時區保持一致?

謝謝!

+0

Unix時間不UTC的另一個例子,但是隻要沒有閏秒,他們就會跟蹤對方。 –

回答

2

下可能會幫助你做轉換:

import pytz, datetime, time 
import os 

originalTimeStamp = os.stat("/tmp/file-from-us-west-coast").st_mtime 

# prints e.g. 2010-03-31 13:01:18 
print "original:",datetime.datetime.fromtimestamp(originalTimeStamp) 

# re-interpret 
originalTimeZone = "America/Los_Angeles" 
targetTimeZone = "Europe/Paris" 

newTimeStamp = pytz.timezone(originalTimeZone).localize(datetime.datetime.fromtimestamp(originalTimeStamp)).astimezone(pytz.timezone(targetTimeZone)) 

# prints e.g. 2010-03-31 22:01:18+02:00 
print "new:  ",newTimeStamp 

# convert back to seconds since epoch 
newTimeStamp = time.mktime(newTimeStamp.timetuple()) 

# print time difference in hours 
print (newTimeStamp - originalTimeStamp)/3600.0 

請參閱此鏈接瞭解更多信息:http://pytz.sourceforge.net/

代碼

from datetime import * 
from pytz import timezone 
import pytz 

[...] 

if (not isinstance(pacificTime, int)): 
    pacificTime = int(pacificTime) 
originalTimeZone = "America/Los_Angeles" 
print datetime.fromtimestamp(pacificTime, pytz.timezone(originalTimeZone)) 
相關問題