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

Change the behavior of DateTime::from_msdos when value is invalid #145

Closed
sorairolake opened this issue May 22, 2024 · 1 comment · Fixed by #149
Closed

Change the behavior of DateTime::from_msdos when value is invalid #145

sorairolake opened this issue May 22, 2024 · 1 comment · Fixed by #149
Labels
enhancement New feature or request fix merged Fix is merged, but the issue will stay open until it's released.

Comments

@sorairolake
Copy link
Contributor

Is your feature request related to a problem? Please describe.
DateTime::from_msdos can create a DateTime value even if datepart or timepart is an invalid value.

zip2/src/types.rs

Lines 139 to 156 in 6c60c67

/// Converts an msdos (u16, u16) pair to a DateTime object
pub const fn from_msdos(datepart: u16, timepart: u16) -> DateTime {
let seconds = (timepart & 0b0000000000011111) << 1;
let minutes = (timepart & 0b0000011111100000) >> 5;
let hours = (timepart & 0b1111100000000000) >> 11;
let days = datepart & 0b0000000000011111;
let months = (datepart & 0b0000000111100000) >> 5;
let years = (datepart & 0b1111111000000000) >> 9;
DateTime {
year: years + 1980,
month: months as u8,
day: days as u8,
hour: hours as u8,
minute: minutes as u8,
second: seconds as u8,
}
}

Describe the solution you'd like
I would like one of the following:

A clear and concise description of what you want to happen.

  1. Make DateTime::from_msdos an unsafe function.
  2. Returns Err if datepart or timepart is invalid.
  3. Panics if datepart or timepart is invalid.

Describe alternatives you've considered
Specify in the documentation that an invalid DateTime value may be created.

@sorairolake sorairolake added the enhancement New feature or request label May 22, 2024
Pr0methean added a commit that referenced this issue May 23, 2024
@Pr0methean Pr0methean added the fix merged Fix is merged, but the issue will stay open until it's released. label May 23, 2024
@sorairolake
Copy link
Contributor Author

sorairolake commented May 24, 2024

zip2/src/types.rs

Lines 188 to 205 in 94765a4

/// Converts an msdos (u16, u16) pair to a DateTime object if it represents a valid date and
/// time.
pub fn try_from_msdos(datepart: u16, timepart: u16) -> Result<DateTime, DateTimeRangeError> {
let seconds = (timepart & 0b0000000000011111) << 1;
let minutes = (timepart & 0b0000011111100000) >> 5;
let hours = (timepart & 0b1111100000000000) >> 11;
let days = datepart & 0b0000000000011111;
let months = (datepart & 0b0000000111100000) >> 5;
let years = (datepart & 0b1111111000000000) >> 9;
Self::from_date_and_time(
years.checked_add(1980).ok_or(DateTimeRangeError)?,
months.try_into()?,
days.try_into()?,
hours.try_into()?,
minutes.try_into()?,
seconds.try_into()?,
)
}

This may also be defined as TryFrom<[u16; 2]> for DateTime and/or TryFrom<(u16, u16)> for DateTime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix merged Fix is merged, but the issue will stay open until it's released.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants