2016-05-20 11:20:48 +00:00
|
|
|
(ns status-im.utils.datetime
|
2017-03-17 14:37:54 +00:00
|
|
|
(:require [cljs-time.core :as t :refer [date-time plus days hours before?]]
|
2016-10-06 13:15:57 +00:00
|
|
|
[cljs-time.coerce :refer [from-long to-long from-date]]
|
2016-08-04 15:36:13 +00:00
|
|
|
[cljs-time.format :refer [formatters
|
|
|
|
formatter
|
|
|
|
unparse]]
|
2016-06-30 16:00:44 +00:00
|
|
|
[status-im.i18n :refer [label label-pluralize]]
|
|
|
|
[goog.string :as gstring]
|
2018-01-04 01:13:12 +00:00
|
|
|
goog.string.format
|
|
|
|
goog.i18n.DateTimeFormat
|
|
|
|
[clojure.string :as s]
|
|
|
|
[goog.object :refer [get]]))
|
2016-08-04 15:36:13 +00:00
|
|
|
|
2017-03-17 14:37:54 +00:00
|
|
|
(defn now []
|
|
|
|
(t/now))
|
|
|
|
|
2016-08-04 15:36:13 +00:00
|
|
|
(def hour (* 1000 60 60))
|
|
|
|
(def day (* hour 24))
|
|
|
|
(def week (* 7 day))
|
2016-06-30 16:00:44 +00:00
|
|
|
(def units [{:name :t/datetime-second :limit 60 :in-second 1}
|
|
|
|
{:name :t/datetime-minute :limit 3600 :in-second 60}
|
|
|
|
{:name :t/datetime-hour :limit 86400 :in-second 3600}
|
|
|
|
{:name :t/datetime-day :limit nil :in-second 86400}])
|
2016-05-20 09:41:55 +00:00
|
|
|
|
|
|
|
(def time-zone-offset (hours (- (/ (.getTimezoneOffset (js/Date.)) 60))))
|
|
|
|
|
2018-01-04 01:13:12 +00:00
|
|
|
;; xx-YY locale, xx locale or en fallback
|
|
|
|
(defn- locale-symbols [locale-name]
|
|
|
|
(if-let [loc (get goog.i18n (str "DateTimeSymbols_" locale-name))]
|
|
|
|
loc
|
|
|
|
(let [name-first (s/replace (or locale-name "") #"-.*$" "")
|
|
|
|
loc (get goog.i18n (str "DateTimeSymbols_" name-first))]
|
|
|
|
(or loc goog.i18n.DateTimeSymbols_en))))
|
|
|
|
|
|
|
|
;; Closure does not have an enum for datetime formats
|
|
|
|
(def short-date-time-format 10)
|
|
|
|
(def short-date-format 2)
|
|
|
|
|
|
|
|
(defn mk-fmt [locale format]
|
|
|
|
(goog.i18n.DateTimeFormat. format (locale-symbols locale)))
|
|
|
|
|
|
|
|
(def date-time-fmt
|
|
|
|
(mk-fmt status-im.i18n/locale short-date-time-format))
|
|
|
|
(def date-fmt
|
|
|
|
(mk-fmt status-im.i18n/locale short-date-format))
|
|
|
|
|
|
|
|
(defn- to-str [ms old-fmt-fn yesterday-fmt-fn today-fmt-fn]
|
|
|
|
(let [date (from-long ms)
|
|
|
|
local (plus date time-zone-offset)
|
|
|
|
today (t/today-at-midnight)
|
|
|
|
yesterday (plus today (days -1))]
|
|
|
|
(cond
|
|
|
|
(before? date yesterday) (old-fmt-fn local)
|
|
|
|
(before? date today) (yesterday-fmt-fn local)
|
|
|
|
:else (today-fmt-fn local))))
|
|
|
|
|
|
|
|
(defn to-short-str [ms]
|
|
|
|
(to-str ms
|
|
|
|
#(.format date-fmt %)
|
|
|
|
#(label :t/datetime-yesterday)
|
|
|
|
#(unparse (formatters :hour-minute) %)))
|
|
|
|
|
|
|
|
(defn day-relative [ms]
|
|
|
|
(to-str ms
|
2018-03-20 18:23:08 +00:00
|
|
|
#(.format date-fmt %)
|
2018-01-04 01:13:12 +00:00
|
|
|
#(label :t/datetime-yesterday)
|
|
|
|
#(label :t/datetime-today)))
|
2016-10-04 20:58:33 +00:00
|
|
|
|
2017-09-11 12:04:11 +00:00
|
|
|
(defn timestamp->mini-date [ms]
|
2017-09-05 22:37:43 +00:00
|
|
|
(unparse (formatter "dd MMM") (-> ms
|
|
|
|
from-long
|
|
|
|
(plus time-zone-offset))))
|
|
|
|
|
2017-10-16 14:11:56 +00:00
|
|
|
(defn timestamp->time [ms]
|
|
|
|
(unparse (formatter "HH:mm") (-> ms
|
|
|
|
from-long
|
|
|
|
(plus time-zone-offset))))
|
|
|
|
|
2017-09-11 12:04:11 +00:00
|
|
|
(defn timestamp->date-key [ms]
|
|
|
|
(keyword (unparse (formatter "YYYYMMDD") (-> ms
|
|
|
|
from-long
|
|
|
|
(plus time-zone-offset)))))
|
|
|
|
|
2017-09-12 02:03:59 +00:00
|
|
|
(defn timestamp->long-date [ms]
|
|
|
|
(keyword (unparse (formatter "MMM DD YYYY HH:mm:ss")
|
|
|
|
(-> ms
|
|
|
|
from-long
|
|
|
|
(plus time-zone-offset)))))
|
|
|
|
|
2016-06-30 16:00:44 +00:00
|
|
|
(defn format-time-ago [diff unit]
|
|
|
|
(let [name (label-pluralize diff (:name unit))]
|
2017-03-16 15:11:09 +00:00
|
|
|
(label :t/datetime-ago-format {:ago (label :t/datetime-ago)
|
|
|
|
:number diff
|
|
|
|
:time-intervals name})))
|
2016-06-30 16:00:44 +00:00
|
|
|
|
2016-08-04 15:36:13 +00:00
|
|
|
(defn time-ago [time]
|
|
|
|
(let [diff (t/in-seconds (t/interval time (t/now)))]
|
|
|
|
(if (< diff 60)
|
|
|
|
(label :t/active-online)
|
2016-10-11 14:24:52 +00:00
|
|
|
(let [unit (first (drop-while #(and (>= diff (:limit %))
|
|
|
|
(:limit %))
|
2016-08-04 15:36:13 +00:00
|
|
|
units))]
|
|
|
|
(-> (/ diff (:in-second unit))
|
|
|
|
Math/floor
|
|
|
|
int
|
2016-06-30 16:00:44 +00:00
|
|
|
(format-time-ago unit))))))
|
2016-08-04 15:36:13 +00:00
|
|
|
|
|
|
|
(defn to-date [ms]
|
|
|
|
(from-long ms))
|
|
|
|
|
2018-02-20 14:47:56 +00:00
|
|
|
(defn timestamp []
|
|
|
|
(inst-ms (js/Date.)))
|
2016-10-06 13:15:57 +00:00
|
|
|
|
|
|
|
(defn format-date [format date]
|
|
|
|
(let [local (plus (from-date date) time-zone-offset)]
|
|
|
|
(unparse (formatter format) local)))
|
|
|
|
|
|
|
|
(defn get-ordinal-date [date]
|
|
|
|
(let [local (plus (from-date date) time-zone-offset)
|
|
|
|
day (js/parseInt (unparse (formatter "d") local))
|
|
|
|
s {0 "th"
|
|
|
|
1 "st"
|
|
|
|
2 "nd"
|
|
|
|
3 "rd"}
|
|
|
|
m (mod day 100)]
|
|
|
|
(str day (or (s (mod (- m 20) 10))
|
|
|
|
(s m)
|
|
|
|
(s 0)))))
|