date.cljs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. (ns frontend.date
  2. (:require ["chrono-node" :as chrono]
  3. [cljs-bean.core :as bean]
  4. [cljs-time.coerce :as tc]
  5. [cljs-time.core :as t]
  6. [cljs-time.format :as tf]
  7. [cljs-time.local :as tl]
  8. [clojure.string :as string]
  9. [frontend.state :as state]
  10. [frontend.util :as util]
  11. [goog.object :as gobj]
  12. [lambdaisland.glogi :as log]))
  13. (defn nld-parse
  14. [s]
  15. (when (string? s)
  16. ((gobj/get chrono "parseDate") s)))
  17. (defn format
  18. [date]
  19. (when-let [formatter-string (state/get-date-formatter)]
  20. (tf/unparse (tf/formatter formatter-string) date)))
  21. (def custom-formatter (tf/formatter "yyyy-MM-dd'T'HH:mm:ssZZ"))
  22. (defn journal-title-formatters
  23. []
  24. (conj
  25. #{"do MMM yyyy"
  26. "do MMMM yyyy"
  27. "MMM do, yyyy"
  28. "MMMM do, yyyy"
  29. "E, dd-MM-yyyy"
  30. "E, dd.MM.yyyy"
  31. "E, MM/dd/yyyy"
  32. "E, yyyy/MM/dd"
  33. "EEE, dd-MM-yyyy"
  34. "EEE, dd.MM.yyyy"
  35. "EEE, MM/dd/yyyy"
  36. "EEE, yyyy/MM/dd"
  37. "EEEE, dd-MM-yyyy"
  38. "EEEE, dd.MM.yyyy"
  39. "EEEE, MM/dd/yyyy"
  40. "EEEE, yyyy/MM/dd"
  41. "dd-MM-yyyy"
  42. "dd.MM.yyyy"
  43. "MM/dd/yyyy"
  44. "MM-dd-yyyy"
  45. "MM_dd_yyyy"
  46. "yyyy/MM/dd"
  47. "yyyy-MM-dd"
  48. "yyyy-MM-dd EEEE"
  49. "yyyy_MM_dd"
  50. "yyyyMMdd"
  51. "yyyy年MM月dd日"}
  52. (state/get-date-formatter)))
  53. ;; (tf/parse (tf/formatter "dd.MM.yyyy") "2021Q4") => 20040120T000000
  54. (defn safe-journal-title-formatters
  55. []
  56. (->> [(state/get-date-formatter) "yyyy-MM-dd" "yyyy_MM_dd"]
  57. (remove string/blank?)
  58. distinct))
  59. (defn get-date-time-string
  60. ([]
  61. (get-date-time-string (t/now)))
  62. ([date-time]
  63. (tf/unparse custom-formatter date-time)))
  64. (defn get-locale-string
  65. [s]
  66. (try
  67. (->> (tf/parse (tf/formatters :date-time-no-ms) s)
  68. (t/to-default-time-zone)
  69. (tf/unparse (tf/formatter "MMM do, yyyy")))
  70. (catch js/Error e
  71. nil)))
  72. (defn ISO-string
  73. []
  74. (.toISOString (js/Date.)))
  75. (defn get-local-date-time-string
  76. []
  77. (get-date-time-string (tl/local-now)))
  78. (def custom-formatter-2 (tf/formatter "yyyy-MM-dd-HH-mm-ss"))
  79. (defn get-date-time-string-2 []
  80. (tf/unparse custom-formatter-2 (tl/local-now)))
  81. (defn get-weekday
  82. [date]
  83. (.toLocaleString date "en-us" (clj->js {:weekday "long"})))
  84. (defn get-date
  85. ([]
  86. (get-date (js/Date.)))
  87. ([date]
  88. {:year (.getFullYear date)
  89. :month (inc (.getMonth date))
  90. :day (.getDate date)
  91. :weekday (get-weekday date)}))
  92. (defn year-month-day-padded
  93. ([]
  94. (year-month-day-padded (get-date)))
  95. ([date]
  96. (let [{:keys [year month day]} date]
  97. {:year year
  98. :month (util/zero-pad month)
  99. :day (util/zero-pad day)})))
  100. (defn journal-name
  101. ([]
  102. (journal-name (tl/local-now)))
  103. ([date]
  104. (format date)))
  105. (defn journal-name-s [s]
  106. (try
  107. (journal-name (tf/parse (tf/formatter "yyyy-MM-dd") s))
  108. (catch js/Error e
  109. (log/info :parse-journal-date {:message "Unable to parse date to journal name, skipping."
  110. :date-str s})
  111. nil)))
  112. (defn today
  113. []
  114. (journal-name))
  115. (defn tomorrow
  116. []
  117. (journal-name (t/plus (t/today) (t/days 1))))
  118. (defn yesterday
  119. []
  120. (journal-name (t/minus (t/today) (t/days 1))))
  121. (defn get-month-last-day
  122. []
  123. (let [today (js/Date.)
  124. date (js/Date. (.getFullYear today) (inc (.getMonth today)) 0)]
  125. (.getDate date)))
  126. (defn ymd
  127. ([]
  128. (ymd (js/Date.)))
  129. ([date]
  130. (ymd date "/"))
  131. ([date sep]
  132. (let [{:keys [year month day]} (year-month-day-padded (get-date date))]
  133. (str year sep month sep day))))
  134. (defn get-local-date
  135. []
  136. (let [date (js/Date.)
  137. year (.getFullYear date)
  138. month (inc (.getMonth date))
  139. day (.getDate date)
  140. hour (.getHours date)
  141. minute (.getMinutes date)]
  142. {:year year
  143. :month month
  144. :day day
  145. :hour hour
  146. :minute minute}))
  147. (defn get-current-time
  148. []
  149. (let [d (js/Date.)]
  150. (.toLocaleTimeString
  151. d
  152. (gobj/get js/window.navigator "language")
  153. (bean/->js {:hour "2-digit"
  154. :minute "2-digit"
  155. :hourCycle "h23"}))))
  156. (defn valid?
  157. [s]
  158. (some
  159. (fn [formatter]
  160. (try
  161. (tf/parse (tf/formatter formatter) s)
  162. (catch js/Error _e
  163. false)))
  164. (journal-title-formatters)))
  165. (defn valid-journal-title?
  166. [title]
  167. (and title
  168. (valid? (util/capitalize-all title))))
  169. (defn journal-title->
  170. ([journal-title then-fn]
  171. (journal-title-> journal-title then-fn (safe-journal-title-formatters)))
  172. ([journal-title then-fn formatters]
  173. (when-not (string/blank? journal-title)
  174. (when-let [time (->> (map
  175. (fn [formatter]
  176. (try
  177. (tf/parse (tf/formatter formatter) (util/capitalize-all journal-title))
  178. (catch js/Error _e
  179. nil)))
  180. formatters)
  181. (filter some?)
  182. first)]
  183. (then-fn time)))))
  184. (defn journal-title->int
  185. [journal-title]
  186. (when journal-title
  187. (let [journal-title (util/capitalize-all journal-title)]
  188. (journal-title-> journal-title #(util/parse-int (tf/unparse (tf/formatter "yyyyMMdd") %))))))
  189. (defn int->journal-title
  190. [day]
  191. (when day
  192. (format (tf/parse (tf/formatter "yyyyMMdd") (str day)))))
  193. (defn journal-day->ts
  194. [day]
  195. (when day
  196. (-> (tf/parse (tf/formatter "yyyyMMdd") (str day))
  197. (tc/to-long))))
  198. (defn journal-title->long
  199. [journal-title]
  200. (journal-title-> journal-title #(tc/to-long %)))
  201. (def default-journal-title-formatter (tf/formatter "yyyy_MM_dd"))
  202. (defn journal-title->default
  203. [journal-title]
  204. (let [formatter (if-let [format (state/get-journal-file-name-format)]
  205. (tf/formatter format)
  206. default-journal-title-formatter)]
  207. (journal-title-> journal-title #(tf/unparse formatter %))))
  208. (defn journal-title->custom-format
  209. [journal-title]
  210. (journal-title-> journal-title format))
  211. (defn int->local-time
  212. [n]
  213. (get-date-time-string (t/to-default-time-zone (tc/from-long n))))
  214. (defn int->local-time-2
  215. [n]
  216. (tf/unparse
  217. (tf/formatter "yyyy-MM-dd HH:mm")
  218. (t/to-default-time-zone (tc/from-long n))))
  219. (comment
  220. (def default-formatter (tf/formatter "MMM do, yyyy"))
  221. (def zh-formatter (tf/formatter "YYYY年MM月dd日"))
  222. (tf/show-formatters)
  223. ;; :date 2020-05-31
  224. ;; :rfc822 Sun, 31 May 2020 03:00:57 Z
  225. )