date.cljs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. [frontend.state :as state]
  9. [frontend.util :as util]
  10. [logseq.graph-parser.util :as gp-util]
  11. [logseq.graph-parser.date-time-util :as date-time-util]
  12. [goog.object :as gobj]
  13. [lambdaisland.glogi :as log]))
  14. (defn nld-parse
  15. [s]
  16. (when (string? s)
  17. ((gobj/get chrono "parseDate") s)))
  18. (def custom-formatter (tf/formatter "yyyy-MM-dd'T'HH:mm:ssZZ"))
  19. (defn journal-title-formatters
  20. []
  21. (->
  22. (cons
  23. (state/get-date-formatter)
  24. (list
  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. (distinct)))
  53. (defn get-date-time-string
  54. ([]
  55. (get-date-time-string (t/now)))
  56. ([date-time]
  57. (tf/unparse custom-formatter date-time)))
  58. (defn get-locale-string
  59. [s]
  60. (try
  61. (->> (tf/parse (tf/formatters :date-time-no-ms) s)
  62. (t/to-default-time-zone)
  63. (tf/unparse (tf/formatter "MMM do, yyyy")))
  64. (catch js/Error _e
  65. nil)))
  66. (def custom-formatter-2 (tf/formatter "yyyy-MM-dd-HH-mm-ss"))
  67. (defn get-date-time-string-2 []
  68. (tf/unparse custom-formatter-2 (tl/local-now)))
  69. (def custom-formatter-3 (tf/formatter "yyyy-MM-dd E HH:mm"))
  70. (defn get-date-time-string-3 []
  71. (tf/unparse custom-formatter-3 (tl/local-now)))
  72. (def custom-formatter-4 (tf/formatter "yyyy-MM-dd E HH:mm:ss"))
  73. (defn get-date-time-string-4 []
  74. (tf/unparse custom-formatter-4 (tl/local-now)))
  75. (defn get-weekday
  76. [date]
  77. (.toLocaleString date "en-us" (clj->js {:weekday "long"})))
  78. (defn get-date
  79. ([]
  80. (get-date (js/Date.)))
  81. ([date]
  82. {:year (.getFullYear date)
  83. :month (inc (.getMonth date))
  84. :day (.getDate date)
  85. :weekday (get-weekday date)}))
  86. (defn year-month-day-padded
  87. ([]
  88. (year-month-day-padded (get-date)))
  89. ([date]
  90. (let [{:keys [year month day]} date]
  91. {:year year
  92. :month (util/zero-pad month)
  93. :day (util/zero-pad day)})))
  94. (defn journal-name
  95. ([]
  96. (journal-name (tl/local-now)))
  97. ([date]
  98. (date-time-util/format date (state/get-date-formatter))))
  99. (defn journal-name-s [s]
  100. (try
  101. (journal-name (tf/parse (tf/formatter "yyyy-MM-dd") s))
  102. (catch js/Error _e
  103. (log/info :parse-journal-date {:message "Unable to parse date to journal name, skipping."
  104. :date-str s})
  105. nil)))
  106. (defn today
  107. []
  108. (journal-name))
  109. (defn tomorrow
  110. []
  111. (journal-name (t/plus (t/today) (t/days 1))))
  112. (defn yesterday
  113. []
  114. (journal-name (t/minus (t/today) (t/days 1))))
  115. (defn ymd
  116. ([]
  117. (ymd (js/Date.)))
  118. ([date]
  119. (ymd date "/"))
  120. ([date sep]
  121. (let [{:keys [year month day]} (year-month-day-padded (get-date date))]
  122. (str year sep month sep day))))
  123. (defn get-local-date
  124. []
  125. (let [date (js/Date.)
  126. year (.getFullYear date)
  127. month (inc (.getMonth date))
  128. day (.getDate date)
  129. hour (.getHours date)
  130. minute (.getMinutes date)]
  131. {:year year
  132. :month month
  133. :day day
  134. :hour hour
  135. :minute minute}))
  136. (defn get-current-time
  137. []
  138. (let [d (js/Date.)]
  139. (.toLocaleTimeString
  140. d
  141. (gobj/get js/window.navigator "language")
  142. (bean/->js {:hour "2-digit"
  143. :minute "2-digit"
  144. :hourCycle "h23"}))))
  145. (defn valid?
  146. [s]
  147. (some
  148. (fn [formatter]
  149. (try
  150. (tf/parse (tf/formatter formatter) s)
  151. (catch js/Error _e
  152. false)))
  153. (journal-title-formatters)))
  154. (defn valid-journal-title?
  155. [title]
  156. (and title
  157. (valid? (gp-util/capitalize-all title))))
  158. (defn journal-title->
  159. ([journal-title then-fn]
  160. (journal-title-> journal-title then-fn (date-time-util/safe-journal-title-formatters (state/get-date-formatter))))
  161. ([journal-title then-fn formatters]
  162. (date-time-util/journal-title-> journal-title then-fn formatters)))
  163. (defn journal-title->int
  164. [journal-title]
  165. (date-time-util/journal-title->int
  166. journal-title
  167. (date-time-util/safe-journal-title-formatters (state/get-date-formatter))))
  168. (defn journal-day->ts
  169. [day]
  170. (when day
  171. (-> (tf/parse (tf/formatter "yyyyMMdd") (str day))
  172. (tc/to-long))))
  173. (defn journal-title->long
  174. [journal-title]
  175. (journal-title-> journal-title #(tc/to-long %)))
  176. (def default-journal-title-formatter (tf/formatter "yyyy_MM_dd"))
  177. (defn journal-title->default
  178. [journal-title]
  179. (let [formatter (if-let [format (state/get-journal-file-name-format)]
  180. (tf/formatter format)
  181. default-journal-title-formatter)]
  182. (journal-title-> journal-title #(tf/unparse formatter %))))
  183. (defn date->file-name
  184. [date]
  185. (let [formatter (if-let [format (state/get-journal-file-name-format)]
  186. (tf/formatter format)
  187. default-journal-title-formatter)]
  188. (tf/unparse formatter date)))
  189. (defn journal-title->custom-format
  190. [journal-title]
  191. (journal-title-> journal-title #(date-time-util/format % (state/get-date-formatter))))
  192. (defn int->local-time-2
  193. [n]
  194. (tf/unparse
  195. (tf/formatter "yyyy-MM-dd HH:mm")
  196. (t/to-default-time-zone (tc/from-long n))))
  197. (comment
  198. (def default-formatter (tf/formatter "MMM do, yyyy"))
  199. (def zh-formatter (tf/formatter "YYYY年MM月dd日"))
  200. (tf/show-formatters)
  201. ;; :date 2020-05-31
  202. ;; :rfc822 Sun, 31 May 2020 03:00:57 Z
  203. )