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?
2**53. In other words, your timestamp can be converted safely for the next 200 million years (or back 200 million years)float(2**53-1)-float(2**53-2)is 1.0, and generally speakingfloat(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). Whilefloat(2**53+2)-float(2**53+1)is 2.0, and generally speakingfloat(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, ...)time.time_ns(), you sidestep this issue and get the underlying platform's time resolution uniformly.