date.cljs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. (ns frontend.date
  2. "Journal date related utility fns"
  3. (:require ["chrono-node" :as chrono]
  4. [cljs-bean.core :as bean]
  5. [cljs-time.coerce :as tc]
  6. [cljs-time.core :as t]
  7. [cljs-time.format :as tf]
  8. [cljs-time.local :as tl]
  9. [frontend.state :as state]
  10. [logseq.common.util.date-time :as date-time-util]
  11. [goog.object :as gobj]
  12. [lambdaisland.glogi :as log]
  13. [frontend.common.date :as common-date]))
  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. (common-date/journal-title-formatters (state/get-date-formatter)))
  22. (defn get-date-time-string
  23. ([]
  24. (get-date-time-string (t/now)))
  25. ([date-time]
  26. (tf/unparse custom-formatter date-time)))
  27. (defn get-locale-string
  28. "Accepts a :date-time-no-ms string representation, or a cljs-time date object"
  29. [input]
  30. (try
  31. (->> (cond->> input
  32. (string? input) (tf/parse (tf/formatters :date-time-no-ms)))
  33. (t/to-default-time-zone)
  34. (tf/unparse (tf/formatter "MMM do, yyyy")))
  35. (catch :default _e
  36. nil)))
  37. (def custom-formatter-2 (tf/formatter "yyyy-MM-dd-HH-mm-ss"))
  38. (defn get-date-time-string-2 []
  39. (tf/unparse custom-formatter-2 (tl/local-now)))
  40. (def custom-formatter-3 (tf/formatter "yyyy-MM-dd E HH:mm"))
  41. (defn get-date-time-string-3 []
  42. (tf/unparse custom-formatter-3 (tl/local-now)))
  43. (def custom-formatter-4 (tf/formatter "yyyy-MM-dd E HH:mm:ss"))
  44. (defn get-date-time-string-4 []
  45. (tf/unparse custom-formatter-4 (tl/local-now)))
  46. (defn journal-name
  47. ([]
  48. (journal-name (tl/local-now)))
  49. ([date]
  50. (let [formatter (state/get-date-formatter)]
  51. (try
  52. (date-time-util/format date formatter)
  53. (catch :default e
  54. (log/error :parse-journal-date {:message "Failed to parse date to journal name."
  55. :date date
  56. :format formatter})
  57. (throw e))))))
  58. (defn journal-name-s [s]
  59. (try
  60. (journal-name (tf/parse (tf/formatter "yyyy-MM-dd") s))
  61. (catch :default _e
  62. (log/error :parse-journal-date {:message "Unable to parse date to journal name, skipping."
  63. :date-str s})
  64. nil)))
  65. (defn today
  66. []
  67. (journal-name))
  68. (defn tomorrow
  69. []
  70. (journal-name (t/plus (t/today) (t/days 1))))
  71. (defn yesterday
  72. []
  73. (journal-name (t/minus (t/today) (t/days 1))))
  74. (defn get-local-date
  75. []
  76. (let [date (js/Date.)
  77. year (.getFullYear date)
  78. month (inc (.getMonth date))
  79. day (.getDate date)
  80. hour (.getHours date)
  81. minute (.getMinutes date)]
  82. {:year year
  83. :month month
  84. :day day
  85. :hour hour
  86. :minute minute}))
  87. (defn get-current-time
  88. []
  89. (let [d (js/Date.)]
  90. (.toLocaleTimeString
  91. d
  92. (gobj/get js/window.navigator "language")
  93. (bean/->js {:hour "2-digit"
  94. :minute "2-digit"
  95. :hourCycle "h23"}))))
  96. (defn normalize-date
  97. [s]
  98. (common-date/normalize-date s (state/get-date-formatter)))
  99. (defn normalize-journal-title
  100. [title]
  101. (common-date/normalize-journal-title title (state/get-date-formatter)))
  102. (defn valid-journal-title?
  103. [title]
  104. (common-date/valid-journal-title? title (state/get-date-formatter)))
  105. (defn journal-title->
  106. ([journal-title then-fn]
  107. (journal-title-> journal-title then-fn (date-time-util/safe-journal-title-formatters (state/get-date-formatter))))
  108. ([journal-title then-fn formatters]
  109. (date-time-util/journal-title-> journal-title then-fn formatters)))
  110. (defn journal-title->int
  111. [journal-title]
  112. (date-time-util/journal-title->int
  113. journal-title
  114. (date-time-util/safe-journal-title-formatters (state/get-date-formatter))))
  115. (defn journal-day->ts
  116. "journal-day format yyyyMMdd"
  117. [day]
  118. (when day
  119. (-> (tf/parse (tf/formatter "yyyyMMdd") (str day))
  120. (tc/to-long))))
  121. (defn journal-title->long
  122. [journal-title]
  123. (journal-title-> journal-title #(tc/to-long %)))
  124. (def default-journal-filename-formatter common-date/default-journal-filename-formatter)
  125. (defn journal-title->default
  126. "Journal title to filename format"
  127. [journal-title]
  128. (let [formatter (if-let [format (state/get-journal-file-name-format)]
  129. (tf/formatter format)
  130. (tf/formatter default-journal-filename-formatter))]
  131. (journal-title-> journal-title #(tf/unparse formatter %))))
  132. (defn journal-title->custom-format
  133. [journal-title]
  134. (journal-title-> journal-title #(date-time-util/format % (state/get-date-formatter))))
  135. (defn int->local-time-2
  136. [n]
  137. (tf/unparse
  138. (tf/formatter "yyyy-MM-dd HH:mm")
  139. (t/to-default-time-zone (tc/from-long n))))
  140. (def iso-parser (tf/formatter "yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'"))
  141. (defn parse-iso [string]
  142. (tf/parse iso-parser string))
  143. (defn js-date->journal-title
  144. [date]
  145. (journal-name (tc/to-local-date date)))
  146. (defn js-date->goog-date
  147. [d]
  148. (cond
  149. (some->> d (instance? js/Date))
  150. (goog.date.Date. (.getFullYear d) (.getMonth d) (.getDate d))
  151. :else d))
  152. (comment
  153. (def default-formatter (tf/formatter "MMM do, yyyy"))
  154. (def zh-formatter (tf/formatter "YYYY年MM月dd日"))
  155. (tf/show-formatters)
  156. ;; :date 2020-05-31
  157. ;; :rfc822 Sun, 31 May 2020 03:00:57 Z
  158. (let [info {:ExpireTime 1680781356,
  159. :UserGroups [],
  160. :LemonRenewsAt "2024-04-11T07:28:00.000000Z",
  161. :LemonEndsAt nil,
  162. :LemonStatus "active"}]
  163. (->> info :LemonRenewsAt (tf/parse iso-parser) (< (js/Date.)))))