FuelLabs / sway

🌴 Empowering everyone to build reliable and efficient smart contracts.
https://docs.fuel.network/docs/sway/
Apache License 2.0
62.79k stars 5.36k forks source link

Consider generating more readable timestamps in the standard library #3945

Open mohammadfawaz opened 1 year ago

mohammadfawaz commented 1 year ago

Currently, the standard library provides timestamp functions that return values in TAI64 format which is hard to read. It would be nice to provide abstractions on top of that to allow retrieving common time periods such as day, hour, etc.

nfurfaro commented 1 year ago

as a reference, this is what solidity provides users in this regard: image

mohammadfawaz commented 1 year ago

I find the syntax 1 days quite novel in programming languages.. It's certainly foreign for languages like Rust/C++. Can we do simple constants instead?

const SECOND = 1;
const MINUTE = 60;
const HOUR = 3600;
...
nfurfaro commented 1 year ago

Constants are certainly simpler. If they were associated costs on a Time type, we could implement conversions between them. I suppose free functions could be used to achieve the same though.

I was originally imagining something like the following:

pub struct Minutes {
    seconds: u64
}

impl Minutes {
    fn new(min: u64) -> Self {
        Minutes {
            seconds: min * 60,
        }
    }
}

let 5_minutes = Minutes::new(5)
mohammadfawaz commented 1 year ago

If they were associated consts on a Time type

I like this - we will have this soon