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

Adjusts windows local for millisecond and year #997

Closed
wants to merge 10 commits into from

Conversation

nekevss
Copy link
Contributor

@nekevss nekevss commented Mar 24, 2023

This PR is meant to address a couple fixes/potential gaps in the last request that I found when running the most recent 0.4.x on boa with the test262 Date built-in suite.

Primarily the changes are:

  • Adds support for millisecond, which to be frank was probably a complete oversight on my part.
  • Lowers the potential min year, by checking the year and adding a shifted field to LocalSysTime.
  • Adjusts the previous test case to account for the above

@@ -62,6 +63,7 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<Date
struct LocalSysTime {
inner: SYSTEMTIME,
offset: i32,
shifted: bool,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should get a docstring to explain what it means.

fn system_time_from_naive_date_time(dt: &NaiveDateTime) -> SYSTEMTIME {
SYSTEMTIME {
fn system_time_from_naive_date_time(dt: &NaiveDateTime) -> (SYSTEMTIME, bool) {
let (year, shifted) = if dt.year() < 1601 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if dt.year() is smaller than zero?

I also think we should shift year by some multiple of 400, to avoid getting into issues with leap year calculations. Ideally we'd cover this behavior with some testing.

Copy link
Contributor Author

@nekevss nekevss Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How low of a year does there need to be coverage for? We could go to the minimum year from naive internals, but it might need to be done iteratively due to the range. Or it can be cut off arbitrarily at a certain point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to go all the way to the naive minimum. We can probably do some calculation to figure out how many multiples of 400 we should subtract?

let date =
NaiveDate::from_ymd_opt(st.wYear as i32, st.wMonth as u32, st.wDay as u32).unwrap();
let time = NaiveTime::from_hms(st.wHour as u32, st.wMinute as u32, st.wSecond as u32);
let year = if self.shifted { st.wYear - 1600 } else { st.wYear };
Copy link
Contributor

@jtmoon79 jtmoon79 Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could value 1600 be a file-level const? Could a docstring explain the significance of year 1600?
Currently, 1600 is an opaque incantation.

fn system_time_from_naive_date_time(dt: &NaiveDateTime) -> SYSTEMTIME {
SYSTEMTIME {
fn system_time_from_naive_date_time(dt: &NaiveDateTime) -> (SYSTEMTIME, bool) {
let (year, shifted) = if dt.year() < 1601 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could value 1601 value be a file-level const with a helpful docstring? Currently, value 1601 is an opaque incantation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a docstring down in SYSTEMTIME that designates the valid values for Window's SYSTEMTIME struct (1601-30827). I don't see the exact issue with a const though


let offset = FixedOffset::east_opt(self.offset).unwrap();
DateTime::from_utc(date.and_time(time) - offset, offset)
}
}

fn system_time_from_naive_date_time(dt: &NaiveDateTime) -> SYSTEMTIME {
SYSTEMTIME {
fn system_time_from_naive_date_time(dt: &NaiveDateTime) -> (SYSTEMTIME, bool) {
Copy link
Contributor

@jtmoon79 jtmoon79 Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add test cases for only this function system_time_from_naive_date_time?
I can see this function is indirectly tested elsewhere but it seems good to me have tests focused solely on this function.

Placing the test within this file windows.rs wouldn't require changing the tested function's visibility.

@@ -68,7 +68,12 @@ struct LocalSysTime {
inner: SYSTEMTIME,
/// The offset value from UTC
offset: i32,
// Denotes the +/- 400 shifted a year value from invalid to valid
// Denotes the multiple of 400 year intervals shifted to create a valid year value. The
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great comment. While you're at it, can this be a docstring, e.g. leading /// ?

@nekevss nekevss requested review from djc and jtmoon79 March 31, 2023 00:08
@nekevss
Copy link
Contributor Author

nekevss commented Mar 31, 2023

Feedback changes made 😄 let me know what you think!

@pitdicker
Copy link
Collaborator

@nekevss Thank you for working on this! Going to close this PR because #1017 is the preferred solution, even though it is not yet merged.

@pitdicker pitdicker closed this Jan 31, 2024
@nekevss
Copy link
Contributor Author

nekevss commented Jan 31, 2024

@pitdicker Sounds good to me 😄 Definitely agree that #1017 would be the better approach as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants