0

Here is my code:

from dateutil import parser
import datetime

rTime = '2020-06-22T17:19:37.729000Z'
dateTimeRes = parser.isoparse(rTime)
naive = dateTimeRes.replace(tzinfo=None)
lastTime = (naive-datetime.datetime(1970,1,1)).total_seconds()
print ("Seconds : {}".format(lastTime))

output :

python test.py
Seconds : 1592846377.73

But expected '1592846377729000000'

  • 2
    Would you care to elaberate why you expect that number? The total seconds is the number of seconds from the "epoch", Jan 1 1970. Your expected number is "way ahead"! There are only 365.2522*24*3600=31.2e6 seconds per year, so your "expected number" corresponds to about 5e10 years.... – Dov Grobgeld Jul 09 '20 at 05:51
  • Using expected time in influxdb time series.. and also needs a format '2020-06-22T17:19:37.729000Z' to input influxdb. – Young Star Hemanth Jul 09 '20 at 06:08
  • 1
    Your expected output seem to be nanoseconds, is that correct? – FObersteiner Jul 09 '20 at 06:12
  • if your required units are not seconds, but milliseconds then just convert by multiplication – Rob Raymond Jul 09 '20 at 06:15
  • Also, you can just do `dateTimeRes.timestamp()` to get the seconds since 1970-1-1. – FObersteiner Jul 09 '20 at 06:25

2 Answers2

0

It's not the way I convert a UTC string. Why do you expect such a high number? estimating 365 days * 24 hrs * 60 mins * 60 seconds * 50 years included in output below.

from datetime import datetime
rTime = '2020-06-22T17:19:37.729000Z'
# string conversion only works for 6 digit ms,  strip back to 26 chars
print((datetime.strptime(rTime[:26]+"Z", "%Y-%m-%dT%H:%M:%S.%fZ") - datetime(1970,1,1)).total_seconds())
365*24*60*60*50

output

1592846377.729
1576800000
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
  • can i blindly add 6 zeros to output(1592846377.729) ?? will it affect when i reverse convert?? – Young Star Hemanth Jul 09 '20 at 06:19
  • there are 60 seconds in a minute, there are 10^3 milliseconds in a second, there are 10^6 nanoseconds in a second. It's not blind, multiplying by 10^6 converts seconds into nanoseconds if that's your required unit – Rob Raymond Jul 09 '20 at 06:21
  • something to note if you need to be accurate https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate – darkash Jul 09 '20 at 06:30
  • @darkash good point, however in this case there is only 3dp in input string and there is no accuracy loss – Rob Raymond Jul 09 '20 at 06:36
0

Once you have a datetime object (here naive), it is easy to convert it to a nano seconds timestamp, because the timestamp method returns a floating point number of seconds:

ts = int(naive.timestamp() * 1000000) * 1000

Simply you must first compute microseconds as integer to prevent the floating point inaccuracy to give a wrong nanoseconds number: int(naive.timestamp() * 1000000000) is the inaccurate value 1592839177729000192.

BTW, nano-seconds timestamp are mainly used in numpy or Pandas. If you use them, you can directly use numpy:

rTime = '2020-06-22T17:19:37.729000Z'
ts = np.int64(np.datetime64(rTime))

which gives as expected 1592846377729000

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252