2

The float timestamp used in Python seems to be problematic. First, it will lose its precision as time goes on, and second, the timestamp is not usable, for example, storing in a database big integer field.

Is there a predefined way to use integer time? I know the seconds integer can be found using

timestamp_int = int(datetime.datetime.now().timestamp())

but wouldn't it have been better if there was a way to do like:

datetime.datetime.now().itimestamp()

Is there an official way other than doing a hack around? What is the point of using float when everyone else is using an integer instead?

9
  • 2
    It's unclear (to me) what you're looking for. Nanoseconds since the Epoch perhaps? Commented Jul 4 at 11:49
  • 1
    The timestamp is a 64 bit floating point number. That can represent integers accurately up to 2**53. In other words, your timestamp can be converted safely for the next 200 million years (or back 200 million years) Commented Jul 4 at 11:51
  • And it gives a millisecond resolution for 285 thousand years. Commented Jul 4 at 11:56
  • 1
    2^53 signed, not 2^52 signed. It's a float. The sign bit is not among the 53 bits. Not that it matters a lot, but well, ... (you can easily check that float(2**53-1)-float(2**53-2) is 1.0, and generally speaking float(2**53-k)-float(2**53-k-1) is 1.0 for all positive k (unless k is so big that you wrap up to big on the negative side). While float(2**53+2)-float(2**53+1) is 2.0, and generally speaking float(2**53+k+1)-float(2**53+k) is either 0.0 or 2.0 (or even 4.0, 8.0, ... if k is big enough to reach 55 bits, 56 bits, ...) Commented Jul 4 at 13:29
  • 1
    When using floating point, the fact that the available precision diminishes the farther you go into the future is admittedly somewhat of a wart. You only get nanosecond precision for a few months, losing it at about 2am on April 6, 1970. Microsecond precision, on the other hand, you can have 'til the year 2242. And if you use time.time_ns(), you sidestep this issue and get the underlying platform's time resolution uniformly. Commented Jul 5 at 12:45

2 Answers 2

2

You could use time.time_ns() instead. It returns the number of nanoseconds from the epoc as an int.

Sign up to request clarification or add additional context in comments.

1 Comment

And as the documentation notes, this "avoids the precision loss caused by the float type".
-1

If you just want the epoch time as an integer, you don't really need the datetime module (since you don't need a date).

I would recommend using time.monotonic_ns if you specifically want to generate integer timestamps, since it ensures that the clock is always moving forward and will prevent bugs related to your system clock jumping backwards and potentially giving the same timestamp for two events that happen non-simultaneously. The caveat to this is that your timestamps will not be able to be converted back into real-world times, so only use if you don't need to know precisely what time something happened, just the relative time. Read about it here https://docs.python.org/3/library/time.html#time.monotonic_ns

4 Comments

If you don't need a date, you're not dealing with "epoch time" at all! (My point is that your first sentence is kinda meaningless. Change it to "If you just want elapsed time as an integer" and it's fine.)
elapsed time is also best implemented with monotonic clock, since it represents real time and intervals accurately
You seem to be assuming that since the poster wants the time as an integer, the only possible thing they could be doing with those integers is subtracting them, to compute elapsed time over intervals. But I don't see that as the OP's intention. The OP wants to store timestamps into a database, and presumably these timestamps represent points in time, for which the date reference absolutely would be important.
I include a bolded disclaimer in my answer that it is an alternative for the other correct answer given by Mureinik if unique chronologically ordered / spaced timestamps are more important than logging real-world time. I am not trying to guess OP's intention with the timestamps lmao. OP (or other users on the thread) can pick which one matches their use case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.