Nim noob here.
Is this valid? Is there a better way?
let myDuration = cast[Duration](getTime())
In Nim the cast operation is for reinterpretting a type as another, type conversions are done.
float(anInt)
anInt.float #thanks to ufcs
It also seems duration is meant for offseting/finding the difference between time. First of all is, what do you want?
Do you want a timestamp, a duration, how a cast work, the 'let' statement, something with time or all together?
Have a look at here: https://nim-lang.org/docs/times.html#Duration (To create a new Duration, use initDuration proc.)
proc initDuration https://nim-lang.org/docs/times.html#initDuration,int64,int64,int64,int64,int64,int64,int64,int64
and an example here: https://docs.w3cub.com/nim/times/
In Google type in: Nim Duration. (only as help, first the word Nim and what you are searching. Sometimes is difficult to find something. Is not Python :-) )
A timestamp is just a duration since an origin, like the epoch time since Jan 1, 1970. times lets you create one easily, though. You can just say
let dur = initDuration(seconds=3600*24*10_000)
to get a Duration-typed interval of 10,000 days since Jan 1, 1970.To get elapsed nanoseconds since the epoch you can
import times
proc epochNanos(t:Time):int64=
t.toUnix*1_000_000_000 + t.nanosecond
echo getTime().epochNanos
import times
proc epochNanos(t:Time):int64=
t.toUnix*1_000_000_000 + t.nanosecond
echo getTime().epochNanos
You are right that it is not suitable to use the timestamp library if the use case beyond time bound from 1677-09-21T00:12:43.145Z to 2262-04-11T23:47:16.854Z
It is more plausible to use the library for time-series data which usually just the time of measurements and a lot for data points collected over time. So a compact and fast time representation of time is more flavourful.
64 bits is "numerically plenty",
Numpy took an approach of timedelta64 that supplants 64-bit integer with units (Y,M,D,h,...ms,us,ns...) creating a balance between time span and resolution. Nim can do it more elegantly using distinct int64 types and converters.