Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8821895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:58:01+00:00 2026-06-14T05:58:01+00:00

I have a database that stores datetime as UTC. I need to look up

  • 0

I have a database that stores datetime as UTC. I need to look up info from a particular time, but the date and time are given in a local time, let’s say ‘Europe/Copenhagen’. I’m given these as:

year = 2012; month = 12; day = 2; hour = 13; min = 1;

So, I need to convert these to UTC so I can look them up in the database. I want to do this using pytz. I am looking at localize:

 local_tz = timezone('Europe/Copenhagen')
 t = local_tz.localize(datetime.datetime(year, month, day, hour, min))

But I’m confused about localize(). Is this assuming that year, etc, are given to me in local time? Or, is it assuming that they they are given in UTC and now it has converted them to local time?

print t gives me:

2012-12-02 13:01:00+01:00

So it seems that it assumed that the original year, etc was in utc; hours is now 13+1 instead of 13. So what should I do instead? I have read the pytz documentation and this does not make it clearer to me. It mentions a lot that things are tricky so I’m not sure whether pytz is actually solving these issues. And, I don’t always know if the examples are showing me things that work or things that won’t work.

I tried normalize:

print local_tz.normalize(t)

That gives me the same result as print t.

EDIT: With the numbers given above for year etc. it should match up with information in the database for 2012-12-2 12:01. (since Copenhagen is utc+1 on that date)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:58:02+00:00Added an answer on June 14, 2026 at 5:58 am

    localize() attaches the timezone to a naive datetime.datetime instance in the local timezone.

    If you have datetime values in a local timezone, localize to that timezone, then use .astimezone() to cast the value to UTC:

    >>> localdt = local_tz.localize(datetime.datetime(year, month, day, hour, min))
    >>> localdt.astimezone(pytz.UTC)
    datetime.datetime(2012, 12, 2, 12, 1, tzinfo=<UTC>)
    

    Note that you don’t need to do this, datetime objects with a timezone can be compared; they’ll both be normalized to UTC for the test:

    >>> localdt.astimezone(pytz.UTC) == localdt
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have variable that store data datetime UTC from database. I want to make
I have a database that stores the time for me. I insert it from
I have datetime information stored in the database as UTC. I'm displaying this info
I have a database that stores dates broken into int's. I.e. Day, Month and
I have a database that has a table email_eml that stores 3 attributes name_eml,
I have a MySql database that stores a timestamp for each record I insert.
Our scenario: We have a main database that stores company-wide information. We have several
i have taken over a database that stores fitness information and we were having
I have a distance field in my database that stores the distance traveled on
Ok I have a table in my SQL Server database that stores comments. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.