0

I am trying to get the current time in nanoseconds from the exact start of 01/01/2010

I thought about just multiplying the total seconds by 1e+9, however I don't know how accurate that would be.

I've also tried to use .total_nanoseconds(), however that doesn't seem to work.

dateTimeObj = (datetime.datetime.now() - datetime.datetime(2010, 1, 1)).total_seconds() 

print(dateTimeObj)
olinox14
  • 6,177
  • 2
  • 22
  • 39
  • Multiplying the seconds is about as accurate as it gets with floating point numbers. Since the value of `total_seconds` is expressed as a float anyway, it's probably not 100% accurate to begin with. What kind of accuracy do you need over the span of 9 years…? – deceze Jun 18 '19 at 14:01
  • 2
    datetime only stores dates up to microseconds, not nanoseconds, and the only `.total_` method is `.total_seconds()` – h4z3 Jun 18 '19 at 14:03

1 Answers1

0

If you have a new enough Python, you might be able to use time.time_ns() to accomplish this (by subtracting, I guess):

>>> import time
>>> time.time()
1560868060.8969848
>>> time.time_ns()
1560868065180866064
>>> 

I just tried it on the Python shell in the browser at their website.

A couple bug reports regarding the module you are asking about, where this is not yet possible:

kcrisman
  • 4,374
  • 20
  • 41
  • I assume there is a way to calculate the nanosecond for your date in 2010 using https://docs.python.org/3/library/time.html – kcrisman Jun 18 '19 at 14:31