pawl
pawl

Reputation: 354

Why doesn't datetime.time have astimezone?

I'm converting a datetime.time object's timezone to a different timezone. It looks like the easiest way is to create a datetime.datetime from the datetime.time object, then do the conversion.

Like this:

dt = datetime.datetime.combine(datetime.date.today(), self.data)
utc_dt = dt.astimezone(utc)
self.data = utc_dt.time()

Why doesn't datetime.time have astimezone like datetime.datetime does? It seems like that would make things much easier. Is it because timezone offset can be ambiguous without the date?

Upvotes: 1

Views: 732

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123440

Timezones require a date, the time alone is not enough.

That's because:

  1. You need to be able to determine if DST applies.
  2. You need to know what offsets to apply. Timezones are not static, they have changed over time, adjusting DST start and end dates, as well as the offset from UTC.

Upvotes: 7

Related Questions