diff options
author | tomsmeding <tom.smeding@gmail.com> | 2019-08-19 13:00:05 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2019-08-19 13:17:48 +0200 |
commit | 4e5590d148a7f2b517dce18c231b9d4cb0b1d19f (patch) | |
tree | dd04cecda1525f826f06752102ea2a58754fdbf7 /src/Utils/Time.hs |
Import of source files
Diffstat (limited to 'src/Utils/Time.hs')
-rw-r--r-- | src/Utils/Time.hs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/Utils/Time.hs b/src/Utils/Time.hs new file mode 100644 index 0000000..2139c53 --- /dev/null +++ b/src/Utils/Time.hs @@ -0,0 +1,43 @@ +{-| +Module : Utils.Time +Copyright : (c) UU, 2019 +License : MIT +Maintainer : Tom Smeding +Stability : experimental +Portability : POSIX, macOS, Windows +-} +module Utils.Time where + +import Data.Time.Clock +import Data.Time.Clock.System hiding (getSystemTime) +import qualified Data.Time.Clock.System as Sys +import Data.Time.Format + + +-- | A timestamp in milliseconds. +newtype TimeStamp = TimeStamp Int + deriving (Show, Read, Eq, Ord) + +-- | The number of milliseconds stored in the timestamp newtype. +timestampMS :: TimeStamp -> Int +timestampMS (TimeStamp n) = n + +-- | Offset the timestamp by the given number of milliseconds. +plusMS :: TimeStamp -> Int -> TimeStamp +plusMS (TimeStamp n) m = TimeStamp (n + m) + +-- | The number of milliseconds that the first timestamp is later than the +-- second. +subtractMS :: TimeStamp -> TimeStamp -> Int +subtractMS (TimeStamp n) (TimeStamp m) = n - m + +-- | Return the current system time as a timestamp. +getSystemTime :: IO TimeStamp +getSystemTime = TimeStamp . systemToMS <$> Sys.getSystemTime + where + systemToMS tm = let MkSystemTime s ns = truncateSystemTimeLeapSecond tm + in fromIntegral s * 1000 + fromIntegral ns `div` 1000000 + +-- | Show a UTCTime in ISO-8601 format. +iso8601Show :: UTCTime -> String +iso8601Show = formatTime defaultTimeLocale (iso8601DateFormat (Just "%H:%M:%S")) |