~pepe/bearimy

bbe7b418f0d3db5306a68d6ae2623da022b4e084 — Josef Pospíšil 1 year, 5 days ago 2ea60c0
Ensure right docs in core
2 files changed, 62 insertions(+), 21 deletions(-)

M bearimy/init.janet
M test/suite0.janet
M bearimy/init.janet => bearimy/init.janet +60 -21
@@ 4,18 4,24 @@
  (string/format "%.4i-%.2i-%.2i" y (inc m) (inc d)))

(def midnite
  "Start of the day struct."
  {:hours 0 :minutes 0 :seconds 0})

(def year-start
  "Start of the year struct."
  {:month 0 :year-day 0 :month 0 :month-day 0})

(def month-start
  "Start of the month struct."
  {:month-day 0})

(def week-days {:short ["Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"]
                :long ["Sunday" "Monday" "Tuesday" "Wednesday"
                       "Thursday" "Friday" "Saturday"]})
(def week-days
  "Register for week days names and abbrevs"
  {:short ["Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"]
   :long ["Sunday" "Monday" "Tuesday" "Wednesday"
          "Thursday" "Friday" "Saturday"]})
(def months
  "Register of months names and abbrevs."
  {:short
   ["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"]
   :long


@@ 23,6 29,7 @@
    "September" "October" "November" "December"]})

(def Date
  "Prototype for the `Date` objects"
  @{:format _format-date
    :epoch os/mktime
    :str-week-day


@@ 36,6 43,7 @@
    :local (fn [dt] (merge-into dt (os/date (os/mktime dt) true)))})

(def DateTime
  "Prototype for the `DateTime` objects"
  (table/setproto
    @{:format
      (fn [{:month m :year y :month-day d


@@ 54,15 62,19 @@

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

(defn now []
(defn now
  "Returns current `DateTime`."
  []
  (make DateTime (table-date)))

(defn today []
(defn today
  "Returns today's `Date`"
  []
  (make Date (merge (table-date) midnite)))

(defn- nc [c &opt dc] (fn [n] {c ((if dc dec identity) (scan-number n))}))
(defn- mc [& cs] (merge ;cs))
(def date-time-grammar
(def- date-time-grammar
  (peg/compile
    ~{:date-sep (set "-/")
      :time-sep ":"


@@ 86,28 98,33 @@
                                          (? (* :time-sep :seconds)))))))))))
               ,mc)}))

(defn from-string [x]
(defn- from-string [x]
  (merge
    year-start
    month-start
    midnite ;(peg/match date-time-grammar x)))

(defn normalize [x]
(defn- normalize [x]
  (case (type x)
    :table x
    :struct (merge x)
    :number (merge (os/date x))
    :string (from-string x)))

(defn make-date [date] # name?
(defn make-date
  "Convenience factory for creating `Date` objects."
  [date] # name?
  (def d (normalize date))
  (make Date (merge (os/date (os/mktime (merge d midnite))))))

(defn make-date-time [date-time]
(defn make-date-time
  "Convenience factory for creating `DateTime` objects."
  [date-time]
  (def dt (normalize date-time))
  (make DateTime (merge (os/date (os/mktime (merge dt))))))

(def Interval
  "Prototype for the `Interval` objects"
  @{:format
    (fn [{:duration dur}]
      (def h (math/floor (/ dur 3600)))


@@ 141,7 158,7 @@
  (default m 1)
  (fn [t] (* (scan-number t) m)))

(def duration-grammar
(def- duration-grammar
  (comptime
    (peg/compile
      ~{:num (range "09")


@@ 158,17 175,33 @@
        :colon (* :hrs ":" :mins (any (if ":" (* ":" :secs))))
        :main (+ (some :text) (some :colon))})))

(defn from-string-dur [s]
  [s]
(defn- from-string-dur [s]
  (-?> (peg/match duration-grammar s) sum))

(defn minutes [m] (* 60 (or m 0)))
(defn hours [h] (* 60 (minutes h)))
(defn days [d] (* 24 (hours d)))
(defn weeks [d] (* 7 (days d)))
(defn years [y] (* 365 (days y)))
(defn minutes
  "Returns amount of seconds in minutes `m`"
  [m]
  (* 60 (or m 0)))
(defn hours
  "Returns amount of seconds in hours `h`"
  [h]
  (* 60 (minutes h)))
(defn days
  "Returns amount of seconds in days `d`"
  [d]
  (* 24 (hours d)))
(defn weeks
  "Returns amount of seconds in weeks `w`"
  [d]
  (* 7 (days d)))
(defn years
  "Returns amount of seconds in years `y`"
  [y]
  (* 365 (days y)))

(defn make-interval [interval]
(defn make-interval
  "Convenience factory for creating `Interval` objects."
  [interval]
  (make
    Interval
    @{:duration


@@ 192,6 225,7 @@
        :string (from-string-dur interval))}))

(def Calendar
  "Prototype for the `Calendar` objects"
  (make
    DateTime
    @{:sooner


@@ 215,12 249,15 @@
      (fn [self date-time]
        (compare> self date-time))}))

(defn make-calendar [date-time]
(defn make-calendar
  "Convenience factory for creating `Calendar` objects."
  [date-time]
  (make
    Calendar
    (make-date-time date-time)))

(def Period
  "Prototype for the `Period` objects"
  (make
    Calendar
    @{:later


@@ 243,7 280,9 @@
      :end
      (fn [self] (+ (:epoch self) (self :duration)))}))

(defn make-period [calendar interval]
(defn make-period
  "Convenience factory for creating `Period` objects."
  [calendar interval]
  (make
    Period
    (merge (make-calendar calendar)

M test/suite0.janet => test/suite0.janet +2 -0
@@ 1,3 1,4 @@
(use marble)
(use spork/test)
(use /bearimy)



@@ 355,4 356,5 @@
(assert (= 13 ((:local (make-date-time time-stamp-struct)) :hours))
        "local date")

(assert-docs "/bearimy")
(end-suite)