简体   繁体   中英

How can I take one nanosecond away from a datetime timestamp, and have the final form in datetime timestamp?

I have a datetime timestamp of 2022-10-11 06:00:00 .

I am trying to take away ONE nanosecond and have it back in datetime format. So the desired output is:

`2022-10-11 05:59:59.999999999`

I have followed a few functions and conversions on stack overflow, but most don't get the precision correct. They convert the datetime into nanoseconds, take one away then convert it back, but then one hour is somehow added because of precision (or lack of precision)

Any help would be great!

datetime.datetime only supports till microsecond precision. If you need nanosecond precision, use pandas.Timestamp instead.

import pandas as pd
import time

now_in_ns = pd.Timestamp(time.time_ns(), unit='ns')
one_ns_later = now_in_ns + pd.Timedelta('1ns')

print(now_in_ns, one_ns_later)

So, if you are going to be working with nanoseconds, either extend datetime.datetime to support nanosecond or familiarize yourself with the class: https://pandas.pydata.org/docs/reference/api/pandas.Timestamp.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM