Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert datetime.time from UTC to different timezone?

I have variable which holds time which is of type datetime.time in UTC, I wanted it to convert to some other timezone.

we can convert timezones in datetime.datetime instance as shown in this SO link - How do I convert local time to UTC in Python?. I am not able to figure out how to convert timezones in datetime.time instances. I can't use astimezone because datetime.time doesn't have this method.

For example:

>>> t = d.datetime.now().time()
>>> t
datetime.time(12, 56, 44, 398402)
>>> 

I need 't' in UTC format.

like image 902
rajpy Avatar asked May 17 '13 07:05

rajpy


People also ask

How do you convert UTC to specific time zones?

(GMT-5:00) Eastern Time (US & Canada)Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

How do I convert datetime to another time zone?

To convert a date to another time zone: Use the toLocaleString() method to get a string that represents the date according to the provided time zone. Pass the result to the Date() constructor. The returned Date object will have its date and time set according to the provided time zone.

How do I convert UTC time to date?

Getting the UTC timestampUse the datetime. datetime. now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC.


2 Answers

There are four cases:

  1. input datetime.time has tzinfo set (eg OP mentions UTC)
    1. output as non-naive time
    2. output as naive time (tzinfo not set)
  2. input datetime.time has tzinfo not set
    1. output as non-naive time
    2. output as naive time (tzinfo not set)

The correct answer needs to make use of datetime.datetime.timetz() function because datetime.time cannot be built as a non-naive timestamp by calling localize() or astimezone() directly.

from datetime import datetime, time
import pytz

def timetz_to_tz(t, tz_out):
    return datetime.combine(datetime.today(), t).astimezone(tz_out).timetz()

def timetz_to_tz_naive(t, tz_out):
    return datetime.combine(datetime.today(), t).astimezone(tz_out).time()

def time_to_tz(t, tz_out):
    return tz_out.localize(datetime.combine(datetime.today(), t)).timetz()

def time_to_tz_naive(t, tz_in, tz_out):
    return tz_in.localize(datetime.combine(datetime.today(), t)).astimezone(tz_out).time()

Example based on OP requirement:

t = time(12, 56, 44, 398402)
time_to_tz(t, pytz.utc) # assigning tzinfo= directly would not work correctly with other timezones

datetime.time(12, 56, 44, 398402, tzinfo=<UTC>)

In case naive timestamp is wanted:

time_to_tz_naive(t, pytz.utc, pytz.timezone('Europe/Berlin'))

datetime.time(14, 56, 44, 398402)

The cases where the time() instance has already tzinfo set are easier because datetime.combine picks up the tzinfo from the passed parameter, so we just need to convert to tz_out.

like image 118
Dorian B. Avatar answered Sep 19 '22 00:09

Dorian B.


I would create a temp datetime object, convert the tz, and extract the time again.

import datetime
def time_to_utc(t):
    dt = datetime.datetime.combine(datetime.date.today(), t)
    utc_dt = datetime_to_utc(dt)
    return utc_dt.time()

t = datetime.datetime.now().time()
utc_t = time_to_utc(t)

where, datetime_to_utc is any of the suggestions in the linked question.

like image 25
shx2 Avatar answered Sep 21 '22 00:09

shx2