Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Dec 18, 2023
1 parent aa99077 commit fc2f028
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 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.26"
version = "0.3.27"
edition = "2021"
description = "Rust fast date carte"
readme = "Readme.md"
Expand Down
15 changes: 11 additions & 4 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,25 @@ impl DateTime {
let offset = self.offset();
let add_sub = if offset >= 0 { '+' } else { '-' };
let mut result = String::with_capacity(fmt.len());
let mut chars = fmt.chars();
while let Some(c) = chars.next() {
result.push(c);
let chars = fmt.as_bytes();
let mut index = 0;
let mut iter = chars.iter();
while let Some(c) = iter.next() {
result.push(*c as char);
if result.ends_with(".000000000") {
for _ in 0..".000000000".len() {
result.pop();
}
write!(result, ".{:09}", self.nano()).unwrap()
} else if result.ends_with(".000000") {
if (index + 3) < fmt.len() && chars[index + 1] == '0' as u8 && chars[index + 2] == '0' as u8 && chars[index + 3] == '0' as u8 {
index += 1;
continue;
}
for _ in 0..".000000".len() {
result.pop();
}
write!(result, ".{:06}", self.nano() / 1000).unwrap()
write!(result, ".{:06}", self.nano() / 1000).unwrap();
} else if result.ends_with("+00:00") {
for _ in 0.."+00:00".len() {
result.pop();
Expand Down Expand Up @@ -218,6 +224,7 @@ impl DateTime {
}
write!(result, "{:02}", self.sec()).unwrap();
}
index += 1;
}
result
}
Expand Down
4 changes: 2 additions & 2 deletions tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ fn test_format() {
year: 2000,
},
Time {
nano: 123456000,
nano: 123456789,
sec: 11,
minute: 1,
hour: 1,
Expand All @@ -872,7 +872,7 @@ fn test_format() {
let f = dt.format("YYYY-MM-DD/hh/mm/ss.000000");
assert_eq!(f, "2000-01-01/01/01/11.123456");
let f = dt.format("YYYY-MM-DD/hh/mm/ss.000000000");
assert_eq!(f, "2000-01-01/01/01/11.123456000");
assert_eq!(f, "2000-01-01/01/01/11.123456789");
}

#[test]
Expand Down

0 comments on commit fc2f028

Please sign in to comment.