Skip to content

Commit

Permalink
Merge pull request #62 from moia-oss/subsec_nanos
Browse files Browse the repository at this point in the history
Add as_seconds and as_subsecond_nanos
  • Loading branch information
urmaul authored Jul 26, 2024
2 parents 8456c9e + eb50043 commit bf7bdf9
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,37 @@ impl Time {
Time::from(SystemTime::now())
}

/// Returns the number of whole seconds in the time.
///
/// # Examples
///
/// ```
/// # use tinytime::Time;
/// assert_eq!(Time::minutes(1).as_seconds(), 60);
/// ```
#[must_use]
pub const fn as_seconds(&self) -> i64 {
self.0 / Self::SECOND.0
}

#[must_use]
pub const fn as_millis(&self) -> i64 {
self.0
}

/// Returns the number of subsecond millis converted to nanos.
///
/// # Examples
///
/// ```
/// # use tinytime::Time;
/// assert_eq!(Time::millis(12345).as_subsecond_nanos(), 345_000_000);
/// ```
#[must_use]
pub const fn as_subsecond_nanos(&self) -> i32 {
(self.0 % Self::SECOND.0 * 1_000_000) as i32
}

/// Rounds time down to a step size
///
/// # Examples
Expand Down

0 comments on commit bf7bdf9

Please sign in to comment.