Skip to content

Commit 4a0f279

Browse files
committed
fix: date time from different timezones
1 parent 96c1cc5 commit 4a0f279

File tree

2 files changed

+58
-54
lines changed

2 files changed

+58
-54
lines changed

src/main/frontend/components/property/value.cljs

+51-50
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns frontend.components.property.value
22
(:require [cljs-time.coerce :as tc]
33
[cljs-time.core :as t]
4+
[cljs-time.local :as local]
45
[clojure.set :as set]
56
[clojure.string :as string]
67
[datascript.impl.entity :as de]
@@ -298,6 +299,14 @@
298299
(when done-choice
299300
(db-property/property-value-content done-choice))]])]))
300301

302+
(defn- get-local-journal-date-time
303+
[year month day]
304+
(let [[op h m] (:offset (t/default-time-zone))
305+
f (if (= op :-) t/plus t/minus)]
306+
(-> (t/date-time year month day)
307+
(f (t/hours h))
308+
(f (t/minutes m)))))
309+
301310
(rum/defcs calendar-inner < rum/reactive db-mixins/query
302311
(rum/local (str "calendar-inner-" (js/Date.now)) ::identity)
303312
{:init (fn [state]
@@ -320,7 +329,10 @@
320329
value (get block (:db/ident property))
321330
value (cond
322331
(map? value)
323-
(js/Date. (date/journal-title->long (:block/title value)))
332+
(when-let [day (:block/journal-day value)]
333+
(let [t (tc/to-date-time (date/journal-day->ts day))]
334+
(js/Date.
335+
(get-local-journal-date-time (t/year t) (t/month t) (t/day t)))))
324336

325337
(number? value)
326338
(js/Date. value)
@@ -330,26 +342,25 @@
330342
(.setHours d 0 0 0)
331343
d))
332344
*ident (::identity state)
333-
initial-day (or (some-> value (.getTime) (js/Date.)) (js/Date.))
345+
initial-day value
334346
initial-month (when value
335-
(js/Date. (.getFullYear value) (.getMonth value)))
347+
(let [d (tc/to-date-time value)]
348+
(js/Date. (t/last-day-of-the-month (t/date-time (t/year d) (t/month d))))))
336349
select-handler!
337350
(fn [^js d]
338-
;; force local to UTC
339351
(when d
340-
(let [gd (goog.date.Date. (.getFullYear d) (.getMonth d) (.getDate d))]
341-
(let [journal (date/js-date->journal-title gd)]
342-
(p/do!
343-
(when-not (db/get-page journal)
344-
(page-handler/<create! journal {:redirect? false
345-
:create-first-block? false}))
346-
(when (fn? on-change)
347-
(let [value (if datetime? (tc/to-long d) (db/get-page journal))]
348-
(on-change value)))
349-
(when-not datetime?
350-
(shui/popup-hide! id)
351-
(ui/hide-popups-until-preview-popup!)
352-
(shui/dialog-close!)))))))]
352+
(let [journal (date/js-date->journal-title d)]
353+
(p/do!
354+
(when-not (db/get-page journal)
355+
(page-handler/<create! journal {:redirect? false
356+
:create-first-block? false}))
357+
(when (fn? on-change)
358+
(let [value (if datetime? (tc/to-long d) (db/get-page journal))]
359+
(on-change value)))
360+
(when-not datetime?
361+
(shui/popup-hide! id)
362+
(ui/hide-popups-until-preview-popup!)
363+
(shui/dialog-close!))))))]
353364
[:div.flex.flex-row.gap-2
354365
[:div.flex.flex-col
355366
(ui/nlp-calendar
@@ -380,32 +391,30 @@
380391
:title "Overdue"))
381392
content])))
382393

383-
(defn- human-date-label
384-
[date]
385-
(let [today (t/today)
386-
today-y (t/year today)
387-
today-m (t/month today)
388-
today-d (t/day today)
389-
same-day? (fn [date]
390-
(and (= today-y (t/year date)) (= today-m (t/month date)) (= today-d (t/day date))))]
394+
(defn- human-date-label [date]
395+
(let [given-date (date/start-of-day date)
396+
now (local/local-now)
397+
today (date/start-of-day now)
398+
tomorrow (t/plus today (t/days 1))
399+
yesterday (t/minus today (t/days 1))]
391400
(cond
392-
(same-day? date)
401+
(and (t/before? given-date today) (not (t/before? given-date yesterday)))
402+
"Yesterday"
403+
404+
(and (not (t/before? given-date today)) (t/before? given-date tomorrow))
393405
"Today"
394-
(let [tomorrow (t/minus date (t/days 1))]
395-
(same-day? tomorrow))
406+
407+
(and (not (t/before? given-date tomorrow)) (t/before? given-date (t/plus tomorrow (t/days 1))))
396408
"Tomorrow"
397-
(let [yesterday (t/plus date (t/days 1))]
398-
(same-day? yesterday))
399-
"Yesterday"
400-
:else
401-
nil)))
409+
410+
:else nil)))
402411

403412
(rum/defc datetime-value
404413
[value property-id repeated-task?]
405-
(when-let [date (tc/from-long value)]
414+
(when-let [date (t/to-default-time-zone (tc/from-long value))]
406415
(let [content [:div.ls-datetime.flex.flex-row.gap-1.items-center
407416
(when-let [page-cp (state/get-component :block/page-cp)]
408-
(let [page-title (date/journal-name (date/js-date->goog-date (js/Date. value)))]
417+
(let [page-title (date/journal-name date)]
409418
(rum/with-key
410419
(page-cp {:disable-preview? true
411420
:show-non-exists-page? true
@@ -469,15 +478,15 @@
469478
(ui/icon "repeat" {:size 14 :class "opacity-40"}))
470479
(cond
471480
(map? value)
472-
(let [date (tc/to-date-time (date/journal-title->long (:block/title value)))
481+
(let [date (tc/to-date-time (date/journal-day->ts (:block/journal-day value)))
473482
compare-value (some-> date
474483
(t/plus (t/days 1))
475484
(t/minus (t/seconds 1)))
476485
content (when-let [page-cp (state/get-component :block/page-cp)]
477486
(rum/with-key
478487
(page-cp {:disable-preview? true
479488
:meta-click? other-position?
480-
:label (human-date-label date)} value)
489+
:label (human-date-label (t/to-default-time-zone date))} value)
481490
(:db/id value)))]
482491
(if (or repeated-task? (contains? #{:logseq.task/deadline :logseq.task/scheduled} (:db/id property)))
483492
(overdue compare-value content)
@@ -501,19 +510,11 @@
501510
:datetime? datetime?
502511
:multiple-values? multiple-values?
503512
:on-change (fn [value]
504-
(let [journal (when (number? value)
505-
(date/journal-name (date/js-date->goog-date (js/Date. value))))]
506-
(p/do!
507-
(when-not (db/get-page journal)
508-
(page-handler/<create! journal
509-
{:redirect? false
510-
:create-first-block? false
511-
:tags #{:logseq.class/Journal}}))
512-
(property-handler/set-block-property! repo (:block/uuid block)
513-
(:db/ident property)
514-
(if datetime?
515-
value
516-
(:db/id value))))))
513+
(property-handler/set-block-property! repo (:block/uuid block)
514+
(:db/ident property)
515+
(if datetime?
516+
value
517+
(:db/id value))))
517518
:del-btn? (some? value)
518519
:on-delete (fn []
519520
(property-handler/set-block-property! repo (:block/uuid block)

src/main/frontend/date.cljs

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
[cljs-time.format :as tf]
88
[cljs-time.local :as tl]
99
[frontend.state :as state]
10-
[logseq.common.util.date-time :as date-time-util]
1110
[goog.object :as gobj]
1211
[lambdaisland.glogi :as log]
13-
[logseq.common.date :as common-date]))
12+
[logseq.common.date :as common-date]
13+
[logseq.common.util.date-time :as date-time-util]))
1414

1515
(defn nld-parse
1616
[s]
@@ -73,17 +73,20 @@
7373
:date-str s})
7474
nil)))
7575

76+
(defn start-of-day [date]
77+
(t/date-time (t/year date) (t/month date) (t/day date)))
78+
7679
(defn today
7780
[]
7881
(journal-name))
7982

8083
(defn tomorrow
8184
[]
82-
(journal-name (t/plus (t/today) (t/days 1))))
85+
(journal-name (t/plus (start-of-day (tl/local-now)) (t/days 1))))
8386

8487
(defn yesterday
8588
[]
86-
(journal-name (t/minus (t/today) (t/days 1))))
89+
(journal-name (t/minus (start-of-day (tl/local-now)) (t/days 1))))
8790

8891
(defn get-local-date
8992
[]

0 commit comments

Comments
 (0)