Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print a datetime in the local timezone?

Let's say I have a variable t that's set to this:

datetime.datetime(2009, 7, 10, 18, 44, 59, 193982, tzinfo=<UTC>) 

If I say str(t), i get:

'2009-07-10 18:44:59.193982+00:00' 

How can I get a similar string, except printed in the local timezone rather than UTC?

like image 622
mike Avatar asked Jul 10 '09 18:07

mike


People also ask

How can I get local time in datetime?

How to Get the Current Time with the datetime Module. To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.

How do I get local datetime in Python?

today() method to get the current local date. By the way, date. today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.

How do I convert datetime to timezone?

To do this, we will use the FindSystemTimeZoneById() of TimeZoneInfo class. This function takes Id as a parameter to return the equivalent time in timezone passed. In the example below, we are taking the current system time and converting it into “Central Standard Time”. DateTime currentTime = TimeZoneInfo.


1 Answers

This script demonstrates a few ways to show the local timezone using astimezone():

#!/usr/bin/env python3  import pytz from datetime import datetime, timezone from tzlocal import get_localzone  utc_dt = datetime.now(timezone.utc)  PST = pytz.timezone('US/Pacific') EST = pytz.timezone('US/Eastern') JST = pytz.timezone('Asia/Tokyo') NZST = pytz.timezone('Pacific/Auckland')  print("Pacific time {}".format(utc_dt.astimezone(PST).isoformat())) print("Eastern time {}".format(utc_dt.astimezone(EST).isoformat())) print("UTC time     {}".format(utc_dt.isoformat())) print("Japan time   {}".format(utc_dt.astimezone(JST).isoformat()))  # Use astimezone() without an argument print("Local time   {}".format(utc_dt.astimezone().isoformat()))  # Use tzlocal get_localzone print("Local time   {}".format(utc_dt.astimezone(get_localzone()).isoformat()))  # Explicitly create a pytz timezone object # Substitute a pytz.timezone object for your timezone print("Local time   {}".format(utc_dt.astimezone(NZST).isoformat())) 

It outputs the following:

$ ./timezones.py  Pacific time 2019-02-22T17:54:14.957299-08:00 Eastern time 2019-02-22T20:54:14.957299-05:00 UTC time     2019-02-23T01:54:14.957299+00:00 Japan time   2019-02-23T10:54:14.957299+09:00 Local time   2019-02-23T14:54:14.957299+13:00 Local time   2019-02-23T14:54:14.957299+13:00 Local time   2019-02-23T14:54:14.957299+13:00 

As of python 3.6 calling astimezone() without a timezone object defaults to the local zone (docs). This means you don't need to import tzlocal and can simply do the following:

#!/usr/bin/env python3  from datetime import datetime, timezone  utc_dt = datetime.now(timezone.utc)  print("Local time {}".format(utc_dt.astimezone().isoformat())) 
like image 118
htaccess Avatar answered Sep 27 '22 01:09

htaccess