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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:57:13+00:00 2026-05-27T02:57:13+00:00

Python’s datetime class has a fromtimestamp method to create a datetime object from a

  • 0

Python’s datetime class has a fromtimestamp method to create a datetime object from a timestamp, but doesn’t provide a totimestamp method for the other way round…
I’m aware that with something like time.mktime(x.timetuple()) you can convert the datetime object to a timestamp, but this looks unnecessary complicated to me, so I’m curious why there is no totimestamp method?

  • 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-27T02:57:14+00:00Added an answer on May 27, 2026 at 2:57 am

    I do remember a discussion/bug report about this thing while I wondered about this some time back. Long story short: plenty of proposals have been made, but for some reason, none have been accepted.

    The point is I think best summed up in this reply:

    Plenty have proposed a satisfactory solution. No one has come up with a solution that is satisfactory to you, because you have overconstrained the problem. The reason we still have no utctotimestamp() after all these years is that you, and you alone as far as I know, refuse to accept a method that inverts utcfromtimestamp() with microsecond precision over its working range. Such a method is a perfectly reasonable and acceptable solution and would add a lot of value to Python as a language.

    I suspect you don’t realize just how much pain you have unintentionally caused the world of Python users by singlehandedly blocking progress on this issue. I’ve seen them: students, friends, coworkers — even very smart and capable people are stymied by it. No one thinks of looking in the calendar module. Maybe if you watched some of them struggle with this, you would understand.

    The end result to this story was that documentation was added on how to do it yourself:

    # On the POSIX compliant platforms, `utcfromtimestamp(timestamp)` is
    # equivalent to the following expression:
    datetime(1970, 1, 1) + timedelta(seconds=timestamp)
    
    # There is no method to obtain the timestamp from a `datetime` instance,
    # but POSIX timestamp corresponding to a `datetime` instance `dt` can be
    # easily calculated as follows. For a naive `dt`:
    timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)
    
    # And for an aware ``dt``::
    timestamp = (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)) / timedelta(seconds=1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python 2.6+ and 3.* have next(), but pre-2.6 only offers the object.next method. Is
Python uses the reference count method to handle object life time. So an object
Python gives us the ability to create 'private' methods and variables within a class
Python documentations states : Exceptions should typically be derived from the Exception class, either
Python Decimal doesn't support being constructed from float; it expects that you have to
Python's list type has an index() method that takes one parameter and returns the
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python's IDLE has 'Check Module' (Alt-X) to check the syntax which can be called
Python has the idea of metaclasses that, if I understand correctly, allow you to
Python: How to get the caller's method name in the called method? Assume I

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.