Optimize time::from::nanos function. (#2811)

This commit is contained in:
Finn Bear 2023-10-18 01:53:25 -07:00 committed by GitHub
parent 9d9fde2db8
commit 3a3125d030
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -247,21 +247,10 @@ pub mod from {
use chrono::{NaiveDateTime, Offset, TimeZone, Utc}; use chrono::{NaiveDateTime, Offset, TimeZone, Utc};
pub fn nanos((val,): (i64,)) -> Result<Value, Error> { pub fn nanos((val,): (i64,)) -> Result<Value, Error> {
// Example unix nanoseconds timestamp: 1_696_537_096_309_565_400ns const NANOS_PER_SEC: i64 = 1_000_000_000;
// Get seconds part: 1_696_537_096s
// Get nanoseconds part: 309_565_400ns
let s = val.to_string();
let seconds: i64 = if s.len() > 9 { let seconds = val.div_euclid(NANOS_PER_SEC);
s.as_str()[..(s.len() - 9)].parse().unwrap() let nanoseconds = val.rem_euclid(NANOS_PER_SEC) as u32;
} else {
0
};
let nanoseconds: u32 = if s.len() > 9 {
s.as_str()[(s.len() - 9)..].parse().unwrap()
} else {
s.as_str().parse().unwrap()
};
match NaiveDateTime::from_timestamp_opt(seconds, nanoseconds) { match NaiveDateTime::from_timestamp_opt(seconds, nanoseconds) {
Some(v) => match Utc.fix().from_local_datetime(&v).earliest() { Some(v) => match Utc.fix().from_local_datetime(&v).earliest() {