5

I have a Unix timestamp which value is 1502878840. This Unix timestamp value can be converted to human readable like Aug 16, 2017 10:20:40. I have 2 following python code to convert 1502878840 to Aug 16, 2017 10:20:40. Both of them give a same result (Aug 16, 2017 10:20:40)

  1. First method
    utc = datetime.fromtimestamp(1502878840)
  2. Second method
    utc = datetime(1970, 1, 1) + timedelta(seconds=1502878840)

Could anyone answer me 2 following questions.
1. The result of 2 methods are same. But at the logic view point of Python code, is there any case that may cause the difference in result?
I ask this question because I see most of the python code use First method.
2. As I read here, the Unix time will have a problem on 19 January, 2038 03:14:08 GMT.
I run a timestamp which has a date after 19.Jan, 2038 (2148632440- Feb 01, 2038 10:20:40). The result is as follows
First method: ValueError: timestamp out of range for platform time_t
Second method: 2038-02-01 10:20:40
Question is: Can I use Second method to overcome the problem of "Year 2038 problem"?

2
  • First method needs to be utcfromtimestamp(). Commented Aug 16, 2017 at 6:04
  • @Mark Tolonen: Thank you so much for your suggestion. I read manual of datetime again. It is better if we use utcfromtimestamp Commented Aug 18, 2017 at 2:34

1 Answer 1

3

Quoting the documentation:

fromtimestamp() may raise OverflowError, if the timestamp is out of the range of values supported by the platform C localtime() or gmtime() functions, and OSError on localtime() or gmtime() failure. It’s common for this to be restricted to years in 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by fromtimestamp(), and then it’s possible to have two timestamps differing by a second that yield identical datetime objects. See also utcfromtimestamp().

The second solution solves your problem:

utc = datetime(1970, 1, 1) + timedelta(seconds=1502878840)
Sign up to request clarification or add additional context in comments.

4 Comments

The timedelta documentation mentions a limit of 999999999 days. That's less than 3M years. The planet earth may last much longer than that.
@Gribouillis Good news, and Python may survive to that 🙂
@Laurent LAPORTE : Thank you for your answer. By this answer, I have a method to overcome the Year 2038 problem.
@Gribouillis: Thank you for your information. Actually, my intention is to solve Year 2038 problem firstly. I know that timedelta has a limitation as your mention (3M years), but for a very far future like that, we can not sure anything. The hardware running python program can running until to the next 3M years? I see some RTC chip also limits their correct operation until 2099 :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.