{-# LANGUAGE DeriveAnyClass       #-}
{-# LANGUAGE DeriveGeneric        #-}
{-# LANGUAGE DerivingStrategies   #-}
{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE MonoLocalBinds       #-}
{-# LANGUAGE NoImplicitPrelude    #-}
{-# LANGUAGE OverloadedStrings    #-}
{-# LANGUAGE TemplateHaskell      #-}
{-# LANGUAGE UndecidableInstances #-}
-- Otherwise we get a complaint about the 'fromIntegral' call in the generated instance of 'Integral' for 'Ada'
{-# OPTIONS_GHC -Wno-identities #-}
{-# OPTIONS_GHC -fno-ignore-interface-pragmas #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}

-- | UTCTime and UTCTime ranges.
module Plutus.V1.Ledger.Time(
      POSIXTime(..)
    , POSIXTimeRange
    , DiffMilliSeconds(..)
    , fromMilliSeconds
    ) where

import Codec.Serialise.Class (Serialise)
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON (parseJSON), FromJSONKey, ToJSON (toJSON), ToJSONKey, Value (Number))
import Data.Aeson.Types (prependFailure, typeMismatch)
import Data.Hashable (Hashable)
import Data.Scientific (floatingOrInteger, scientific)
import GHC.Generics (Generic)
import Plutus.V1.Ledger.Interval
import PlutusTx qualified
import PlutusTx.Lift (makeLift)
import PlutusTx.Prelude
import Prelude qualified as Haskell
import Prettyprinter (Pretty (pretty), (<+>))


-- | This is a length of time, as measured by a number of milliseconds.
newtype DiffMilliSeconds = DiffMilliSeconds Integer
  deriving stock (DiffMilliSeconds -> DiffMilliSeconds -> Bool
(DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> (DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> Eq DiffMilliSeconds
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c/= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
== :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c== :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
Haskell.Eq, Eq DiffMilliSeconds
Eq DiffMilliSeconds
-> (DiffMilliSeconds -> DiffMilliSeconds -> Ordering)
-> (DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> (DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> (DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> (DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> Ord DiffMilliSeconds
DiffMilliSeconds -> DiffMilliSeconds -> Bool
DiffMilliSeconds -> DiffMilliSeconds -> Ordering
DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$cmin :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
max :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$cmax :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
>= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c>= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
> :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c> :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
<= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c<= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
< :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c< :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
compare :: DiffMilliSeconds -> DiffMilliSeconds -> Ordering
$ccompare :: DiffMilliSeconds -> DiffMilliSeconds -> Ordering
$cp1Ord :: Eq DiffMilliSeconds
Haskell.Ord, Int -> DiffMilliSeconds -> ShowS
[DiffMilliSeconds] -> ShowS
DiffMilliSeconds -> String
(Int -> DiffMilliSeconds -> ShowS)
-> (DiffMilliSeconds -> String)
-> ([DiffMilliSeconds] -> ShowS)
-> Show DiffMilliSeconds
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DiffMilliSeconds] -> ShowS
$cshowList :: [DiffMilliSeconds] -> ShowS
show :: DiffMilliSeconds -> String
$cshow :: DiffMilliSeconds -> String
showsPrec :: Int -> DiffMilliSeconds -> ShowS
$cshowsPrec :: Int -> DiffMilliSeconds -> ShowS
Haskell.Show, (forall x. DiffMilliSeconds -> Rep DiffMilliSeconds x)
-> (forall x. Rep DiffMilliSeconds x -> DiffMilliSeconds)
-> Generic DiffMilliSeconds
forall x. Rep DiffMilliSeconds x -> DiffMilliSeconds
forall x. DiffMilliSeconds -> Rep DiffMilliSeconds x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DiffMilliSeconds x -> DiffMilliSeconds
$cfrom :: forall x. DiffMilliSeconds -> Rep DiffMilliSeconds x
Generic)
  deriving anyclass (Value -> Parser [DiffMilliSeconds]
Value -> Parser DiffMilliSeconds
(Value -> Parser DiffMilliSeconds)
-> (Value -> Parser [DiffMilliSeconds])
-> FromJSON DiffMilliSeconds
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [DiffMilliSeconds]
$cparseJSONList :: Value -> Parser [DiffMilliSeconds]
parseJSON :: Value -> Parser DiffMilliSeconds
$cparseJSON :: Value -> Parser DiffMilliSeconds
FromJSON, FromJSONKeyFunction [DiffMilliSeconds]
FromJSONKeyFunction DiffMilliSeconds
FromJSONKeyFunction DiffMilliSeconds
-> FromJSONKeyFunction [DiffMilliSeconds]
-> FromJSONKey DiffMilliSeconds
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
fromJSONKeyList :: FromJSONKeyFunction [DiffMilliSeconds]
$cfromJSONKeyList :: FromJSONKeyFunction [DiffMilliSeconds]
fromJSONKey :: FromJSONKeyFunction DiffMilliSeconds
$cfromJSONKey :: FromJSONKeyFunction DiffMilliSeconds
FromJSONKey, [DiffMilliSeconds] -> Encoding
[DiffMilliSeconds] -> Value
DiffMilliSeconds -> Encoding
DiffMilliSeconds -> Value
(DiffMilliSeconds -> Value)
-> (DiffMilliSeconds -> Encoding)
-> ([DiffMilliSeconds] -> Value)
-> ([DiffMilliSeconds] -> Encoding)
-> ToJSON DiffMilliSeconds
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [DiffMilliSeconds] -> Encoding
$ctoEncodingList :: [DiffMilliSeconds] -> Encoding
toJSONList :: [DiffMilliSeconds] -> Value
$ctoJSONList :: [DiffMilliSeconds] -> Value
toEncoding :: DiffMilliSeconds -> Encoding
$ctoEncoding :: DiffMilliSeconds -> Encoding
toJSON :: DiffMilliSeconds -> Value
$ctoJSON :: DiffMilliSeconds -> Value
ToJSON, ToJSONKeyFunction [DiffMilliSeconds]
ToJSONKeyFunction DiffMilliSeconds
ToJSONKeyFunction DiffMilliSeconds
-> ToJSONKeyFunction [DiffMilliSeconds]
-> ToJSONKey DiffMilliSeconds
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
toJSONKeyList :: ToJSONKeyFunction [DiffMilliSeconds]
$ctoJSONKeyList :: ToJSONKeyFunction [DiffMilliSeconds]
toJSONKey :: ToJSONKeyFunction DiffMilliSeconds
$ctoJSONKey :: ToJSONKeyFunction DiffMilliSeconds
ToJSONKey, DiffMilliSeconds -> ()
(DiffMilliSeconds -> ()) -> NFData DiffMilliSeconds
forall a. (a -> ()) -> NFData a
rnf :: DiffMilliSeconds -> ()
$crnf :: DiffMilliSeconds -> ()
NFData)
  deriving newtype (Integer -> DiffMilliSeconds
DiffMilliSeconds -> DiffMilliSeconds
DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
(DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds)
-> (Integer -> DiffMilliSeconds)
-> Num DiffMilliSeconds
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> DiffMilliSeconds
$cfromInteger :: Integer -> DiffMilliSeconds
signum :: DiffMilliSeconds -> DiffMilliSeconds
$csignum :: DiffMilliSeconds -> DiffMilliSeconds
abs :: DiffMilliSeconds -> DiffMilliSeconds
$cabs :: DiffMilliSeconds -> DiffMilliSeconds
negate :: DiffMilliSeconds -> DiffMilliSeconds
$cnegate :: DiffMilliSeconds -> DiffMilliSeconds
* :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$c* :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
- :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$c- :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
+ :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$c+ :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
Haskell.Num, DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
(DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> AdditiveSemigroup DiffMilliSeconds
forall a. (a -> a -> a) -> AdditiveSemigroup a
+ :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$c+ :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
AdditiveSemigroup, AdditiveSemigroup DiffMilliSeconds
DiffMilliSeconds
AdditiveSemigroup DiffMilliSeconds
-> DiffMilliSeconds -> AdditiveMonoid DiffMilliSeconds
forall a. AdditiveSemigroup a -> a -> AdditiveMonoid a
zero :: DiffMilliSeconds
$czero :: DiffMilliSeconds
$cp1AdditiveMonoid :: AdditiveSemigroup DiffMilliSeconds
AdditiveMonoid, AdditiveMonoid DiffMilliSeconds
AdditiveMonoid DiffMilliSeconds
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> AdditiveGroup DiffMilliSeconds
DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
forall a. AdditiveMonoid a -> (a -> a -> a) -> AdditiveGroup a
- :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$c- :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$cp1AdditiveGroup :: AdditiveMonoid DiffMilliSeconds
AdditiveGroup, Int -> DiffMilliSeconds
DiffMilliSeconds -> Int
DiffMilliSeconds -> [DiffMilliSeconds]
DiffMilliSeconds -> DiffMilliSeconds
DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds]
DiffMilliSeconds
-> DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds]
(DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds)
-> (Int -> DiffMilliSeconds)
-> (DiffMilliSeconds -> Int)
-> (DiffMilliSeconds -> [DiffMilliSeconds])
-> (DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds])
-> (DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds])
-> (DiffMilliSeconds
    -> DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds])
-> Enum DiffMilliSeconds
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: DiffMilliSeconds
-> DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds]
$cenumFromThenTo :: DiffMilliSeconds
-> DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds]
enumFromTo :: DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds]
$cenumFromTo :: DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds]
enumFromThen :: DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds]
$cenumFromThen :: DiffMilliSeconds -> DiffMilliSeconds -> [DiffMilliSeconds]
enumFrom :: DiffMilliSeconds -> [DiffMilliSeconds]
$cenumFrom :: DiffMilliSeconds -> [DiffMilliSeconds]
fromEnum :: DiffMilliSeconds -> Int
$cfromEnum :: DiffMilliSeconds -> Int
toEnum :: Int -> DiffMilliSeconds
$ctoEnum :: Int -> DiffMilliSeconds
pred :: DiffMilliSeconds -> DiffMilliSeconds
$cpred :: DiffMilliSeconds -> DiffMilliSeconds
succ :: DiffMilliSeconds -> DiffMilliSeconds
$csucc :: DiffMilliSeconds -> DiffMilliSeconds
Haskell.Enum, DiffMilliSeconds -> DiffMilliSeconds -> Bool
(DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> Eq DiffMilliSeconds
forall a. (a -> a -> Bool) -> Eq a
== :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c== :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
Eq, Eq DiffMilliSeconds
Eq DiffMilliSeconds
-> (DiffMilliSeconds -> DiffMilliSeconds -> Ordering)
-> (DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> (DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> (DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> (DiffMilliSeconds -> DiffMilliSeconds -> Bool)
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> Ord DiffMilliSeconds
DiffMilliSeconds -> DiffMilliSeconds -> Bool
DiffMilliSeconds -> DiffMilliSeconds -> Ordering
DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$cmin :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
max :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$cmax :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
>= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c>= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
> :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c> :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
<= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c<= :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
< :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
$c< :: DiffMilliSeconds -> DiffMilliSeconds -> Bool
compare :: DiffMilliSeconds -> DiffMilliSeconds -> Ordering
$ccompare :: DiffMilliSeconds -> DiffMilliSeconds -> Ordering
$cp1Ord :: Eq DiffMilliSeconds
Ord, Num DiffMilliSeconds
Ord DiffMilliSeconds
Num DiffMilliSeconds
-> Ord DiffMilliSeconds
-> (DiffMilliSeconds -> Rational)
-> Real DiffMilliSeconds
DiffMilliSeconds -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: DiffMilliSeconds -> Rational
$ctoRational :: DiffMilliSeconds -> Rational
$cp2Real :: Ord DiffMilliSeconds
$cp1Real :: Num DiffMilliSeconds
Haskell.Real, Enum DiffMilliSeconds
Real DiffMilliSeconds
Real DiffMilliSeconds
-> Enum DiffMilliSeconds
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds)
-> (DiffMilliSeconds
    -> DiffMilliSeconds -> (DiffMilliSeconds, DiffMilliSeconds))
-> (DiffMilliSeconds
    -> DiffMilliSeconds -> (DiffMilliSeconds, DiffMilliSeconds))
-> (DiffMilliSeconds -> Integer)
-> Integral DiffMilliSeconds
DiffMilliSeconds -> Integer
DiffMilliSeconds
-> DiffMilliSeconds -> (DiffMilliSeconds, DiffMilliSeconds)
DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: DiffMilliSeconds -> Integer
$ctoInteger :: DiffMilliSeconds -> Integer
divMod :: DiffMilliSeconds
-> DiffMilliSeconds -> (DiffMilliSeconds, DiffMilliSeconds)
$cdivMod :: DiffMilliSeconds
-> DiffMilliSeconds -> (DiffMilliSeconds, DiffMilliSeconds)
quotRem :: DiffMilliSeconds
-> DiffMilliSeconds -> (DiffMilliSeconds, DiffMilliSeconds)
$cquotRem :: DiffMilliSeconds
-> DiffMilliSeconds -> (DiffMilliSeconds, DiffMilliSeconds)
mod :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$cmod :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
div :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$cdiv :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
rem :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$crem :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
quot :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$cquot :: DiffMilliSeconds -> DiffMilliSeconds -> DiffMilliSeconds
$cp2Integral :: Enum DiffMilliSeconds
$cp1Integral :: Real DiffMilliSeconds
Haskell.Integral, Decoder s DiffMilliSeconds
Decoder s [DiffMilliSeconds]
[DiffMilliSeconds] -> Encoding
DiffMilliSeconds -> Encoding
(DiffMilliSeconds -> Encoding)
-> (forall s. Decoder s DiffMilliSeconds)
-> ([DiffMilliSeconds] -> Encoding)
-> (forall s. Decoder s [DiffMilliSeconds])
-> Serialise DiffMilliSeconds
forall s. Decoder s [DiffMilliSeconds]
forall s. Decoder s DiffMilliSeconds
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [DiffMilliSeconds]
$cdecodeList :: forall s. Decoder s [DiffMilliSeconds]
encodeList :: [DiffMilliSeconds] -> Encoding
$cencodeList :: [DiffMilliSeconds] -> Encoding
decode :: Decoder s DiffMilliSeconds
$cdecode :: forall s. Decoder s DiffMilliSeconds
encode :: DiffMilliSeconds -> Encoding
$cencode :: DiffMilliSeconds -> Encoding
Serialise, Int -> DiffMilliSeconds -> Int
DiffMilliSeconds -> Int
(Int -> DiffMilliSeconds -> Int)
-> (DiffMilliSeconds -> Int) -> Hashable DiffMilliSeconds
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: DiffMilliSeconds -> Int
$chash :: DiffMilliSeconds -> Int
hashWithSalt :: Int -> DiffMilliSeconds -> Int
$chashWithSalt :: Int -> DiffMilliSeconds -> Int
Hashable, DiffMilliSeconds -> BuiltinData
(DiffMilliSeconds -> BuiltinData) -> ToData DiffMilliSeconds
forall a. (a -> BuiltinData) -> ToData a
toBuiltinData :: DiffMilliSeconds -> BuiltinData
$ctoBuiltinData :: DiffMilliSeconds -> BuiltinData
PlutusTx.ToData, BuiltinData -> Maybe DiffMilliSeconds
(BuiltinData -> Maybe DiffMilliSeconds)
-> FromData DiffMilliSeconds
forall a. (BuiltinData -> Maybe a) -> FromData a
fromBuiltinData :: BuiltinData -> Maybe DiffMilliSeconds
$cfromBuiltinData :: BuiltinData -> Maybe DiffMilliSeconds
PlutusTx.FromData, BuiltinData -> DiffMilliSeconds
(BuiltinData -> DiffMilliSeconds)
-> UnsafeFromData DiffMilliSeconds
forall a. (BuiltinData -> a) -> UnsafeFromData a
unsafeFromBuiltinData :: BuiltinData -> DiffMilliSeconds
$cunsafeFromBuiltinData :: BuiltinData -> DiffMilliSeconds
PlutusTx.UnsafeFromData)

makeLift ''DiffMilliSeconds

-- | POSIX time is measured as the number of milliseconds since 1970-01-01T00:00:00Z
newtype POSIXTime = POSIXTime { POSIXTime -> Integer
getPOSIXTime :: Integer }
  deriving stock (POSIXTime -> POSIXTime -> Bool
(POSIXTime -> POSIXTime -> Bool)
-> (POSIXTime -> POSIXTime -> Bool) -> Eq POSIXTime
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: POSIXTime -> POSIXTime -> Bool
$c/= :: POSIXTime -> POSIXTime -> Bool
== :: POSIXTime -> POSIXTime -> Bool
$c== :: POSIXTime -> POSIXTime -> Bool
Haskell.Eq, Eq POSIXTime
Eq POSIXTime
-> (POSIXTime -> POSIXTime -> Ordering)
-> (POSIXTime -> POSIXTime -> Bool)
-> (POSIXTime -> POSIXTime -> Bool)
-> (POSIXTime -> POSIXTime -> Bool)
-> (POSIXTime -> POSIXTime -> Bool)
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> Ord POSIXTime
POSIXTime -> POSIXTime -> Bool
POSIXTime -> POSIXTime -> Ordering
POSIXTime -> POSIXTime -> POSIXTime
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: POSIXTime -> POSIXTime -> POSIXTime
$cmin :: POSIXTime -> POSIXTime -> POSIXTime
max :: POSIXTime -> POSIXTime -> POSIXTime
$cmax :: POSIXTime -> POSIXTime -> POSIXTime
>= :: POSIXTime -> POSIXTime -> Bool
$c>= :: POSIXTime -> POSIXTime -> Bool
> :: POSIXTime -> POSIXTime -> Bool
$c> :: POSIXTime -> POSIXTime -> Bool
<= :: POSIXTime -> POSIXTime -> Bool
$c<= :: POSIXTime -> POSIXTime -> Bool
< :: POSIXTime -> POSIXTime -> Bool
$c< :: POSIXTime -> POSIXTime -> Bool
compare :: POSIXTime -> POSIXTime -> Ordering
$ccompare :: POSIXTime -> POSIXTime -> Ordering
$cp1Ord :: Eq POSIXTime
Haskell.Ord, Int -> POSIXTime -> ShowS
[POSIXTime] -> ShowS
POSIXTime -> String
(Int -> POSIXTime -> ShowS)
-> (POSIXTime -> String)
-> ([POSIXTime] -> ShowS)
-> Show POSIXTime
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [POSIXTime] -> ShowS
$cshowList :: [POSIXTime] -> ShowS
show :: POSIXTime -> String
$cshow :: POSIXTime -> String
showsPrec :: Int -> POSIXTime -> ShowS
$cshowsPrec :: Int -> POSIXTime -> ShowS
Haskell.Show, (forall x. POSIXTime -> Rep POSIXTime x)
-> (forall x. Rep POSIXTime x -> POSIXTime) -> Generic POSIXTime
forall x. Rep POSIXTime x -> POSIXTime
forall x. POSIXTime -> Rep POSIXTime x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep POSIXTime x -> POSIXTime
$cfrom :: forall x. POSIXTime -> Rep POSIXTime x
Generic)
  deriving anyclass (FromJSONKeyFunction [POSIXTime]
FromJSONKeyFunction POSIXTime
FromJSONKeyFunction POSIXTime
-> FromJSONKeyFunction [POSIXTime] -> FromJSONKey POSIXTime
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
fromJSONKeyList :: FromJSONKeyFunction [POSIXTime]
$cfromJSONKeyList :: FromJSONKeyFunction [POSIXTime]
fromJSONKey :: FromJSONKeyFunction POSIXTime
$cfromJSONKey :: FromJSONKeyFunction POSIXTime
FromJSONKey, ToJSONKeyFunction [POSIXTime]
ToJSONKeyFunction POSIXTime
ToJSONKeyFunction POSIXTime
-> ToJSONKeyFunction [POSIXTime] -> ToJSONKey POSIXTime
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
toJSONKeyList :: ToJSONKeyFunction [POSIXTime]
$ctoJSONKeyList :: ToJSONKeyFunction [POSIXTime]
toJSONKey :: ToJSONKeyFunction POSIXTime
$ctoJSONKey :: ToJSONKeyFunction POSIXTime
ToJSONKey, POSIXTime -> ()
(POSIXTime -> ()) -> NFData POSIXTime
forall a. (a -> ()) -> NFData a
rnf :: POSIXTime -> ()
$crnf :: POSIXTime -> ()
NFData)
  deriving newtype (POSIXTime -> POSIXTime -> POSIXTime
(POSIXTime -> POSIXTime -> POSIXTime)
-> AdditiveSemigroup POSIXTime
forall a. (a -> a -> a) -> AdditiveSemigroup a
+ :: POSIXTime -> POSIXTime -> POSIXTime
$c+ :: POSIXTime -> POSIXTime -> POSIXTime
AdditiveSemigroup, AdditiveSemigroup POSIXTime
POSIXTime
AdditiveSemigroup POSIXTime
-> POSIXTime -> AdditiveMonoid POSIXTime
forall a. AdditiveSemigroup a -> a -> AdditiveMonoid a
zero :: POSIXTime
$czero :: POSIXTime
$cp1AdditiveMonoid :: AdditiveSemigroup POSIXTime
AdditiveMonoid, AdditiveMonoid POSIXTime
AdditiveMonoid POSIXTime
-> (POSIXTime -> POSIXTime -> POSIXTime) -> AdditiveGroup POSIXTime
POSIXTime -> POSIXTime -> POSIXTime
forall a. AdditiveMonoid a -> (a -> a -> a) -> AdditiveGroup a
- :: POSIXTime -> POSIXTime -> POSIXTime
$c- :: POSIXTime -> POSIXTime -> POSIXTime
$cp1AdditiveGroup :: AdditiveMonoid POSIXTime
AdditiveGroup, POSIXTime -> POSIXTime -> Bool
(POSIXTime -> POSIXTime -> Bool) -> Eq POSIXTime
forall a. (a -> a -> Bool) -> Eq a
== :: POSIXTime -> POSIXTime -> Bool
$c== :: POSIXTime -> POSIXTime -> Bool
Eq, Eq POSIXTime
Eq POSIXTime
-> (POSIXTime -> POSIXTime -> Ordering)
-> (POSIXTime -> POSIXTime -> Bool)
-> (POSIXTime -> POSIXTime -> Bool)
-> (POSIXTime -> POSIXTime -> Bool)
-> (POSIXTime -> POSIXTime -> Bool)
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> Ord POSIXTime
POSIXTime -> POSIXTime -> Bool
POSIXTime -> POSIXTime -> Ordering
POSIXTime -> POSIXTime -> POSIXTime
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: POSIXTime -> POSIXTime -> POSIXTime
$cmin :: POSIXTime -> POSIXTime -> POSIXTime
max :: POSIXTime -> POSIXTime -> POSIXTime
$cmax :: POSIXTime -> POSIXTime -> POSIXTime
>= :: POSIXTime -> POSIXTime -> Bool
$c>= :: POSIXTime -> POSIXTime -> Bool
> :: POSIXTime -> POSIXTime -> Bool
$c> :: POSIXTime -> POSIXTime -> Bool
<= :: POSIXTime -> POSIXTime -> Bool
$c<= :: POSIXTime -> POSIXTime -> Bool
< :: POSIXTime -> POSIXTime -> Bool
$c< :: POSIXTime -> POSIXTime -> Bool
compare :: POSIXTime -> POSIXTime -> Ordering
$ccompare :: POSIXTime -> POSIXTime -> Ordering
$cp1Ord :: Eq POSIXTime
Ord, Integer -> POSIXTime
POSIXTime -> Integer
POSIXTime -> POSIXTime
(POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime)
-> (Integer -> POSIXTime)
-> (POSIXTime -> Integer)
-> Enum POSIXTime
forall a.
(a -> a) -> (a -> a) -> (Integer -> a) -> (a -> Integer) -> Enum a
fromEnum :: POSIXTime -> Integer
$cfromEnum :: POSIXTime -> Integer
toEnum :: Integer -> POSIXTime
$ctoEnum :: Integer -> POSIXTime
pred :: POSIXTime -> POSIXTime
$cpred :: POSIXTime -> POSIXTime
succ :: POSIXTime -> POSIXTime
$csucc :: POSIXTime -> POSIXTime
Enum, POSIXTime -> BuiltinData
(POSIXTime -> BuiltinData) -> ToData POSIXTime
forall a. (a -> BuiltinData) -> ToData a
toBuiltinData :: POSIXTime -> BuiltinData
$ctoBuiltinData :: POSIXTime -> BuiltinData
PlutusTx.ToData, BuiltinData -> Maybe POSIXTime
(BuiltinData -> Maybe POSIXTime) -> FromData POSIXTime
forall a. (BuiltinData -> Maybe a) -> FromData a
fromBuiltinData :: BuiltinData -> Maybe POSIXTime
$cfromBuiltinData :: BuiltinData -> Maybe POSIXTime
PlutusTx.FromData, BuiltinData -> POSIXTime
(BuiltinData -> POSIXTime) -> UnsafeFromData POSIXTime
forall a. (BuiltinData -> a) -> UnsafeFromData a
unsafeFromBuiltinData :: BuiltinData -> POSIXTime
$cunsafeFromBuiltinData :: BuiltinData -> POSIXTime
PlutusTx.UnsafeFromData)
  deriving newtype (Integer -> POSIXTime
POSIXTime -> POSIXTime
POSIXTime -> POSIXTime -> POSIXTime
(POSIXTime -> POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime)
-> (Integer -> POSIXTime)
-> Num POSIXTime
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> POSIXTime
$cfromInteger :: Integer -> POSIXTime
signum :: POSIXTime -> POSIXTime
$csignum :: POSIXTime -> POSIXTime
abs :: POSIXTime -> POSIXTime
$cabs :: POSIXTime -> POSIXTime
negate :: POSIXTime -> POSIXTime
$cnegate :: POSIXTime -> POSIXTime
* :: POSIXTime -> POSIXTime -> POSIXTime
$c* :: POSIXTime -> POSIXTime -> POSIXTime
- :: POSIXTime -> POSIXTime -> POSIXTime
$c- :: POSIXTime -> POSIXTime -> POSIXTime
+ :: POSIXTime -> POSIXTime -> POSIXTime
$c+ :: POSIXTime -> POSIXTime -> POSIXTime
Haskell.Num, Int -> POSIXTime
POSIXTime -> Int
POSIXTime -> [POSIXTime]
POSIXTime -> POSIXTime
POSIXTime -> POSIXTime -> [POSIXTime]
POSIXTime -> POSIXTime -> POSIXTime -> [POSIXTime]
(POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime)
-> (Int -> POSIXTime)
-> (POSIXTime -> Int)
-> (POSIXTime -> [POSIXTime])
-> (POSIXTime -> POSIXTime -> [POSIXTime])
-> (POSIXTime -> POSIXTime -> [POSIXTime])
-> (POSIXTime -> POSIXTime -> POSIXTime -> [POSIXTime])
-> Enum POSIXTime
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: POSIXTime -> POSIXTime -> POSIXTime -> [POSIXTime]
$cenumFromThenTo :: POSIXTime -> POSIXTime -> POSIXTime -> [POSIXTime]
enumFromTo :: POSIXTime -> POSIXTime -> [POSIXTime]
$cenumFromTo :: POSIXTime -> POSIXTime -> [POSIXTime]
enumFromThen :: POSIXTime -> POSIXTime -> [POSIXTime]
$cenumFromThen :: POSIXTime -> POSIXTime -> [POSIXTime]
enumFrom :: POSIXTime -> [POSIXTime]
$cenumFrom :: POSIXTime -> [POSIXTime]
fromEnum :: POSIXTime -> Int
$cfromEnum :: POSIXTime -> Int
toEnum :: Int -> POSIXTime
$ctoEnum :: Int -> POSIXTime
pred :: POSIXTime -> POSIXTime
$cpred :: POSIXTime -> POSIXTime
succ :: POSIXTime -> POSIXTime
$csucc :: POSIXTime -> POSIXTime
Haskell.Enum, Num POSIXTime
Ord POSIXTime
Num POSIXTime
-> Ord POSIXTime -> (POSIXTime -> Rational) -> Real POSIXTime
POSIXTime -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: POSIXTime -> Rational
$ctoRational :: POSIXTime -> Rational
$cp2Real :: Ord POSIXTime
$cp1Real :: Num POSIXTime
Haskell.Real, Enum POSIXTime
Real POSIXTime
Real POSIXTime
-> Enum POSIXTime
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime -> POSIXTime)
-> (POSIXTime -> POSIXTime -> (POSIXTime, POSIXTime))
-> (POSIXTime -> POSIXTime -> (POSIXTime, POSIXTime))
-> (POSIXTime -> Integer)
-> Integral POSIXTime
POSIXTime -> Integer
POSIXTime -> POSIXTime -> (POSIXTime, POSIXTime)
POSIXTime -> POSIXTime -> POSIXTime
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: POSIXTime -> Integer
$ctoInteger :: POSIXTime -> Integer
divMod :: POSIXTime -> POSIXTime -> (POSIXTime, POSIXTime)
$cdivMod :: POSIXTime -> POSIXTime -> (POSIXTime, POSIXTime)
quotRem :: POSIXTime -> POSIXTime -> (POSIXTime, POSIXTime)
$cquotRem :: POSIXTime -> POSIXTime -> (POSIXTime, POSIXTime)
mod :: POSIXTime -> POSIXTime -> POSIXTime
$cmod :: POSIXTime -> POSIXTime -> POSIXTime
div :: POSIXTime -> POSIXTime -> POSIXTime
$cdiv :: POSIXTime -> POSIXTime -> POSIXTime
rem :: POSIXTime -> POSIXTime -> POSIXTime
$crem :: POSIXTime -> POSIXTime -> POSIXTime
quot :: POSIXTime -> POSIXTime -> POSIXTime
$cquot :: POSIXTime -> POSIXTime -> POSIXTime
$cp2Integral :: Enum POSIXTime
$cp1Integral :: Real POSIXTime
Haskell.Integral, Decoder s POSIXTime
Decoder s [POSIXTime]
[POSIXTime] -> Encoding
POSIXTime -> Encoding
(POSIXTime -> Encoding)
-> (forall s. Decoder s POSIXTime)
-> ([POSIXTime] -> Encoding)
-> (forall s. Decoder s [POSIXTime])
-> Serialise POSIXTime
forall s. Decoder s [POSIXTime]
forall s. Decoder s POSIXTime
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [POSIXTime]
$cdecodeList :: forall s. Decoder s [POSIXTime]
encodeList :: [POSIXTime] -> Encoding
$cencodeList :: [POSIXTime] -> Encoding
decode :: Decoder s POSIXTime
$cdecode :: forall s. Decoder s POSIXTime
encode :: POSIXTime -> Encoding
$cencode :: POSIXTime -> Encoding
Serialise, Int -> POSIXTime -> Int
POSIXTime -> Int
(Int -> POSIXTime -> Int)
-> (POSIXTime -> Int) -> Hashable POSIXTime
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: POSIXTime -> Int
$chash :: POSIXTime -> Int
hashWithSalt :: Int -> POSIXTime -> Int
$chashWithSalt :: Int -> POSIXTime -> Int
Hashable)

-- | Custom `FromJSON` instance which allows to parse a JSON number to a
-- 'POSIXTime' value. The parsed JSON value MUST be an 'Integer' or else the
-- parsing fails.
instance FromJSON POSIXTime where
  parseJSON :: Value -> Parser POSIXTime
parseJSON v :: Value
v@(Number Scientific
n) =
      (Double -> Parser POSIXTime)
-> (Integer -> Parser POSIXTime)
-> Either Double Integer
-> Parser POSIXTime
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\Double
_ -> String -> Parser POSIXTime -> Parser POSIXTime
forall a. String -> Parser a -> Parser a
prependFailure String
"parsing POSIXTime failed, " (String -> Value -> Parser POSIXTime
forall a. String -> Value -> Parser a
typeMismatch String
"Integer" Value
v))
             (POSIXTime -> Parser POSIXTime
forall (m :: * -> *) a. Monad m => a -> m a
Haskell.return (POSIXTime -> Parser POSIXTime)
-> (Integer -> POSIXTime) -> Integer -> Parser POSIXTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> POSIXTime
POSIXTime)
             (Scientific -> Either Double Integer
forall r i. (RealFloat r, Integral i) => Scientific -> Either r i
floatingOrInteger Scientific
n :: Either Haskell.Double Integer)
  parseJSON Value
invalid =
      String -> Parser POSIXTime -> Parser POSIXTime
forall a. String -> Parser a -> Parser a
prependFailure String
"parsing POSIXTime failed, " (String -> Value -> Parser POSIXTime
forall a. String -> Value -> Parser a
typeMismatch String
"Number" Value
invalid)

-- | Custom 'ToJSON' instance which allows to simply convert a 'POSIXTime'
-- value to a JSON number.
instance ToJSON POSIXTime where
  toJSON :: POSIXTime -> Value
toJSON (POSIXTime Integer
n) = Scientific -> Value
Number (Scientific -> Value) -> Scientific -> Value
forall a b. (a -> b) -> a -> b
$ Integer -> Int -> Scientific
scientific Integer
n Int
0

makeLift ''POSIXTime

instance Pretty POSIXTime where
  pretty :: POSIXTime -> Doc ann
pretty (POSIXTime Integer
i) = Doc ann
"POSIXTime" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Integer -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Integer
i

-- | An 'Interval' of 'POSIXTime's.
type POSIXTimeRange = Interval POSIXTime

-- | Simple conversion from 'DiffMilliSeconds' to 'POSIXTime'.
{-# INLINABLE fromMilliSeconds #-}
fromMilliSeconds :: DiffMilliSeconds -> POSIXTime
fromMilliSeconds :: DiffMilliSeconds -> POSIXTime
fromMilliSeconds (DiffMilliSeconds Integer
s) = Integer -> POSIXTime
POSIXTime Integer
s