×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: Jason Kumpf
Added: Jun 6, 2013 1:39 AM
Views: 32
Tags: no tags
Get unixtime from date time with timezone info.
  1. from datetime import datetime, tzinfo, timedelta
  2. from time import mktime
  3. from calendar import timegm
  4.  
  5.  
  6. class UTC(tzinfo):
  7.     """UTC"""
  8.  
  9.     def utcoffset(self, dt):
  10.         return timedelta(0)
  11.  
  12.     def tzname(self, dt):
  13.         return "UTC"
  14.  
  15.     def dst(self, dt):
  16.         return timedelta(0)
  17.  
  18.  
  19. if __name__ == '__main__':
  20.     iso_string = '2013-06-05T15:19:10Z'
  21.  
  22.     datetime_obj = datetime.strptime(
  23.         iso_string, '%Y-%m-%dT%H:%M:%SZ'
  24.     )
  25.     datetime_obj = datetime_obj.replace(tzinfo=UTC())
  26.  
  27.     timestamp = timegm(datetime_obj.timetuple())
  28.  
  29.     print timestamp
  30.     print '-> %s' % datetime\
  31.         .utcfromtimestamp(timestamp).isoformat()