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

后端 未结 3 1969
甜味超标
甜味超标 2020-12-07 01:16

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

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 01:38

    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.

提交回复
热议问题