Skip to content

Commit 9b35e8b

Browse files
committed
add a basic time_until_refresh implementation
1 parent 44852e5 commit 9b35e8b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/totp.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,28 @@ impl TOTP {
149149
}
150150
}
151151

152+
/// All helper methods for totp generation
153+
impl TOTP {
154+
155+
/// Returns the time in seconds until an OTP refresh is needed.
156+
///
157+
/// Just like the corresponding [`TOTP::get_otp`] method, this method
158+
/// takes the current system time in seconds.
159+
pub fn time_until_refresh(&self, time: u64) -> u64 {
160+
self.time_until_refresh_with_start(time, 0)
161+
}
162+
163+
/// Returns the time in seconds until an OTP refresh is needed.
164+
///
165+
/// Just like the corresponding [`TOTP::get_otp_with_custom_time_start`]
166+
/// method, this method takes the current time in seconds along with a
167+
/// specified start time in case an offset is desired. Both values must be
168+
/// in seconds.
169+
pub fn time_until_refresh_with_start(&self, time: u64, time_start: u64) -> u64 {
170+
(time - time_start) % period
171+
}
172+
}
173+
152174
/// All otp generation methods for the [`TOTP`] struct.
153175
impl TOTP {
154176
/// Generates and returns the TOTP value for the specified time.

0 commit comments

Comments
 (0)