Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
tbenr 2018-07-30 12:16:12 +02:00 committed by Julien Eluard
parent bb729c6c81
commit 0f2af863e7
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
2 changed files with 82 additions and 12 deletions

View File

@ -32,19 +32,53 @@
loc (get goog.i18n (str "DateTimeSymbols_" name-first))]
(or loc goog.i18n.DateTimeSymbols_en))))
(def medium-date-time-format (.-MEDIUM_DATETIME goog.i18n.DateTimeFormat.Format))
(def medium-date-format (.-MEDIUM_DATE goog.i18n.DateTimeFormat.Format))
(def short-time-format (.-SHORT_TIME goog.i18n.DateTimeFormat.Format))
;; detects if given locale symbols timeformat generates AM/PM ("a")
(defn- is24Hour-locsym [locsym]
(not (s/includes?
(nth (get locsym 'TIMEFORMATS) 2)
"a")))
(defn mk-fmt [locale format]
(goog.i18n.DateTimeFormat. format (locale-symbols locale)))
;; returns is24Hour from device or from given locale symbols
;; when device-info module is not available (ie. desktop) returns from the given locale
;
; TODO integrate with native module. example:
; (defn- is24Hour [locsym]
; (if rn/device-info
; (.is24Hour rn/device-info)
; (is24Hour-locsym locsym)))
(defn- is24Hour [locsym]
(is24Hour-locsym locsym))
;; time formats
(defn- short-time-format [locsym] (if (is24Hour locsym) "HH:mm" "h:mm a"))
(defn- time-format [locsym] (if (is24Hour locsym) "HH:mm:ss" "h:mm:ss a"))
;; date formats
(defn- short-date-format [locsym] "dd MMM")
(defn- medium-date-format [locsym] (nth (get locsym 'DATEFORMATS) 2)) ; get medium format from current locale symbols
;; date-time formats
(defn- medium-date-time-format [locsym] (str (medium-date-format locsym) ", " (time-format locsym)))
;; get formatter for current locale symbols and format function
(defn- mk-fmt [locale format-fn]
(let [locsym (locale-symbols locale)]
(goog.i18n.DateTimeFormat. (format-fn locsym) locsym)))
;; generate formatters for different formats
(def date-time-fmt
(mk-fmt status-im.i18n/locale medium-date-time-format))
(def date-fmt
(mk-fmt status-im.i18n/locale medium-date-format))
(def time-fmt
(mk-fmt status-im.i18n/locale short-time-format))
(def short-date-fmt
(mk-fmt status-im.i18n/locale short-date-format))
;;
;; functions which apply formats for the given timestamp
;;
(defn- to-str [ms old-fmt-fn yesterday-fmt-fn today-fmt-fn]
(let [date (from-long ms)
@ -69,7 +103,7 @@
#(label :t/datetime-today)))
(defn timestamp->mini-date [ms]
(unparse (formatter "dd MMM") (-> ms
(.format short-date-fmt (-> ms
from-long
(plus time-zone-offset))))
@ -84,10 +118,9 @@
(plus time-zone-offset)))))
(defn timestamp->long-date [ms]
(keyword (unparse (formatter "MMM DD YYYY HH:mm:ss")
(-> ms
(.format date-time-fmt (-> ms
from-long
(plus time-zone-offset)))))
(plus time-zone-offset))))
(defn format-time-ago [diff unit]
(let [name (label-pluralize diff (:name unit))]

View File

@ -21,12 +21,35 @@
;; 1970-01-03 00:00:00 UTC
(def epoch-plus-3d 172800000)
(deftest is24Hour-locale-en-test
(is (= (d/is24Hour-locsym (d/locale-symbols "en")) false)))
(deftest is24Hour-locale-it-test
(is (= (d/is24Hour-locsym (d/locale-symbols "it")) true)))
(deftest is24Hour-locale-nb-test
(is (= (d/is24Hour-locsym (d/locale-symbols "nb-NO")) true)))
(deftest to-short-str-today-test
(with-redefs [t/*ms-fn* (constantly epoch-plus-3d)
d/time-fmt (d/mk-fmt "us" d/short-time-format)
d/time-zone-offset (t/period :hours 0)]
(is (= (d/to-short-str epoch-plus-3d) "12:00 AM"))))
(deftest to-short-str-today-force-24H-test
(with-redefs [t/*ms-fn* (constantly epoch-plus-3d)
d/is24Hour (constantly true)
d/time-fmt (d/mk-fmt "us" d/short-time-format)
d/time-zone-offset (t/period :hours 0)]
(is (= (d/to-short-str epoch-plus-3d) "00:00"))))
(deftest to-short-str-today-force-AMPM-test
(with-redefs [t/*ms-fn* (constantly epoch-plus-3d)
d/is24Hour (constantly false)
d/time-fmt (d/mk-fmt "it" d/short-time-format)
d/time-zone-offset (t/period :hours 0)]
(is (= (d/to-short-str epoch-plus-3d) "12:00 AM"))))
(deftest to-short-str-before-yesterday-us-test
(with-redefs [t/*ms-fn* (constantly epoch-plus-3d)
d/time-zone-offset (t/period :hours 0)
@ -50,3 +73,17 @@
d/time-zone-offset (t/period :hours 0)
d/date-fmt (d/mk-fmt "nb-NO" d/medium-date-time-format)]
(is (= (d/day-relative epoch) "1. jan. 1970, 00:00:00"))))
(deftest day-relative-before-yesterday-force-24H-test
(with-redefs [t/*ms-fn* (constantly epoch-plus-3d)
d/is24Hour (constantly true)
d/time-zone-offset (t/period :hours 0)
d/date-fmt (d/mk-fmt "us" d/medium-date-time-format)]
(is (= (d/day-relative epoch) "Jan 1, 1970, 00:00:00"))))
(deftest day-relative-before-yesterday-force-AMPM-test
(with-redefs [t/*ms-fn* (constantly epoch-plus-3d)
d/is24Hour (constantly false)
d/time-zone-offset (t/period :hours 0)
d/date-fmt (d/mk-fmt "it" d/medium-date-time-format)]
(is (= (d/day-relative epoch) "01 gen 1970, 12:00:00 AM"))))