0

I have many strings of dates and times (or both), like these:

'Thu Jun 18 19:30:21 2015'
'21:07:52'

I want to convert these times to the proper datetime format while also changing the timezone to UTC. The current timezone is 4 hours behind UTC. Is there a way that I can tell python to add 4 hours while converting the formats? Can it also take care of the date in UTC such that when the hour goes past 24 the date changes and time resets?

I will ultimately be inserting these into a mysql table into fields with the 'datetime' and 'time' data type, but they all need to be in UTC.

curious_cosmo
  • 1,184
  • 1
  • 18
  • 36
  • I think this could help. It's the opposite of what you need. It's parsing dates with timezone, but there's also the use of the `tzinfo` parameter in the use of datetime.datetime after parsing: https://stackoverflow.com/questions/1101508/how-to-parse-dates-with-0400-timezone-string-in-python – clarete Aug 31 '17 at 21:07

1 Answers1

0

I would approach this with time.strptime() to parse the source time string, time.mktime() to convert the resulting time vector into an epoch time (seconds since 1970-01-01 00:00:00), and time.strftime() to format the time as you like.

For the timezone adjustment, you could add 4*3600 to the epoch time value or, more generally, append a timezone string to the source and use %Z to parse it.

Tom Barron
  • 1,554
  • 18
  • 23