Rust: time not implemented on this platform

Has anyone run into this runtime error in Rust? time not implemented on this platform.

Just pointing out that it would be nice for the Rust CDK or the Wasm environment to implement these basic functionalities, time, randomness, etc for wasm32-unknown-unknown on the IC

Yes, I hinted about this on the other topic. I hit the same thing, and luckily there’s an api for that in the sdk! Here’s an example that I’ve tested, and works:

use ic_cdk::api::time;

#[update(name = "greetUpdate")]
fn greet_u(name: String) -> String {
    let greeting_store = storage::get_mut::<GreetingStore>();

    let message = format!("Updated Message: {}!", name);
    let timestamp = time();
    let p = ic_cdk::api::caller().to_text();

    let g = Greeting {
        message: message.clone(),
        timestamp,
        principal: p,
    };

    greeting_store.push(g);

    message
}
2 Likes

Awesome, yes I have independently used the ic-specific time functionality, but it would be nice to use std::time::Instant::now(), which my dependency was using. I just commented their code out, and yes I could probably use the ic-specific code, but it would be nice to not have to rewrite all of the already-existing libraries that will probably use std::time

1 Like

Yes, I’ve had to fork some libs to support ic time().

Mostly like:

pub fn u64_to_datatime(time: &u64) -> DateTime<Utc> {
    let sys_time = UNIX_EPOCH + Duration::from_nanos(time.clone());
    DateTime::<Utc>::from(sys_time)
}

If you are doing scheduling, time is constant within a single call. You could use an inter-canister model! I agree if there is support for native std::time that would save lots of time

1 Like

@botch and @gldev thanks for the examples, helped me solve a separate but related issue.