Skip to content

Commit

Permalink
refactor: Change how FileTime::SIGNED_MAX is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Jan 12, 2025
1 parent a9d1565 commit 7c36bd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/file_time/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,24 @@ impl FileTime {
/// value instead of [`FileTime::MAX`].
///
/// Note that the actual largest value of the [`SYSTEMTIME`] structure of
/// the Win32 API is "+30827-12-31 23:59:59.999000000", which is different
/// from this constant. The `FileTimeToSystemTime` function accepts the
/// value of this constant, but it is an invalid date and time for the
/// `SYSTEMTIME` structure.
/// the Win32 API is "+30827-12-31 23:59:59.999000000" (which is either in
/// UTC or local time, depending on the function that is being called),
/// which is different from this constant. The `FileTimeToSystemTime`
/// function accepts the value of this constant, but it is an invalid date
/// and time for the `SYSTEMTIME` structure.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "large-dates")]
/// # {
/// # use nt_time::{time::macros::datetime, FileTime};
/// #
/// # #[cfg(feature = "large-dates")]
/// assert_eq!(
/// FileTime::SIGNED_MAX,
/// datetime!(+30828-09-14 02:48:05.477_580_700 UTC)
/// );
/// # }
/// ```
///
/// [`FileTimeToSystemTime`]: https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-filetimetosystemtime
Expand All @@ -80,7 +83,7 @@ impl FileTime {
/// [`DateTime.ToFileTime`]: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tofiletime
/// [.NET]: https://dotnet.microsoft.com/
/// [`SYSTEMTIME`]: https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime
pub const SIGNED_MAX: Self = Self::new(u64::MAX >> 1);
pub const SIGNED_MAX: Self = Self::new(i64::MAX as u64);

/// The largest value that can be represented by the file time.
///
Expand All @@ -98,13 +101,15 @@ impl FileTime {
/// # Examples
///
/// ```
/// # #[cfg(feature = "large-dates")]
/// # {
/// # use nt_time::{time::macros::datetime, FileTime};
/// #
/// # #[cfg(feature = "large-dates")]
/// assert_eq!(
/// FileTime::MAX,
/// datetime!(+60056-05-28 05:36:10.955_161_500 UTC)
/// );
/// # }
/// ```
///
/// [`FILETIME`]: https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
Expand Down
14 changes: 9 additions & 5 deletions src/file_time/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,35 +143,37 @@ impl TryFrom<FileTime> for OffsetDateTime {
/// time represents after "9999-12-31 23:59:59.999999900 UTC":
///
/// ```
/// # #[cfg(not(feature = "large-dates"))]
/// # {
/// # use nt_time::{time::OffsetDateTime, FileTime};
/// #
/// # #[cfg(not(feature = "large-dates"))]
/// assert!(OffsetDateTime::try_from(FileTime::new(2_650_467_744_000_000_000)).is_err());
/// # }
/// ```
///
/// With the `large-dates` feature enabled, this always succeeds:
///
/// ```
/// # #[cfg(feature = "large-dates")]
/// # {
/// # use nt_time::{
/// # time::{macros::datetime, OffsetDateTime},
/// # FileTime,
/// # };
/// #
/// # #[cfg(feature = "large-dates")]
/// assert_eq!(
/// OffsetDateTime::try_from(FileTime::new(2_650_467_744_000_000_000)).unwrap(),
/// datetime!(+10000-01-01 00:00 UTC)
/// );
/// # #[cfg(feature = "large-dates")]
/// assert_eq!(
/// OffsetDateTime::try_from(FileTime::SIGNED_MAX).unwrap(),
/// datetime!(+30828-09-14 02:48:05.477_580_700 UTC)
/// );
/// # #[cfg(feature = "large-dates")]
/// assert_eq!(
/// OffsetDateTime::try_from(FileTime::MAX).unwrap(),
/// datetime!(+60056-05-28 05:36:10.955_161_500 UTC)
/// );
/// # }
/// ```
#[inline]
fn try_from(ft: FileTime) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -362,16 +364,18 @@ impl TryFrom<OffsetDateTime> for FileTime {
/// UTC":
///
/// ```
/// # #[cfg(feature = "large-dates")]
/// # {
/// # use nt_time::{
/// # time::{macros::datetime, Duration},
/// # FileTime,
/// # };
/// #
/// # #[cfg(feature = "large-dates")]
/// assert!(FileTime::try_from(
/// datetime!(+60056-05-28 05:36:10.955_161_500 UTC) + Duration::nanoseconds(100)
/// )
/// .is_err());
/// # }
/// ```
#[inline]
fn try_from(dt: OffsetDateTime) -> Result<Self, Self::Error> {
Expand Down

0 comments on commit 7c36bd7

Please sign in to comment.