Skip to content

Commit

Permalink
add set_nano
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Oct 20, 2023
1 parent ffe19fd commit 1f1d2ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastdate"
version = "0.3.5"
version = "0.3.6"
edition = "2021"
description = "Rust fast date carte"
readme = "Readme.md"
Expand Down
13 changes: 11 additions & 2 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ impl DateTime {

/// stand "0000-00-00 00:00:00.000000000"
pub fn display_stand(&self) -> String {
let mut v =self.display(false);
v.replace_range((10..11)," ");
let mut v = self.display(false);
v.replace_range(10..11, " ");
v
}

Expand Down Expand Up @@ -392,6 +392,15 @@ impl DateTime {
}
len
}

pub fn set_nano(mut self, nano: u32) -> Self {
let v = self.nano();
if nano != v {
self = self.sub(Duration::from_nanos(v as u64));
self = self.add(Duration::from_micros(nano as u64));
}
self
}
}

impl Add<Duration> for DateTime {
Expand Down
20 changes: 20 additions & 0 deletions tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ fn test_display_datetime() {
let v = epoch.display(false);
assert_eq!(v, "2000-01-01T01:01:11.000001233");
}

#[test]
fn test_display_stand() {
let epoch = fastdate::DateTime::from((Date {
Expand All @@ -370,3 +371,22 @@ fn test_display_stand() {
let v = epoch.display_stand();
assert_eq!(v, "2000-01-01 01:01:11.000001233");
}

#[test]
fn test_set_micro() {
let mut dt = fastdate::DateTime::from((Date {
day: 1,
mon: 1,
year: 2000,
}, Time {
nano: 1233,
sec: 11,
minute: 1,
hour: 1,
}));
dt = dt.set_nano(0);
assert_eq!(dt.display_stand(), "2000-01-01 01:01:11");

dt = dt.set_nano(1);
assert_eq!(dt.display_stand(), "2000-01-01 01:01:11.000001");
}

0 comments on commit 1f1d2ba

Please sign in to comment.