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 4248854
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:19:02+00:00 2026-05-21T04:19:02+00:00

Is there a way to get the UTC timestamp by specifying the date? What

  • 0

Is there a way to get the UTC timestamp by specifying the date? What I would expect:

datetime(2008, 1, 1, 0, 0, 0, 0)

should result in

 1199145600

Creating a naive datetime object means that there is no time zone information. If I look at the documentation for datetime.utcfromtimestamp, creating a UTC timestamp means leaving out the time zone information. So I would guess, that creating a naive datetime object (like I did) would result in a UTC timestamp. However:

then = datetime(2008, 1, 1, 0, 0, 0, 0)
datetime.utcfromtimestamp(float(then.strftime('%s')))

results in

2007-12-31 23:00:00

Is there still any hidden time zone information in the datetime object? What am I doing wrong?

  • 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-05-21T04:19:03+00:00Added an answer on May 21, 2026 at 4:19 am

    Naïve datetime versus aware datetime

    Default datetime objects are said to be “naïve”: they keep time information without the time zone information. Think about naïve datetime as a relative number (ie: +4) without a clear origin (in fact your origin will be common throughout your system boundary).

    In contrast, think about aware datetime as absolute numbers (ie: 8) with a common origin for the whole world.

    Without timezone information you cannot convert the “naive” datetime towards any non-naive time representation (where does +4 targets if we don’t know from where to start ?). This is why you can’t have a datetime.datetime.toutctimestamp() method. (cf: http://bugs.python.org/issue1457227)

    To check if your datetime dt is naïve, check dt.tzinfo, if None, then it’s naïve:

    datetime.now()        ## DANGER: returns naïve datetime pointing on local time
    datetime(1970, 1, 1)  ## returns naïve datetime pointing on user given time
    

    I have naïve datetimes, what can I do ?

    You must make an assumption depending on your particular context:
    The question you must ask yourself is: was your datetime on UTC ? or was it local time ?

    • If you were using UTC (you are out of trouble):

      import calendar
      
      def dt2ts(dt):
          """Converts a datetime object to UTC timestamp
      
          naive datetime will be considered UTC.
      
          """
      
          return calendar.timegm(dt.utctimetuple())
      
    • If you were NOT using UTC, welcome to hell.

      You have to make your datetime non-naïve prior to using the former
      function, by giving them back their intended timezone.

      You’ll need the name of the timezone and the information about
      if DST was in effect
      when producing the target naïve datetime (the
      last info about DST is required for cornercases):

      import pytz     ## pip install pytz
      
      mytz = pytz.timezone('Europe/Amsterdam')             ## Set your timezone
      
      dt = mytz.normalize(mytz.localize(dt, is_dst=True))  ## Set is_dst accordingly
      

      Consequences of not providing is_dst:

      Not using is_dst will generate incorrect time (and UTC timestamp)
      if target datetime was produced while a backward DST was put in place
      (for instance changing DST time by removing one hour).

      Providing incorrect is_dst will of course generate incorrect
      time (and UTC timestamp) only on DST overlap or holes. And, when
      providing
      also incorrect time, occuring in “holes” (time that never existed due
      to forward shifting DST), is_dst will give an interpretation of
      how to consider this bogus time, and this is the only case where
      .normalize(..) will actually do something here, as it’ll then
      translate it as an actual valid time (changing the datetime AND the
      DST object if required). Note that .normalize() is not required
      for having a correct UTC timestamp at the end, but is probably
      recommended if you dislike the idea of having bogus times in your
      variables, especially if you re-use this variable elsewhere.

      and AVOID USING THE FOLLOWING: (cf: Datetime Timezone conversion using pytz)

      dt = dt.replace(tzinfo=timezone('Europe/Amsterdam'))  ## BAD !!
      

      Why? because .replace() replaces blindly the tzinfo without
      taking into account the target time and will choose a bad DST object.
      Whereas .localize() uses the target time and your is_dst hint
      to select the right DST object.

    OLD incorrect answer (thanks @J.F.Sebastien for bringing this up):

    Hopefully, it is quite easy to guess the timezone (your local origin) when you create your naive datetime object as it is related to the system configuration that you would hopefully NOT change between the naive datetime object creation and the moment when you want to get the UTC timestamp. This trick can be used to give an imperfect question.

    By using time.mktime we can create an utc_mktime:

    def utc_mktime(utc_tuple):
        """Returns number of seconds elapsed since epoch
    
        Note that no timezone are taken into consideration.
    
        utc tuple must be: (year, month, day, hour, minute, second)
    
        """
    
        if len(utc_tuple) == 6:
            utc_tuple += (0, 0, 0)
        return time.mktime(utc_tuple) - time.mktime((1970, 1, 1, 0, 0, 0, 0, 0, 0))
    
    def datetime_to_timestamp(dt):
        """Converts a datetime object to UTC timestamp"""
    
        return int(utc_mktime(dt.timetuple()))
    

    You must make sure that your datetime object is created on the same timezone than the one that has created your datetime.

    This last solution is incorrect because it makes the assumption that the UTC offset from now is the same than the UTC offset from EPOCH. Which is not the case for a lot of timezones (in specific moment of the year for the Daylight Saving Time (DST) offsets).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way in an Excel VBA macro to get the current datetime
is there way how to get name ov event from Lambda expression like with
Is there a way to get the tests inside of a TestCase to run
Is there any way to get the ID of the element that fires an
Is there a way to get the current xml data when we make our
Is there any way to get Python to use my ActiveTcl installation instead of
Is there a way to get stored procedures from a SQL Server 2005 Express
Is there a way to get a DrawingContext (or something similar) for a WriteableBitmap
Is there a way to get the C/C++ preprocessor or a template or such
Is there a way to get rid of the selection rectangle when clicking a

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.