-
Notifications
You must be signed in to change notification settings - Fork 13.3k
uefi: fs: Add file times plumbing #138918
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
Open
Ayush1325
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
Ayush1325:uefi-fs-time
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -40,11 +40,22 @@ impl Instant { | |||||
} | ||||||
|
||||||
impl SystemTime { | ||||||
pub(crate) const ZERO: SystemTime = SystemTime(Duration::ZERO); | ||||||
|
||||||
pub(crate) const fn new(t: r_efi::efi::Time) -> Self { | ||||||
Self(system_time_internal::uefi_time_to_duration(t)) | ||||||
} | ||||||
|
||||||
pub fn now() -> SystemTime { | ||||||
system_time_internal::now() | ||||||
.unwrap_or_else(|| panic!("time not implemented on this platform")) | ||||||
} | ||||||
|
||||||
#[expect(dead_code)] | ||||||
pub(crate) const fn to_uefi_time(&self, daylight: u8, timezone: i16) -> r_efi::efi::Time { | ||||||
system_time_internal::uefi_time_from_duration(self.0, daylight, timezone) | ||||||
} | ||||||
|
||||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> { | ||||||
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0) | ||||||
} | ||||||
|
@@ -79,7 +90,7 @@ pub(crate) mod system_time_internal { | |||||
|
||||||
let t = unsafe { t.assume_init() }; | ||||||
|
||||||
Some(SystemTime(uefi_time_to_duration(t))) | ||||||
Some(SystemTime::new(t)) | ||||||
} | ||||||
|
||||||
// This algorithm is based on the one described in the post | ||||||
|
@@ -112,6 +123,52 @@ pub(crate) mod system_time_internal { | |||||
|
||||||
Duration::new(utc_epoch, t.nanosecond) | ||||||
} | ||||||
|
||||||
/// This algorithm is taken from: http://howardhinnant.github.io/date_algorithms.html | ||||||
pub(crate) const fn uefi_time_from_duration( | ||||||
dur: crate::time::Duration, | ||||||
daylight: u8, | ||||||
timezone: i16, | ||||||
) -> r_efi::system::Time { | ||||||
const SECS_IN_MINUTE: u64 = 60; | ||||||
const SECS_IN_HOUR: u64 = SECS_IN_MINUTE * 60; | ||||||
const SECS_IN_DAY: u64 = SECS_IN_HOUR * 24; | ||||||
|
||||||
let secs = if timezone == r_efi::efi::UNSPECIFIED_TIMEZONE { | ||||||
dur.as_secs() | ||||||
} else { | ||||||
dur.as_secs().checked_add_signed(-timezone as i64).unwrap() | ||||||
}; | ||||||
let days = secs / SECS_IN_DAY; | ||||||
let remaining_secs = secs % SECS_IN_DAY; | ||||||
let z = days + 719468; | ||||||
let era = z / 146097; | ||||||
let doe = z - (era * 146097); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is clear by operator precedence, and the source doesn't use parenthesis here either.
Suggested change
|
||||||
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; | ||||||
let mut y = yoe + era * 400; | ||||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); | ||||||
let mp = (5 * doy + 2) / 153; | ||||||
let d = doy - (153 * mp + 2) / 5 + 1; | ||||||
let m = if mp < 10 { mp + 3 } else { mp - 9 }; | ||||||
|
||||||
if m <= 2 { | ||||||
y += 1; | ||||||
} | ||||||
|
||||||
r_efi::system::Time { | ||||||
year: y as u16, | ||||||
month: m as u8, | ||||||
day: d as u8, | ||||||
hour: (remaining_secs / SECS_IN_HOUR) as u8, | ||||||
minute: ((remaining_secs % SECS_IN_HOUR) / SECS_IN_MINUTE) as u8, | ||||||
second: ((remaining_secs % SECS_IN_HOUR) % SECS_IN_MINUTE) as u8, | ||||||
pad1: 0, | ||||||
nanosecond: dur.subsec_nanos(), | ||||||
timezone, | ||||||
daylight, | ||||||
pad2: 0, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
pub(crate) mod instant_internal { | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use subtraction here, just like the formula in the UEFI spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checked_sub_signed
is currently unstable. I can add the feature if that is fine though.