Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Nov 17, 2023
1 parent a9ca165 commit 9815e36
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 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.24"
version = "0.3.25"
edition = "2021"
description = "Rust fast date carte"
readme = "Readme.md"
Expand Down
27 changes: 18 additions & 9 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl DateTime {

///add/sub sec
pub fn add_sub_sec(self, sec: i64) -> Self {
if sec > 0 {
if sec >= 0 {
self.add(Duration::from_secs(sec as u64))
} else {
self.sub(Duration::from_secs((-sec) as u64))
Expand Down Expand Up @@ -112,31 +112,31 @@ impl DateTime {

///from timestamp sec
pub fn from_timestamp(sec: i64) -> DateTime {
if sec > 0 {
if sec >= 0 {
Self::from_system_time(UNIX_EPOCH + Duration::from_secs(sec as u64), 0)
} else {
Self::from_system_time(UNIX_EPOCH - Duration::from_secs((-sec) as u64), 0)
}
}
///from timestamp micros
pub fn from_timestamp_micros(micros: i64) -> DateTime {
if micros > 0 {
if micros >= 0 {
Self::from_system_time(UNIX_EPOCH + Duration::from_micros(micros as u64), 0)
} else {
Self::from_system_time(UNIX_EPOCH - Duration::from_micros((-micros) as u64), 0)
}
}
///from timestamp millis
pub fn from_timestamp_millis(ms: i64) -> DateTime {
if ms > 0 {
if ms >= 0 {
Self::from_system_time(UNIX_EPOCH + Duration::from_millis(ms as u64), 0)
} else {
Self::from_system_time(UNIX_EPOCH - Duration::from_millis((-ms) as u64), 0)
}
}
///from timestamp nano
pub fn from_timestamp_nano(nano: i128) -> DateTime {
if nano > 0 {
if nano >= 0 {
Self::from_system_time(UNIX_EPOCH + Duration::from_nanos(nano as u64), 0)
} else {
Self::from_system_time(UNIX_EPOCH - Duration::from_nanos((-nano) as u64), 0)
Expand All @@ -162,7 +162,16 @@ impl DateTime {
///
/// ```
pub fn format(&self, fmt: &str) -> String {
let (h, m, _) = self.offset_hms();
let (mut h, mut m, _) = self.offset_hms();
let offset = self.offset();
let add_sub;
if offset > 0 {
add_sub = '+';
} else {
add_sub = '-';
h = h.abs();
m = m.abs();
}
fmt.replacen("YYYY", &self.year().to_string(), 1)
.replacen("MM", &self.mon().to_string(), 1)
.replacen("DD", &self.day().to_string(), 1)
Expand All @@ -171,7 +180,7 @@ impl DateTime {
.replacen("ss", &self.sec().to_string(), 1)
.replacen(".000000000", &format!(".{:09}", self.nano()), 1)
.replacen(".000000", &format!(".{:06}", self.micro()), 1)
.replacen("+00:00", &format!("+{:02}:{:02}", h, m), 1)
.replacen("+00:00", &format!("{}{:02}:{:02}", add_sub, h, m), 1)
.to_string()
}

Expand Down Expand Up @@ -297,7 +306,7 @@ impl DateTime {
let offset_sec = offset_sec();
let of = UtcOffset::from_whole_seconds(offset_sec).unwrap();
let (h, m, _) = of.as_hms();
if offset_sec > 0 {
if offset_sec >= 0 {
buf[len] = b'+';
len += 1;
} else {
Expand Down Expand Up @@ -434,7 +443,7 @@ impl DateTime {
len += 1;
} else {
let (h, m, s) = self.offset_hms();
if offset > 0 {
if offset >= 0 {
buf[len] = b'+';
len += 1;
buf[len] = b'0' + (h as u8 / 10);
Expand Down
25 changes: 23 additions & 2 deletions tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn test_parse_z_sub() {

#[test]
fn test_parse_s() {
let date = DateTime::parse("YYYY/MM/DD/hh:mm:ss","2022/12/12/00:00:00").unwrap();
let date = DateTime::parse("YYYY/MM/DD/hh:mm:ss", "2022/12/12/00:00:00").unwrap();
assert_eq!(&date.to_string()[..19], "2022-12-12T00:00:00");
}

Expand Down Expand Up @@ -874,7 +874,7 @@ fn test_format() {
}

#[test]
fn test_forma2t() {
fn test_format2() {
let dt = fastdate::DateTime::from((
Date {
day: 1,
Expand All @@ -894,6 +894,27 @@ fn test_forma2t() {
assert_eq!(f, "2000-1-1/9/1/11.123456/+08:00");
}

#[test]
fn test_format3() {
let dt = fastdate::DateTime::from((
Date {
day: 1,
mon: 1,
year: 2000,
},
Time {
nano: 123456000,
sec: 11,
minute: 1,
hour: 1,
},
))
.set_offset(-8 * 60 * 60);
println!("dt={}", dt.to_string());
let f = dt.format("YYYY-MM-DD/hh/mm/ss.000000/+00:00");
assert_eq!(f, "1999-12-31/17/1/11.123456/-08:00");
}

#[test]
fn test_offset_sec_max() {
let mut dt = fastdate::DateTime::from((
Expand Down

0 comments on commit 9815e36

Please sign in to comment.