0

I'm trying to convert a timezone aware datetime to UTC, but without carrying an offset:

I receive some data to create a start date for an event: year, month, day, hours, minutes

I have a venue object and use its longitude and latitude to obtain the venue timezone like so:

from datetime import datetime
import pytz
from timezonefinder import TimezoneFinder

tf = TimezoneFinder()
tz_str = tf.timezone_at(lng=venue.longitude, lat=venue.latitude)

For instance tz_str can take the value 'Asia/Tokyo'

I then create a timezone aware datetime object:

start_date = datetime(2020, 3, 1, 9, 0, 0, 0, pytz.timezone(tz_str))

this will have the value datetime.datetime(2020, 3, 1, 9, 0, tzinfo=<DstTzInfo 'Asia/Tokyo' LMT+9:19:00 STD>)

I would like to convert it to UTC before storing to the DB, but I don't want the offset.

If I simply convert to UTC, it will keep the offset which I don't want:

utc_start_date = start_date.astimezone(pytz.utc)

this will take the following value datetime.datetime(2020, 2, 29, 23, 41, tzinfo=<UTC>), which is 19 minutes off.

How can I remove the offset?

Just trying
  • 484
  • 1
  • 5
  • 15
  • I found that one option is to create an non-aware datetime object and then localize it: `start_date = pytz.timezone('Asia/Tokyo').localize( datetime(2020, 3, 1, 9, 0, 0, 0) )` this will have value `datetime.datetime(2020, 3, 1, 9, 0, tzinfo=). Note that the 19 in offset has disappeared – Just trying Feb 03 '20 at 01:28
  • 1
    Does this answer your question? [Weird timezone issue with pytz](https://stackoverflow.com/questions/11473721/weird-timezone-issue-with-pytz) – deadvoid Feb 03 '20 at 01:59

0 Answers0