Using pytz to convert from a known timezone to local

I get the date in a fixed time zone. I need to convert it to the time zone of the local computer, but I do not know what it is. How can I do this using pytz (not dateutil)? I found many solutions that use dateutil, for example. this answer , but I can not find a similar function in pytz.

+4
source share
1 answer

You can convert via Unix timestamp (UTC):

foreign_naive = datetime.datetime(2012, 3, 11, 6, 0, 0) foreign_timezone = 'US/Eastern' foreign_dt = pytz.timezone(foreign_timezone).localize(foreign_naive) timestamp = time.mktime(foreign_dt).astimezone(pytz.utc).timetuple() local_dt = datetime.datetime.fromtimestamp(timestamp) 

This uses a solution from Python. Create a unix timestamp of five minutes in the future .

Note that this will not tell you what the local time zone is, although you can find out its offset from UTC at this time using:

 (local_dt - datetime.datetime.utcfromtimestamp(timestamp)).seconds 
+1
source

Source: https://habr.com/ru/post/1432250/


All Articles