G-d willing
Hello,
I want to ask why there is no specific data type that only represent the time, It is a primitive data type, and it is needed in many use cases within the business flow.
In Haskell this is called TimeOfDay
.
Thanks,
G-d willing
Hello,
I want to ask why there is no specific data type that only represent the time, It is a primitive data type, and it is needed in many use cases within the business flow.
In Haskell this is called TimeOfDay
.
Thanks,
Hi Avraham,
I cannot state why the decision was made to not include a specific Data Type to represent Time of Day, but there is an example in the documents whereby you can construct a data type titled timeofday
yourself.
Please see the code in the Assertions section in the Adding Constraints to a Contract.
choice Redeem
: ()
controller owner
do
now <- getTime
let
today = toDateUTC now
dow = dayOfWeek today
timeofday = now `subTime` time today 0 0 0
hrs = convertRelTimeToMicroseconds timeofday / 3600000000
if (hrs < 8 || hrs > 18) then
abort $ "Cannot redeem outside business hours. Current time: " <> show timeofday
else case dow of
Saturday -> abort "Cannot redeem on a Saturday."
Sunday -> abort "Cannot redeem on a Sunday."
_ -> return ()
As you can see there is a lot of flexibility available with the Time function itself. Hope this gives you some context.
If you really want Haskell’s TimeOfDay
then one option is to convert the TimeOfDay module from hackage to Daml (https://hackage.haskell.org/package/time-1.12.2/docs/src/Data.Time.LocalTime.Internal.TimeOfDay.html).
This is a BSD-2 licensed module, so the licensing shouldn’t be a problem. DiffTime
becomes RelTime
in Daml; Rational
becomes BigDecimal
or a suitable Numeric n
type, depending on your need, and the strictness support is of course not relevant to Daml. Other than that the rest should transliterate with almost no changes.
Most of the important hackage packages are BSD licensed, so lifting libraries like this is something I do as a matter of course when the need arises.