0

I am a bit confused between the following:

import datetime
import pytz
str(datetime.datetime.now(pytz.timezone('US/Pacific')))
str(datetime.datetime.now().replace(tzinfo=pytz.timezone('US/Pacific')))

In the first case I get: '2018-08-10 14:21:04.129204-07:00' and in the second case I get '2018-08-10 14:21:17.856903-07:53' I am a bit confused on why the dont return the same time offsets?

  • see also https://stackoverflow.com/questions/24856643/unexpected-results-converting-timezones-in-python/25390097 – John Carter Aug 10 '18 at 23:59

1 Answers1

0

In the first line you are passing

pytz.timezone('US/Pacific')

As function argument of

datetime.datetime.now() 

In the second line you are

.replacing 

The return of

 datetime.datetime.now()

With

pytz.timezone('US/Pacific')

I advise you to research into the pytz library documents : https://pypi.org/project/pytz/

My guess is that the first function the pytz out put is being formated to human readable by datetime.now()

In the second line you are just replacing the return, hence is shown as Unix time

Omar Gonzalez
  • 1,117
  • 4
  • 14
  • 22