sway
May 1, 2024, 9:58am
1
chain returns now - 4611686020141944336 value. how convert timestamp to seconds and milliseconds?
I just did a test
println!("{}", spark.timestamp().await.unwrap().value);
sleep(Duration::from_secs(1));
println!("{}", spark.timestamp().await.unwrap().value);
in fact, the pause is 3-4 seconds and the output result is 4611686020141945438
4611686020141945442. - here the conclusion is that we can only operate in seconds
1 Like
Fuel uses tai64
instead of unix
for our timestamps which is also already in seconds. Divide by 1000 to get milliseconds if you need to. Read more about tai64 here .
Example rust test below:
fn convert_to_datetime(timestamp: u64) -> DateTime<Utc> {
let unix = tai64::Tai64(timestamp).to_unix();
DateTime::from_timestamp(unix, 0).unwrap()
}
/// This test is here in addition to `can_set_custom_block_time` because even though this test
/// passed, the Sway `timestamp` function didn't take into account the block time change. This
/// was fixed and this test is here to demonstrate the fix.
#[tokio::test]
async fn test_sway_timestamp() -> Result<()> {
let block_time = 1u32; // seconds
let provider_config = NodeConfig {
block_production: Trigger::Interval {
block_time: std::time::Duration::from_secs(block_time.into()),
},
..NodeConfig::default()
};
let mut wallets = launch_custom_provider_and_get_wallets(
WalletsConfig::new(Some(1), Some(1), Some(100)),
Some(provider_config),
system
Closed
May 29, 2024, 6:50pm
3
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.