Created
April 30, 2014 22:55
-
-
Save scy/790f32752a8d6b4124bf to your computer and use it in GitHub Desktop.
How to parse a string as UTC in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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