2016-03-15 20:50:03 +02:00
|
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;;
|
|
|
|
;; Copyright (c) 2015-2016 Andrey Antukh <niwi@niwi.nz>
|
|
|
|
|
2017-01-06 16:31:57 +01:00
|
|
|
(ns uxbox.util.time
|
2016-03-15 20:50:03 +02:00
|
|
|
(:require [cljsjs.moment]))
|
|
|
|
|
|
|
|
;; A simplified immutable date time representation type.
|
|
|
|
|
2017-01-06 16:31:57 +01:00
|
|
|
(deftype Instant [^number v]
|
2016-03-15 20:50:03 +02:00
|
|
|
IComparable
|
|
|
|
(-compare [this other]
|
2017-01-06 16:31:57 +01:00
|
|
|
(if (instance? Instant other)
|
2016-03-15 20:50:03 +02:00
|
|
|
(compare v (.-v other))
|
|
|
|
(throw (js/Error. (str "Cannot compare " this " to " other)))))
|
|
|
|
|
|
|
|
IPrintWithWriter
|
|
|
|
(-pr-writer [_ writer _]
|
|
|
|
(let [m (js/moment v)
|
|
|
|
ms (.toISOString m)]
|
2017-01-06 16:31:57 +01:00
|
|
|
(-write writer (str "#instant \"" ms "\""))))
|
2016-03-15 20:50:03 +02:00
|
|
|
|
|
|
|
Object
|
|
|
|
(toString [this]
|
|
|
|
(let [m (js/moment v)]
|
|
|
|
(.toISOString m)))
|
|
|
|
|
|
|
|
(equiv [this other]
|
|
|
|
(-equiv this other))
|
|
|
|
|
|
|
|
IHash
|
|
|
|
(-hash [_] v))
|
|
|
|
|
2017-01-06 16:31:57 +01:00
|
|
|
(alter-meta! #'->Instant assoc :private true)
|
2016-03-15 20:50:03 +02:00
|
|
|
|
2017-01-06 16:31:57 +01:00
|
|
|
(defn instant
|
|
|
|
"Create a new Instant instance from
|
2016-03-15 20:50:03 +02:00
|
|
|
unix offset."
|
|
|
|
[v]
|
|
|
|
{:pre [(number? v)]}
|
2017-01-06 16:31:57 +01:00
|
|
|
(Instant. v))
|
2016-03-15 20:50:03 +02:00
|
|
|
|
2017-01-06 16:25:21 +01:00
|
|
|
(defn instant?
|
2017-01-06 16:31:57 +01:00
|
|
|
"Return true if `v` is an instance of Instant."
|
2017-01-06 16:25:21 +01:00
|
|
|
[v]
|
2017-01-06 16:31:57 +01:00
|
|
|
(instance? Instant v))
|
2017-01-06 16:25:21 +01:00
|
|
|
|
2016-03-15 20:50:03 +02:00
|
|
|
(defn parse
|
2017-01-06 16:31:57 +01:00
|
|
|
"Parse a string representation of instant
|
2016-03-15 20:50:03 +02:00
|
|
|
with an optional `format` parameter."
|
|
|
|
([v] (parse v :offset))
|
|
|
|
([v fmt]
|
|
|
|
(cond
|
2017-01-06 16:31:57 +01:00
|
|
|
(instant? v) v
|
|
|
|
(= fmt :offset) (Instant. v)
|
2016-03-15 20:50:03 +02:00
|
|
|
:else (let [m (if (= fmt :unix)
|
|
|
|
(js/moment.unix v)
|
|
|
|
(js/moment v fmt))]
|
2017-01-06 16:31:57 +01:00
|
|
|
(Instant. (.valueOf m))))))
|
2016-03-15 20:50:03 +02:00
|
|
|
|
|
|
|
(defn format
|
2017-01-06 16:31:57 +01:00
|
|
|
"Returns a string representation of the Instant
|
2016-03-15 20:50:03 +02:00
|
|
|
instance with optional `fmt` format parameter.
|
|
|
|
|
|
|
|
You can use `:iso` and `:unix` shortcuts as
|
|
|
|
format parameter.
|
|
|
|
|
|
|
|
You can read more about format tokens here:
|
|
|
|
http://momentjs.com/docs/#/displaying/format/
|
|
|
|
"
|
|
|
|
([v] (format v :iso))
|
|
|
|
([v fmt]
|
2017-01-06 16:31:57 +01:00
|
|
|
{:pre [(instant? v)]}
|
2016-08-30 18:28:46 +03:00
|
|
|
(let [vm (js/moment (.-v v))]
|
2016-03-15 20:50:03 +02:00
|
|
|
(case fmt
|
|
|
|
:unix (.unix vm)
|
2016-03-15 22:26:33 +02:00
|
|
|
:offset (.valueOf vm)
|
2016-03-15 20:50:03 +02:00
|
|
|
:iso (.toISOString vm)
|
|
|
|
(.format vm fmt)))))
|
|
|
|
|
|
|
|
(defn now
|
2017-01-06 16:31:57 +01:00
|
|
|
"Return the current Instant."
|
2016-03-15 20:50:03 +02:00
|
|
|
[]
|
|
|
|
(let [vm (js/moment)]
|
2017-01-06 16:31:57 +01:00
|
|
|
(Instant. (.valueOf vm))))
|
2016-03-15 20:50:03 +02:00
|
|
|
|
|
|
|
(defn timeago
|
|
|
|
[v]
|
|
|
|
(let [dt (parse v)
|
|
|
|
vm (js/moment (.-v dt))]
|
|
|
|
(.fromNow vm)))
|
|
|
|
|
|
|
|
;; (defn day
|
|
|
|
;; [v]
|
|
|
|
;; (let [dt (parse v)
|
|
|
|
;; vm (js/moment (.-v dt))
|
|
|
|
;; fmt #js {:sameDay "[Today]"
|
|
|
|
;; :sameElse "[Today]"
|
|
|
|
;; :lastDay "[Yesterday]"
|
|
|
|
;; :lastWeek "[Last] dddd"}]
|
|
|
|
;; (.calendar vm nil fmt)))
|