A peer-to-peer stack for code collaboration
Some functions related to time. (#333)
James Haydon4877d4b853df
diff --git a/docs/source/reference.rst b/docs/source/reference.rstindex c33aa58..746bcea 100644--- a/docs/source/reference.rst+++ b/docs/source/reference.rst@@ -387,6 +387,26 @@ Replaces the radicle state with the one provided.Given an atom ``x`` and a value ``v``, sets the value associated to``x`` in the current environment to be ``v``. Doesn't evaluate ``v``.+``timestamp?``+~~~~~~~~~~~~~~++Returns true if the input is an ISO 8601 formatted CoordinatedUniversal+Time (UTC) timestamp string. If the input isn't a string, an exception+is thrown.++``unix-epoch``+~~~~~~~~~~~~~~++Given an ISO 8601 formatted Coordinated Universal Time (UTC) timestamp,+returns the corresponding Unix epoch time, i.e., the number of seconds+since Jan 01 1970 (UTC).++``now!``+~~~~~~~~++Returns a timestamp for the current Coordinated Universal Time (UTC),+right now, formatted according to ISO 8601.+``to-json``~~~~~~~~~~~@@ -476,12 +496,6 @@ Open file in the specified mode (``:read``, ``:write``, ``:append``,Close a handle-``now!``-~~~~~~~~--Returns a timestamp for the current Coordinated Universal Time (UTC),-right now, formatted according to ISO 8601.-``system!``~~~~~~~~~~~@@ -1162,6 +1176,12 @@ and that the signature is valid for the rest of the dict for thatauthor. The rest of the dict is turned into a string according to``show``.+``(timestamp x)``+~~~~~~~~~~~~~~~~~++A validator which checks if a string is an ISO 8601 formatted+Coordinated Universal Time (UTC) timestamp.+``prelude/util``----------------diff --git a/rad/monadic/issue-remote.rad b/rad/monadic/issue-remote.radindex 302f7c5..4284387 100644--- a/rad/monadic/issue-remote.rad+++ b/rad/monadic/issue-remote.rad@@ -61,10 +61,6 @@(validator/pred (string-append "< " (show max-len) " chars in length")(fn [s] (< (string-length s) max-len)))])))-;; TODO correctly formatted date string-(def validator/timestamp- (validator/type :string))-(def input-keys{:nonce validator/nonce:chain-id (validator/= chain-id)})diff --git a/rad/prelude/validation.rad b/rad/prelude/validation.radindex d236f09..4ca89c3 100644--- a/rad/prelude/validation.rad+++ b/rad/prelude/validation.rad@@ -1,6 +1,10 @@{:module 'prelude/validation- :doc "Functions for creating or combining *validators*, which are functions which return the input unchanged or throw with an error message. These can be used for checking data before accepting it onto a chain."- :exports '[= member and or type pred integral optional-key contains contains-all contains-only key optional-keys keys every uuid signed]}+ :doc "Functions for creating or combining *validators*, which are functions+ which return the input unchanged or throw with an error message. These can be+ used for checking data before accepting it onto a chain."+ :exports+ '[= member and or type pred integral optional-key contains contains-all+ contains-only key optional-keys keys every uuid signed timestamp]}(import prelude/dict :unqualified)(import prelude/dict :as 'dict)@@ -254,3 +258,15 @@][ ok ==> :ok ])++(def timestamp+ "A validator which checks if a string is an ISO 8601 formatted Coordinated+ Universal Time (UTC) timestamp."+ (and [(type :string)+ (pred "valid timestamp" timestamp?)]))++(:test "timestamp"+ [ (timestamp "2018-12-12T14:10:21Z") ==> "2018-12-12T14:10:21Z"]+ [ (catch 'validation-failure+ (timestamp "12-12-2018T14:10:21Z")+ (fn [_] :ok)) ==> :ok ])diff --git a/reference-doc.yaml b/reference-doc.yamlindex 79aab13..1e5f308 100644--- a/reference-doc.yaml+++ b/reference-doc.yaml@@ -86,6 +86,10 @@ primFns:- pure-env- set-current-env- set-env!+# Time+- timestamp?+- unix-epoch+- now!# JSON- to-json# UUIDs@@ -106,7 +110,6 @@ primFns:- read-line-handle!- open-file!- close-handle!-- now!- system!- wait-for-process!- write-handle!diff --git a/src/Radicle/Internal/Effects.hs b/src/Radicle/Internal/Effects.hsindex b8046c9..82a76d9 100644--- a/src/Radicle/Internal/Effects.hs+++ b/src/Radicle/Internal/Effects.hs@@ -6,7 +6,6 @@ import Protolude hiding (TypeError, toList)import qualified Data.Map as Mapimport qualified Data.Text as T-import qualified Data.Time as Timeimport GHC.Exts (IsList(..))import System.Console.Haskeline( CompletionFunc@@ -27,6 +26,7 @@ import Radicle.Internal.Interpretimport Radicle.Internal.Number (isInt)import Radicle.Internal.Prettyimport Radicle.Internal.PrimFns+import Radicle.Internal.Time as Timeimport Radicle.Internal.Type (Type(..))import qualified Radicle.Internal.UUID as UUID@@ -323,7 +323,7 @@ replPrimFns sysArgs = fromList $ allDocs $, "Returns a timestamp for the current Coordinated Universal Time (UTC), right now, formatted according to ISO 8601.", \case[] -> do t <- currentTime- pure . String . toS . Time.formatTime Time.defaultTimeLocale (Time.iso8601DateFormat (Just "%H:%M:%SZ")) $ t+ pure . String . Time.formatTime $ txs -> throwErrorHere $ WrongNumberOfArgs "exit!" 0 (length xs)), ( "file-module!"diff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hsindex 243a1af..8717cd7 100644--- a/src/Radicle/Internal/PrimFns.hs+++ b/src/Radicle/Internal/PrimFns.hs@@ -21,6 +21,7 @@ import Radicle.Internal.Identifier (Ident(..), unsafeToIdent)import qualified Radicle.Internal.Number as Numimport Radicle.Internal.Parseimport Radicle.Internal.Pretty+import qualified Radicle.Internal.Time as Timeimport Radicle.Internal.Type (Type(..))import qualified Radicle.Internal.UUID as UUID@@ -577,6 +578,27 @@ purePrimFns = fromList $ allDocs $[v, Vec these, Keyword (Ident "unqualified")] -> import' v (Just these) Unqualified_ -> throwErrorHere $ OtherError "import: expects a module, an optional list of symbols to import, and an optional qualifier.")+ , ( "timestamp?"+ , "Returns true if the input is an ISO 8601 formatted Coordinated\+ \Universal Time (UTC) timestamp string. If the input isn't a string, an\+ \ exception is thrown."+ , oneArg "timestamp?" $ \case+ String s -> case Time.parseTime s of+ Just _ -> pure tt+ Nothing -> pure ff+ v -> throwErrorHere $ TypeError "timestamp?" 0 TString v+ )+ , ( "unix-epoch"+ , "Given an ISO 8601 formatted Coordinated Universal Time (UTC) timestamp\+ \, returns the corresponding Unix epoch time, i.e., the number of\+ \ seconds since Jan 01 1970 (UTC)."+ , oneArg "unix-epoch" $ \case+ String s -> do+ utc <- Time.parseTime s ?? toLangError (OtherError "Invalid UTC timestamp.")+ ss <- Time.unixSeconds utc ?? toLangError (OtherError "Could not convert UTC time to Unix epoch.")+ pure (Number (fromIntegral ss))+ v -> throwErrorHere $ TypeError "unix-epoch" 0 TString v+ )]whereisTy t = "Checks if the argument is a " <> t <> "."diff --git a/src/Radicle/Internal/Time.hs b/src/Radicle/Internal/Time.hsnew file mode 100644index 0000000..bd90f32--- /dev/null+++ b/src/Radicle/Internal/Time.hs@@ -0,0 +1,25 @@+module Radicle.Internal.Time where++import qualified Prelude+import Protolude++import qualified Data.Time as Time++locale :: Time.TimeLocale+locale = Time.defaultTimeLocale++fmt :: Prelude.String+fmt = Time.iso8601DateFormat (Just "%H:%M:%SZ")++parseTime :: Text -> Maybe Time.UTCTime+parseTime = Time.parseTimeM False locale fmt . toS++formatTime :: Time.UTCTime -> Text+formatTime = toS . Time.formatTime locale fmt++unixSeconds :: Time.UTCTime -> Maybe Int+unixSeconds utc =+ let secs = Time.formatTime Time.defaultTimeLocale "%s" utc+ in case readEither secs of+ Left _ -> Nothing+ Right (i :: Int) -> pure $ fromIntegral i