Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct from_i64(). #1

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
tuxuser marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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