3

I don't have the name of the Time Zone, only have an offset value, like +0400.

I have the datetime string in UTC: like 2014-01-07T09:29:35Z.

I want a string in local time, like 2014-01-07T13:29:35.

How to do this?

2
  • 1
    @Thrustmaster Please read the question which says "I don't have the name for the TimeZone". Commented Jan 16, 2014 at 6:46
  • @ATOzTOA Do you have an internet connection ? Commented Jan 16, 2014 at 7:04

2 Answers 2

1

You can write a function to convert string format.

from datetime import datetime, timedelta old_time = '2014-01-07T09:29:35Z' def time_converter(old_time, time_zone): time_zone = float(time_zone[:3] + ('.5' if time_zone[3] == '3' else '.0')) str_time = datetime.strptime(old_time, "%Y-%m-%dT%H:%M:%SZ") return (str_time + timedelta(hours=time_zone)).strftime("%Y-%m-%dT%H:%M:%SZ") if __name__ == '__main__': for time_zone in ('+0400', '+0430', '-1400'): print(time_converter(old_time, time_zone)) 

Output:

2014-01-07T13:29:35Z 2014-01-07T13:59:35Z 2014-01-06T19:29:35Z 
Sign up to request clarification or add additional context in comments.

Comments

0

You can also create timezone classes for creating timezone aware datetime objects:

from datetime import tzinfo, timedelta, datetime class myTimeZone(tzinfo): def utcoffset(self, dt): return timedelta(hours=4) def dst(self, dt): return timedelta(hours=0) class utcTimeZone(tzinfo): def utcoffset(self, dt): return timedelta(hours=0) def dst(self, dt): return timedelta(hours=0) d = datetime.strptime("2014-01-07T09:29:35Z","%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=utcTimeZone()) print(d.astimezone(myTimeZone()).isoformat()) # Prints '2014-01-07T13:29:35+04:00' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.