summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-05 09:52:29 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-05 10:53:25 +0100
commit83063599f4ab1bcbbd9be9166e738a13ae4e4cc6 (patch)
treea7b0f40f269e3b9310f9f95d07fa3b4a17da30ec
parent41d93202f75a5db5239a65f3cc9bf08f11876ec0 (diff)
downloadnitrokey-rs-83063599f4ab1bcbbd9be9166e738a13ae4e4cc6.tar.gz
nitrokey-rs-83063599f4ab1bcbbd9be9166e738a13ae4e4cc6.tar.bz2
Fix example for GenerateOtp::get_totp_code
-rw-r--r--src/otp.rs22
-rw-r--r--tests/otp.rs5
2 files changed, 15 insertions, 12 deletions
diff --git a/src/otp.rs b/src/otp.rs
index 9f0a388..901bef9 100644
--- a/src/otp.rs
+++ b/src/otp.rs
@@ -155,7 +155,7 @@ pub trait GenerateOtp {
///
/// `time` is the number of seconds since January 1st, 1970 (Unix timestamp). Unless `force`
/// is set to `true`, this command fails if the timestamp on the device is larger than the
- /// given timestamp or if it is zero.
+ /// given timestamp or if it is zero.
///
/// The time is used for TOTP generation (see [`get_totp_code`][]).
///
@@ -297,21 +297,21 @@ pub trait GenerateOtp {
///
/// # Example
///
- /// ```ignore
- /// extern crate chrono;
- ///
+ /// ```no_run
+ /// use std::time;
/// use nitrokey::GenerateOtp;
/// # use nitrokey::CommandError;
///
/// # fn try_main() -> Result<(), CommandError> {
/// let device = nitrokey::connect()?;
- /// let time = Utc::now().timestamp();
- /// if time < 0 {
- /// println!("Timestamps before 1970-01-01 are not supported!");
- /// } else {
- /// device.set_time(time as u64);
- /// let code = device.get_totp_code(1)?;
- /// println!("Generated TOTP code on slot 1: {}", code);
+ /// let time = time::SystemTime::now().duration_since(time::UNIX_EPOCH);
+ /// match time {
+ /// Ok(time) => {
+ /// device.set_time(time.as_secs(), false)?;
+ /// let code = device.get_totp_code(1)?;
+ /// println!("Generated TOTP code on slot 1: {}", code);
+ /// },
+ /// Err(_) => println!("Timestamps before 1970-01-01 are not supported!"),
/// }
/// # Ok(())
/// # }
diff --git a/tests/otp.rs b/tests/otp.rs
index 8124c76..2b46088 100644
--- a/tests/otp.rs
+++ b/tests/otp.rs
@@ -63,7 +63,10 @@ fn check_hotp_codes(device: &GenerateOtp, offset: u8) {
fn set_time(device: DeviceWrapper) {
assert_eq!(Ok(()), device.set_time(1546385382, true));
assert_eq!(Ok(()), device.set_time(1546385392, false));
- assert_eq!(Err(CommandError::Timestamp), device.set_time(1546385292, false));
+ assert_eq!(
+ Err(CommandError::Timestamp),
+ device.set_time(1546385292, false)
+ );
assert_eq!(Ok(()), device.set_time(1546385382, true));
}