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')>))
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.
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.
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')
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With