格式說明 : http://www.ietf.org/rfc/rfc3339.txt

因為寫了 google calendar 的應用程式 , 只吃這個時間格

不過了解後才發現這應用起來還蠻不錯用

格式為:

年-月-日T時:分:秒+(-)時區

2013-09-12T22:50:20+08:00

我想了一下為什麼Google 要用這種格式 , 大概是因為可以反推回去標準時間

再依照你指定event 的時區去推出正確的時間

由於 這次是寫python 的應用程式 , 就留點python 語法


# -*- coding: utf-8 –*-
#ntplib (取得網路時間) , rfc3339 需另外安裝 module
import ntplib
import rfc3339
import os
import time
import datetime
c=ntplib.NTPClient()
response = c.request('pool.ntp.org')
ts=response.tx_time

print "系統時間:"
print datetime.datetime.now()

print "rfc3339格式 系統時間:"
print rfc3339.rfc3339(datetime.datetime.now())

print "網路時間:"
print time.ctime(ts)

print "rfc3339格式 網路時間:"
print rfc3339.rfc3339(ts)

print "rfc3339格式網路時間(不使用時區):"
print rfc3339.rfc3339(datetime.datetime.utcfromtimestamp(ts),use_system_timezone=False)

結果為:

系統時間:
2013-09-13 00:19:14.411000


rfc3339格式 系統時間:
2013-09-13T00:19:14+08:00


網路時間:
Fri Sep 13 00:19:19 2013


rfc3339格式 網路時間:
2013-09-13T00:19:19+08:00


rfc3339格式網路時間(不使用時區):
2013-09-12T16:19:19+00:00

 

即使不用使用時區的方式 , 也會把時間回推到 +00:00

所以只要丟出的時間 + 時區是正確的 , 這樣丟給 google , 也不會算錯時間

相关文章