Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the time in a different time zone

Is there an elegant way to display the current time in another time zone?

I would like to have something with the general spirit of:

cur = <Get the current time, perhaps datetime.datetime.now()> print("Local time   {}".format(cur)) print("Pacific time {}".format(<something like cur.tz('PST')>)) print("Israeli time {}".format(<something like cur.tz('IST')>)) 
like image 627
Adam Matan Avatar asked Sep 09 '09 09:09

Adam Matan


People also ask

Can I display 2 time zones on Android?

If you're running the latest version of Android, however, your clock app manages the clock widget and yes, it can indeed display multiple geographical locations – including time and date, as appropriate – within the widget space itself.

How do I display two time zones on my lock screen?

Go to Lock Screen and Security > Always On Display. Under Tip press Settings. Scroll to the desired dual time. Select the "Other" time to display on the map.


2 Answers

A simpler method:

from datetime import datetime from pytz import timezone      south_africa = timezone('Africa/Johannesburg') sa_time = datetime.now(south_africa) print sa_time.strftime('%Y-%m-%d_%H-%M-%S') 
like image 125
Mark Theunissen Avatar answered Oct 11 '22 17:10

Mark Theunissen


You could use the pytz library:

>>> from datetime import datetime >>> import pytz >>> utc = pytz.utc >>> utc.zone 'UTC' >>> eastern = pytz.timezone('US/Eastern') >>> eastern.zone 'US/Eastern' >>> amsterdam = pytz.timezone('Europe/Amsterdam') >>> fmt = '%Y-%m-%d %H:%M:%S %Z%z'  >>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0)) >>> print loc_dt.strftime(fmt) 2002-10-27 06:00:00 EST-0500  >>> ams_dt = loc_dt.astimezone(amsterdam) >>> ams_dt.strftime(fmt) '2002-10-27 12:00:00 CET+0100' 
like image 20
Andre Miller Avatar answered Oct 11 '22 18:10

Andre Miller