Skip to content

Commit

Permalink
fix: date time from different timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Jan 4, 2025
1 parent 96c1cc5 commit 4a0f279
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 54 deletions.
101 changes: 51 additions & 50 deletions src/main/frontend/components/property/value.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns frontend.components.property.value
(:require [cljs-time.coerce :as tc]
[cljs-time.core :as t]
[cljs-time.local :as local]
[clojure.set :as set]
[clojure.string :as string]
[datascript.impl.entity :as de]
Expand Down Expand Up @@ -298,6 +299,14 @@
(when done-choice
(db-property/property-value-content done-choice))]])]))

(defn- get-local-journal-date-time
[year month day]
(let [[op h m] (:offset (t/default-time-zone))
f (if (= op :-) t/plus t/minus)]
(-> (t/date-time year month day)
(f (t/hours h))
(f (t/minutes m)))))

(rum/defcs calendar-inner < rum/reactive db-mixins/query
(rum/local (str "calendar-inner-" (js/Date.now)) ::identity)
{:init (fn [state]
Expand All @@ -320,7 +329,10 @@
value (get block (:db/ident property))
value (cond
(map? value)
(js/Date. (date/journal-title->long (:block/title value)))
(when-let [day (:block/journal-day value)]
(let [t (tc/to-date-time (date/journal-day->ts day))]
(js/Date.
(get-local-journal-date-time (t/year t) (t/month t) (t/day t)))))

(number? value)
(js/Date. value)
Expand All @@ -330,26 +342,25 @@
(.setHours d 0 0 0)
d))
*ident (::identity state)
initial-day (or (some-> value (.getTime) (js/Date.)) (js/Date.))
initial-day value
initial-month (when value
(js/Date. (.getFullYear value) (.getMonth value)))
(let [d (tc/to-date-time value)]
(js/Date. (t/last-day-of-the-month (t/date-time (t/year d) (t/month d))))))
select-handler!
(fn [^js d]
;; force local to UTC
(when d
(let [gd (goog.date.Date. (.getFullYear d) (.getMonth d) (.getDate d))]
(let [journal (date/js-date->journal-title gd)]
(p/do!
(when-not (db/get-page journal)
(page-handler/<create! journal {:redirect? false
:create-first-block? false}))
(when (fn? on-change)
(let [value (if datetime? (tc/to-long d) (db/get-page journal))]
(on-change value)))
(when-not datetime?
(shui/popup-hide! id)
(ui/hide-popups-until-preview-popup!)
(shui/dialog-close!)))))))]
(let [journal (date/js-date->journal-title d)]
(p/do!
(when-not (db/get-page journal)
(page-handler/<create! journal {:redirect? false
:create-first-block? false}))
(when (fn? on-change)
(let [value (if datetime? (tc/to-long d) (db/get-page journal))]
(on-change value)))
(when-not datetime?
(shui/popup-hide! id)
(ui/hide-popups-until-preview-popup!)
(shui/dialog-close!))))))]
[:div.flex.flex-row.gap-2
[:div.flex.flex-col
(ui/nlp-calendar
Expand Down Expand Up @@ -380,32 +391,30 @@
:title "Overdue"))
content])))

(defn- human-date-label
[date]
(let [today (t/today)
today-y (t/year today)
today-m (t/month today)
today-d (t/day today)
same-day? (fn [date]
(and (= today-y (t/year date)) (= today-m (t/month date)) (= today-d (t/day date))))]
(defn- human-date-label [date]
(let [given-date (date/start-of-day date)
now (local/local-now)
today (date/start-of-day now)
tomorrow (t/plus today (t/days 1))
yesterday (t/minus today (t/days 1))]
(cond
(same-day? date)
(and (t/before? given-date today) (not (t/before? given-date yesterday)))
"Yesterday"

(and (not (t/before? given-date today)) (t/before? given-date tomorrow))
"Today"
(let [tomorrow (t/minus date (t/days 1))]
(same-day? tomorrow))

(and (not (t/before? given-date tomorrow)) (t/before? given-date (t/plus tomorrow (t/days 1))))
"Tomorrow"
(let [yesterday (t/plus date (t/days 1))]
(same-day? yesterday))
"Yesterday"
:else
nil)))

:else nil)))

(rum/defc datetime-value
[value property-id repeated-task?]
(when-let [date (tc/from-long value)]
(when-let [date (t/to-default-time-zone (tc/from-long value))]
(let [content [:div.ls-datetime.flex.flex-row.gap-1.items-center
(when-let [page-cp (state/get-component :block/page-cp)]
(let [page-title (date/journal-name (date/js-date->goog-date (js/Date. value)))]
(let [page-title (date/journal-name date)]
(rum/with-key
(page-cp {:disable-preview? true
:show-non-exists-page? true
Expand Down Expand Up @@ -469,15 +478,15 @@
(ui/icon "repeat" {:size 14 :class "opacity-40"}))
(cond
(map? value)
(let [date (tc/to-date-time (date/journal-title->long (:block/title value)))
(let [date (tc/to-date-time (date/journal-day->ts (:block/journal-day value)))
compare-value (some-> date
(t/plus (t/days 1))
(t/minus (t/seconds 1)))
content (when-let [page-cp (state/get-component :block/page-cp)]
(rum/with-key
(page-cp {:disable-preview? true
:meta-click? other-position?
:label (human-date-label date)} value)
:label (human-date-label (t/to-default-time-zone date))} value)
(:db/id value)))]
(if (or repeated-task? (contains? #{:logseq.task/deadline :logseq.task/scheduled} (:db/id property)))
(overdue compare-value content)
Expand All @@ -501,19 +510,11 @@
:datetime? datetime?
:multiple-values? multiple-values?
:on-change (fn [value]
(let [journal (when (number? value)
(date/journal-name (date/js-date->goog-date (js/Date. value))))]
(p/do!
(when-not (db/get-page journal)
(page-handler/<create! journal
{:redirect? false
:create-first-block? false
:tags #{:logseq.class/Journal}}))
(property-handler/set-block-property! repo (:block/uuid block)
(:db/ident property)
(if datetime?
value
(:db/id value))))))
(property-handler/set-block-property! repo (:block/uuid block)
(:db/ident property)
(if datetime?
value
(:db/id value))))
:del-btn? (some? value)
:on-delete (fn []
(property-handler/set-block-property! repo (:block/uuid block)
Expand Down
11 changes: 7 additions & 4 deletions src/main/frontend/date.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
[cljs-time.format :as tf]
[cljs-time.local :as tl]
[frontend.state :as state]
[logseq.common.util.date-time :as date-time-util]
[goog.object :as gobj]
[lambdaisland.glogi :as log]
[logseq.common.date :as common-date]))
[logseq.common.date :as common-date]
[logseq.common.util.date-time :as date-time-util]))

(defn nld-parse
[s]
Expand Down Expand Up @@ -73,17 +73,20 @@
:date-str s})
nil)))

(defn start-of-day [date]
(t/date-time (t/year date) (t/month date) (t/day date)))

(defn today
[]
(journal-name))

(defn tomorrow
[]
(journal-name (t/plus (t/today) (t/days 1))))
(journal-name (t/plus (start-of-day (tl/local-now)) (t/days 1))))

(defn yesterday
[]
(journal-name (t/minus (t/today) (t/days 1))))
(journal-name (t/minus (start-of-day (tl/local-now)) (t/days 1))))

(defn get-local-date
[]
Expand Down

0 comments on commit 4a0f279

Please sign in to comment.