Skip to content

Instantly share code, notes, and snippets.

@scy
Created April 30, 2014 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scy/790f32752a8d6b4124bf to your computer and use it in GitHub Desktop.
Save scy/790f32752a8d6b4124bf to your computer and use it in GitHub Desktop.
How to parse a string as UTC in Python
import datetime
class UTC(datetime.tzinfo):
ZERO = datetime.timedelta(0)
def utcoffset(self, dt):
return self.ZERO
def tzname(self, dt):
return 'UTC'
def dst(self, dt):
return self.ZERO
# s = {'year':'2014', 'month':'05', 'day':'01', 'time':'005359'}
def make_datetime(s):
dt = datetime.datetime.strptime('%(year)s-%(month)s-%(day)s %(time)s' % s, '%Y-%m-%d %H%M%S')
dt = dt.replace(tzinfo=UTC())
print dt.strftime('%z')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment