date.cljs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. (ns frontend.worker.date
  2. "Date related fns that used by worker"
  3. (:require [cljs-time.format :as tf]
  4. [logseq.common.util :as common-util]))
  5. (def default-journal-filename-formatter "yyyy_MM_dd")
  6. (defn journal-title-formatters
  7. [date-formatter]
  8. (->
  9. (cons
  10. date-formatter
  11. (list
  12. "do MMM yyyy"
  13. "do MMMM yyyy"
  14. "MMM do, yyyy"
  15. "MMMM do, yyyy"
  16. "E, dd-MM-yyyy"
  17. "E, dd.MM.yyyy"
  18. "E, MM/dd/yyyy"
  19. "E, yyyy/MM/dd"
  20. "EEE, dd-MM-yyyy"
  21. "EEE, dd.MM.yyyy"
  22. "EEE, MM/dd/yyyy"
  23. "EEE, yyyy/MM/dd"
  24. "EEEE, dd-MM-yyyy"
  25. "EEEE, dd.MM.yyyy"
  26. "EEEE, MM/dd/yyyy"
  27. "EEEE, yyyy/MM/dd"
  28. "dd-MM-yyyy"
  29. ;; This tyle will mess up other date formats like "2022-08" "2022Q4" "2022/10"
  30. ;; "dd.MM.yyyy"
  31. "MM/dd/yyyy"
  32. "MM-dd-yyyy"
  33. "MM_dd_yyyy"
  34. "yyyy/MM/dd"
  35. "yyyy-MM-dd"
  36. "yyyy-MM-dd EEEE"
  37. "yyyy_MM_dd"
  38. "yyyyMMdd"
  39. "yyyy年MM月dd日"))
  40. (distinct)))
  41. (defn normalize-date
  42. "Given raw date string, return a normalized date string at best effort.
  43. Warning: this is a function with heavy cost (likely 50ms). Use with caution"
  44. [s date-formatter]
  45. (some
  46. (fn [formatter]
  47. (try
  48. (tf/parse (tf/formatter formatter) s)
  49. (catch :default _e
  50. false)))
  51. (journal-title-formatters date-formatter)))
  52. (defn normalize-journal-title
  53. "Normalize journal title at best effort. Return nil if title is not a valid date.
  54. Return goog.date.Date.
  55. Return format: 20220812T000000"
  56. [title date-formatter]
  57. (and title
  58. (normalize-date (common-util/capitalize-all title) date-formatter)))
  59. (defn valid-journal-title?
  60. "This is a loose rule, requires double check by journal-title->custom-format.
  61. BUG: This also accepts strings like 3/4/5 as journal titles"
  62. [title date-formatter]
  63. (boolean (normalize-journal-title title date-formatter)))
  64. (defn date->file-name
  65. "Date object to filename format"
  66. [date journal-filename-formatter]
  67. (let [formatter (if journal-filename-formatter
  68. (tf/formatter journal-filename-formatter)
  69. (tf/formatter default-journal-filename-formatter))]
  70. (tf/unparse formatter date)))