~pepe/bearimy

96989609c77f4cfa4d1898d9d2a65ae539786d64 — Josef Pospíšil 3 months ago a62b7f0
Local only for format
4 files changed, 40 insertions(+), 18 deletions(-)

M bearimy/init.janet
M bearimy/utils.janet
M test/suite0.janet
M test/suite1.janet
M bearimy/init.janet => bearimy/init.janet +4 -2
@@ 32,7 32,8 @@
    :str-month
    (fn [{:month m} &opt frm]
      (default frm :short)
      (get-in months [frm m]))})
      (get-in months [frm m]))
    :local (fn [dt] (merge-into dt (os/date (os/mktime dt) true)))})

(def DateTime
  (table/setproto


@@ 47,7 48,8 @@
        (def {:month m :year y :month-day d :week-day wd
              :minutes u :hours h :seconds s} (os/date (:epoch self)))
        (string/format "%s, %.2i %s %.4i %i:%.2i:%.2i GMT"
                       (get-in week-days [:short wd]) (inc d) (get-in months [:short m]) y h u s))}
                       (get-in week-days [:short wd]) (inc d)
                       (get-in months [:short m]) y h u s))}
    Date))

(defn- table-date [] (merge (os/date)))

M bearimy/utils.janet => bearimy/utils.janet +12 -9
@@ 1,7 1,10 @@
(use marble)
(use ./init)

(defn format-date-time [dt]
  (:format (make-date-time dt)))
(defn format-date-time [dt &opt local]
  (default local (dyn :local-time))
  (:format (cond-> (make-date-time dt)
                   local :local)))

(defn http-format-date-time [dt]
  (:http-format (make-date-time dt)))


@@ 9,21 12,21 @@
(defn format-date [dt]
  (:format (make-date dt)))

(defn format-time [dt]
  ((string/split " " (format-date-time dt)) 1))

(defn format-interval [i]
  (:format (make-interval i)))

(defn format-time [dt]
  (def t (make-date-time dt))
  (string/format "%.2i:%.2i" (t :hours) (t :minutes)))
(defn format-time [dt &opt local]
  (def t (cond-> (make-date-time dt)
                 local :local))
  (def h (t :hours))
  (string/format (. "%." (if (zero? h) 1 2) "i:%.2i")
                 h (t :minutes)))

(defn format-today []
  (:format (today)))

(defn format-now []
  (:format (now)))
  (format-date-time (now)))

(defn days-ago [n &opt tdy]
  (default tdy (today))

M test/suite0.janet => test/suite0.janet +3 -0
@@ 352,4 352,7 @@

(assert (= (:in-minutes (make-interval {:hours 1})) 60) "in-minutes")

(assert (= 13 ((:local (make-date-time time-stamp-struct)) :hours))
        "local date")

(end-suite)

M test/suite1.janet => test/suite1.janet +21 -7
@@ 4,22 4,36 @@

(start-suite 1)

(assert (= (format-date-time 1615478528)
           "2021-03-11 16:02:08")
(def tepoch 1615503728)
(assert (= (format-date-time tepoch)
           "2021-03-11 23:02:08")
        "format-date-time")

(assert (= (http-format-date-time 1615478528)
           "Thu, 11 Mar 2021 16:02:08 GMT")
(assert (= (format-date-time tepoch true)
           "2021-03-12 0:02:08")
        "local format-date-time")

(with-dyns [:local-time true]
  (assert (= (format-date-time tepoch)
             "2021-03-12 0:02:08")
          "local dyn format-date-time"))

(assert (= (http-format-date-time tepoch)
           "Thu, 11 Mar 2021 23:02:08 GMT")
        "format-date-time")

(assert (= (format-date 1615478528)
(assert (= (format-date tepoch)
           "2021-03-11")
        "format-date")

(assert (= (format-time 1615478528)
           "16:02")
(assert (= (format-time tepoch)
           "23:02")
        "format-time")

(assert (= (format-time tepoch true)
           "0:02")
        "local format-time")

(assert (= (format-interval 1_000_002)
           "277:46:42")
        "format-interval")