From b348ed305dfac3ef7e9b45e4cfa08dc8467a458e Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 8 Jan 2024 12:15:45 +1000 Subject: [PATCH 1/2] Export functions for creating Timepoint/Duration --- soroban-sdk/src/env.rs | 1 - soroban-sdk/src/num.rs | 26 ++++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index c62aed798..2fac893ba 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -76,7 +76,6 @@ pub mod internal { } pub use internal::xdr; -pub use internal::Compare; pub use internal::ConversionError; pub use internal::EnvBase; pub use internal::Error; diff --git a/soroban-sdk/src/num.rs b/soroban-sdk/src/num.rs index ae162f25d..5718b69f1 100644 --- a/soroban-sdk/src/num.rs +++ b/soroban-sdk/src/num.rs @@ -459,15 +459,19 @@ pub struct Timepoint { impl_num_wrapping_val_type!(Timepoint, TimepointVal, TimepointSmall); impl Timepoint { - fn from_u64(env: &Env, u: u64) -> Timepoint { - let val = TimepointVal::try_from_val(env, &u).unwrap_optimized(); + /// Create a Timepoint from a unix time in seconds, the time in seconds + /// since January 1, 1970 UTC. + pub fn from_unix(env: &Env, seconds: u64) -> Timepoint { + let val = TimepointVal::try_from_val(env, &seconds).unwrap_optimized(); Timepoint { env: env.clone(), val, } } - fn to_u64(&self) -> u64 { + /// Returns the Timepoint as unix time in seconds, the time in seconds since + /// January 1, 1970 UTC. + pub fn to_unix(&self) -> u64 { u64::try_from_val(self.env(), &self.to_val_type()).unwrap_optimized() } } @@ -482,15 +486,17 @@ pub struct Duration { impl_num_wrapping_val_type!(Duration, DurationVal, DurationSmall); impl Duration { - fn from_u64(env: &Env, u: u64) -> Duration { - let val = DurationVal::try_from_val(env, &u).unwrap_optimized(); + /// Create a Duration from seconds. + fn from_seconds(env: &Env, seconds: u64) -> Duration { + let val = DurationVal::try_from_val(env, &seconds).unwrap_optimized(); Duration { env: env.clone(), val, } } - fn to_u64(&self) -> u64 { + /// Returns the Duration as seconds. + fn to_seconds(&self) -> u64 { u64::try_from_val(self.env(), &self.to_val_type()).unwrap_optimized() } } @@ -575,8 +581,8 @@ mod test { fn test_timepoint_roundtrip() { let env = Env::default(); - let tp = Timepoint::from_u64(&env, 123); - let u = tp.to_u64(); + let tp = Timepoint::from_unix(&env, 123); + let u = tp.to_unix(); assert_eq!(u, 123); } @@ -584,8 +590,8 @@ mod test { fn test_duration_roundtrip() { let env = Env::default(); - let tp = Duration::from_u64(&env, 123); - let u = tp.to_u64(); + let tp = Duration::from_seconds(&env, 123); + let u = tp.to_seconds(); assert_eq!(u, 123); } From 049aad25c7389b25c87792477e913260d87f7ba4 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 8 Jan 2024 12:25:52 +1000 Subject: [PATCH 2/2] fix pub --- soroban-sdk/src/num.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/soroban-sdk/src/num.rs b/soroban-sdk/src/num.rs index 5718b69f1..d7063f183 100644 --- a/soroban-sdk/src/num.rs +++ b/soroban-sdk/src/num.rs @@ -487,7 +487,7 @@ impl_num_wrapping_val_type!(Duration, DurationVal, DurationSmall); impl Duration { /// Create a Duration from seconds. - fn from_seconds(env: &Env, seconds: u64) -> Duration { + pub fn from_seconds(env: &Env, seconds: u64) -> Duration { let val = DurationVal::try_from_val(env, &seconds).unwrap_optimized(); Duration { env: env.clone(), @@ -496,7 +496,7 @@ impl Duration { } /// Returns the Duration as seconds. - fn to_seconds(&self) -> u64 { + pub fn to_seconds(&self) -> u64 { u64::try_from_val(self.env(), &self.to_val_type()).unwrap_optimized() } }