Unverified Commit e91b393b authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Cleanup and add more tests for from/to nanos (#2655)

## Motivation and Context
- Simplify no-op code in `as_nanos`
- Add more proptests

## Testing
Ran the tests


----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
parent 7f714db5
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -57,6 +57,9 @@ const NANOS_PER_SECOND_U32: u32 = 1_000_000_000;
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub struct DateTime {
    pub(crate) seconds: i64,
    /// Subsecond nanos always advances the wallclock time, even for times where seconds is negative
    ///
    /// Bigger subsecond nanos => later time
    pub(crate) subsecond_nanos: u32,
}

@@ -93,13 +96,8 @@ impl DateTime {
    /// Returns the number of nanoseconds since the Unix epoch that this `DateTime` represents.
    pub fn as_nanos(&self) -> i128 {
        let seconds = self.seconds as i128 * NANOS_PER_SECOND;
        if seconds < 0 {
            let adjusted_nanos = self.subsecond_nanos as i128 - NANOS_PER_SECOND;
            seconds + NANOS_PER_SECOND + adjusted_nanos
        } else {
        seconds + self.subsecond_nanos as i128
    }
    }

    /// Creates a `DateTime` from a number of seconds and a fractional second since the Unix epoch.
    ///