Skip to content

Commit

Permalink
Export functions for creating Timepoint/Duration (#1201)
Browse files Browse the repository at this point in the history
### What
Export functions for creating and accessing Timepoint and Duration.

### Why
The types are not creatable inside a contract, and outside a contract in
tests are only creatable using the XDR ScVal type. If developers are to
use the Timepoint and Duration types, they need to be able to construct
the types from primitive values.

The `Timepoint` `from_unix` and `to_unix` functions were added using the
unix terminologgy because that's the type of time values the Stellar
network uses.

The `Duration` `from_seconds` and `to_seconds` functions were added
using the seconds terminology because it seemed like the most direct way
to communicate intent. I originally intended to use `secs` which I think
would also be reasonable and feedback on the issue suggested `seconds`
which also seems reasonable to me.

While for both types functions were repurposed/removed, those functions
were private only and should have no impact on compatibility.

Close #1197
  • Loading branch information
leighmcculloch authored Jan 17, 2024
1 parent 405a261 commit eb9f7e2
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions soroban-sdk/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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.
pub 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.
pub fn to_seconds(&self) -> u64 {
u64::try_from_val(self.env(), &self.to_val_type()).unwrap_optimized()
}
}
Expand Down Expand Up @@ -575,17 +581,17 @@ 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);
}

#[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);
}

Expand Down

0 comments on commit eb9f7e2

Please sign in to comment.