Python Convert time to UTC format

13,293

These things are always easier using complete datetime objects, e.g.:

import datetime
import pytz

time_zone = pytz.timezone('Asia/Kolkata')

# get naive date
date = datetime.datetime.now().date()
# get naive time
time = datetime.time(12, 30)
# combite to datetime
date_time = datetime.datetime.combine(date, time)
# make time zone aware
date_time = time_zone.localize(date_time)

# convert to UTC
utc_date_time = date_time.astimezone(pytz.utc)
# get time
utc_time = utc_date_time.time()

print(date_time)
print(utc_date_time)
print(utc_time)

Yields:

2014-07-13 12:30:00+05:30
2014-07-13 07:00:00+00:00
07:00:00

right now for me.

Share:
13,293
Abhilash Joseph
Author by

Abhilash Joseph

I’m Abhilash Joseph C and I write Django and Python code I started this in 2011.I was born in India,Kerala. I have spent my entire childhood over there only, schooling and college too. I shall start with revealing a little secret. Mostly people around me think that I am so much intelligent. I never could understand why because there is never anything that I did so that I can feel the same too. So with that fact,let me share my educational life so far.I did my graduation from Goverment College Manjeshwar,Kasaragod in Science(Statistics) stream. Besides that, I was also trying my hands with this computer stuff.Somehow, I got so much interested in it only. So I opted for my masters in Computer Applications (MCA) only.I did MCA in St Aloysius College Mangalore,Karnataka. As I went more and more deeper in this IT arena, I tried my hands with mostly everything. At the end, I got myself focused towards Web Development and Liux Operating System.Incase you already don't know, Linux Operating system (with the latest release, Ubuntu 12.04) is amongst the most powerful and reliable operating systems in the higher-end server operating systems from OpenSource .I am now fully focused on Python and Django framework. Still too technical ? Ok I am learning and doing computer stuff. How’s that? Where I See Myself In Future? Future is really something which can’t be defined and bounded in any boundaries. Its certainly can’t be decided too. Yes I have some plans and goals that I have decided for me but its better to keep them a secret for now. Lets see where life takes me? Yes whatever I shall be doing, I guarantee you that it will be with full passion and dedication.

Updated on July 28, 2022

Comments

  • Abhilash Joseph
    Abhilash Joseph almost 2 years
    from django.utils import timezone
    time_zone = timezone.get_current_timezone_name() # Gives 'Asia/Kolkata'
    date_time = datetime.time(12,30,tzinfo=pytz.timezone(str(time_zone)))
    

    Now I need to convert this time to UTC format and save it in Django model. I am not able to use date_time.astimezone(pytz.timezone('UTC')). How can I convert the time to UTC. Also Back to 'time_zone'.

    This is a use case when user type time in a text box and we need to save time time in UTC format. Each user will also select his own time zone that we provide from Django timezone module.

    Once the user request back the saved time it must be shown back to him in his selected time zone.

  • Matt Johnson-Pint
    Matt Johnson-Pint almost 10 years
    It shows +5:53:00 when it has not yet been applied to a datetime, because that's the earliest representation in the tzdb. The local mean time is usually calculated from solar observation, which reasonable aligns these old dates to our modern system.
  • Matt Johnson-Pint
    Matt Johnson-Pint almost 10 years
    Also, you should use localize instead of assigning the time zone to tzinfo directly. Read about localize in the pytz docs.
  • famousgarkin
    famousgarkin almost 10 years
    @MattJohnson Thanks for feedback and +5:53 explanation! True that about localize. Updated the answer accordingly, now this is what we wanted.