To use an online time string, for example, obtained from an online service ( http://just-the-time.appspot.com/ ), it can be read and converted to datetime.datetime format using urllib2 and datetime.datetime:
import urllib2 from datetime import datetime def getOnlineUTCTime(): webpage = urllib2.urlopen("http://just-the-time.appspot.com/") internettime = webpage.read() OnlineUTCTime = datetime.strptime(internettime.strip(), '%Y-%m-%d %H:%M:%S') return OnlineUTCTime
or very compact (less readable)
OnlineUTCTime=datetime.strptime(urllib2.urlopen("http://just-the-time.appspot.com/").read().strip(), '%Y-%m-%d %H:%M:%S')
little exercise:
Comparing your UTC time with online time:
print(datetime.utcnow() - getOnlineUTCTime())
(remember that processing time is also included)
source share