aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--date/init.janet28
-rw-r--r--test/02-api.janet7
2 files changed, 35 insertions, 0 deletions
diff --git a/date/init.janet b/date/init.janet
index 858ef79..aaf2272 100644
--- a/date/init.janet
+++ b/date/init.janet
@@ -94,3 +94,31 @@
:year year
# UTC only
:isdst false}))
+
+(defn- format-fn
+ [time fmt f]
+ (def tm (f time))
+ (assert (tm? tm))
+ (:strftime tm fmt))
+
+(defn format
+ ```
+ Format a given `date/time` object according to the format.
+ If `utc?` is truthy, interpret it as UTC, else as your local timezone.
+ Format may either be a strftime-compatible string, or a preset keyword.
+ The following presets are available:
+ * :iso8601 (along with its -calendar, -ordinal, and -week variants)
+ * :rfc3339
+ * :html
+ * :rfc5322 (and its aliases: :email, :rfc5321, :internet)
+ * :default
+
+ Note that the iso8601 and rc3339 implementations are not perfectly compliant.
+ For details, see `src/tm.c#strftime_formats[]`.
+ ```
+ [time &opt fmt utc?]
+ (format-fn time
+ (or fmt :default)
+ (if utc?
+ native/gmtime
+ native/localtime)))
diff --git a/test/02-api.janet b/test/02-api.janet
index f759105..eb0666f 100644
--- a/test/02-api.janet
+++ b/test/02-api.janet
@@ -12,3 +12,10 @@
(def before (date/update-time* now {:sec -1 :min dec}))
(def after (date/update-time* now {:sec 61 :min inc}))
(assert (> after now before))
+
+# default format
+(assert (date/format now))
+# utc default format
+(assert (date/format now :default true))
+# format string
+(assert (date/format now :%c))