1

Using Python, I have a datetime object with the value (for example)

datetime(2021, 12, 28, 22, 31, tzinfo=tzutc())

This prints as

2021-12-28 22:31:00+00:00

How do I display that in US/Pacific?

I've seen references that use import pytz but I don't have that library available.

ANSWER: Even though it isn't the checked answer, I like the response by @jfs

from datetime import datetime, timezone

def utc_to_local(utc_dt):
    return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)

Output:

orig: 2021-12-28 22:31:00+00:00
after utc_to_local: 2021-12-28 14:31:00-08:00

At this time I am in PST and the offset from UTC is 8 hours

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
shepster
  • 339
  • 3
  • 13
  • more duplicates [Convert UTC datetime to Pacific datetime in Python](https://stackoverflow.com/q/49671138/10197418), [need to convert UTC (aws ec2) to PST in Python](https://stackoverflow.com/q/8809765/10197418) – FObersteiner Dec 29 '21 at 11:03

1 Answers1

1

If you're using python 3.9+ you can use the built-in zoneinfo:

from zoneinfo import ZoneInfo
from dateutil.tz import tzutc
from datetime import datetime

date = datetime(2021, 12, 28, 22, 31, tzinfo=tzutc())
date = date.astimezone(ZoneInfo('US/Pacific'))
2021-12-28 14:31:00-08:00
Pedro Maia
  • 2,666
  • 1
  • 5
  • 20