Skip to content

Commit

Permalink
Merge pull request #1 from Enyium/patch-1
Browse files Browse the repository at this point in the history
Correct `from_i64()`.
  • Loading branch information
tuxuser authored Nov 29, 2023
2 parents 9eae93d + a70af71 commit a948046
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: [beta, stable, 1.56.0]
rust: [beta, stable, 1.57.0]

steps:
- uses: actions/checkout@v3
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl FileTime {
/// let ft_i64 = FileTime::now().filetime();
/// ```
pub fn filetime(&self) -> i64 {
(self.secs * Self::HUNDREDS_OF_NANOSECONDS) + self.nsecs
(self.secs * Self::HUNDREDS_OF_NANOSECONDS) + self.nsecs.checked_div(100).unwrap_or(0)
}

/// Return FILETIME epoch as DateTime<Utc>
Expand All @@ -100,7 +100,7 @@ impl FileTime {
pub fn from_i64(filetime: i64) -> Self {
assert!(filetime >= 0, "Only positive values allowed");
let secs: i64 = filetime / Self::HUNDREDS_OF_NANOSECONDS;
let nsecs: i64 = filetime % Self::HUNDREDS_OF_NANOSECONDS;
let nsecs: i64 = filetime % Self::HUNDREDS_OF_NANOSECONDS * 100;

Self { secs, nsecs }
}
Expand All @@ -115,7 +115,7 @@ impl FileTime {
pub fn from_datetime(dt: DateTime<Utc>) -> Self {
let nsecs = Self::EPOCH_AS_FILETIME
+ (dt.timestamp() * Self::HUNDREDS_OF_NANOSECONDS)
+ dt.timestamp_subsec_nanos() as i64;
+ dt.timestamp_subsec_nanos().checked_div(100).unwrap_or(0) as i64;
Self::from_i64(nsecs)
}

Expand Down Expand Up @@ -186,13 +186,13 @@ mod test {
#[test]
fn from_datetime() {
let dt = Utc.from_utc_datetime(
&DateTime::parse_from_rfc3339("2009-07-25T23:00:00.000001000Z")
&DateTime::parse_from_rfc3339("2009-07-25T23:00:00.0001Z")
.unwrap()
.naive_utc(),
);
assert_eq!(
FileTime::from_datetime(dt),
FileTime::from_i64(128930364000001000)
dt,
FileTime::from_i64(128930364000001000).into()
);
}

Expand All @@ -204,7 +204,7 @@ mod test {
ft,
FileTime {
secs: 13013971283,
nsecs: 1482830
nsecs: 148283000
}
);
}
Expand All @@ -214,7 +214,7 @@ mod test {
let bytes = [0xCE_u8, 0xEB, 0x7D, 0x1A, 0x61, 0x59, 0xCE, 0x01];
let ft: [u8; 8] = FileTime {
secs: 13013971283,
nsecs: 1482830,
nsecs: 148283000,
}
.into();
assert_eq!(ft, bytes);
Expand Down Expand Up @@ -243,7 +243,7 @@ mod test {
let dt = Utc
.with_ymd_and_hms(30828, 9, 14, 2, 48, 5)
.unwrap()
.checked_add_signed(Duration::nanoseconds(4775807))
.checked_add_signed(Duration::nanoseconds(477580700))
.unwrap();
assert_eq!(ft.to_datetime(), dt);
}
Expand All @@ -252,7 +252,7 @@ mod test {
fn filetime_one() {
let ft = FileTime::from_i64(1);
assert_eq!(ft.seconds(), 0);
assert_eq!(ft.nanoseconds(), 1);
assert_eq!(ft.nanoseconds(), 100);
}

#[test]
Expand Down

0 comments on commit a948046

Please sign in to comment.