DateTimeFormatter::formatDateSpan() ignores the user's time zone: Dashboard "Upcoming events" shows "in 2 days" for tomorrow-evening events
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
Hướng nghiên cứu
Đọc lib/private/DateTimeFormatter.php, tập trung vào formatDateSpan() và cách hàm này lấy múi giờ từ bộ định dạng. Issue có một cách tái hiện cụ thể và một thay đổi được đề xuất; kiểm tra các bài kiểm thử DateTimeFormatter hiện có về phạm vi ngày và thêm một trường hợp sử dụng múi giờ khác UTC. Công việc hoàn tất khi các ngày gần nửa đêm theo giờ địa phương của người dùng tạo ra nhãn mong đợi, bao gồm “ngày mai” cho các sự kiện vào tối mai.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug description
OC\DateTimeFormatter::formatDateSpan() (and so formatTimeSpan() for spans of a day or more) works out the calendar day of both dates without applying the formatter's time zone ($this->defaultTimeZone, which the server sets from the user's core/timezone preference). It truncates both dates to midnight in whatever time zone they arrive in: usually UTC, PHP's default under Nextcloud, and the time zone of expanded recurring events from the CalDAV backend.
So for users west of UTC, anything after their local UTC midnight is counted as the next day. In America/Los_Angeles (UTC−8), an event tomorrow at 18:00 is shown as "in 2 days", the same as events the day after tomorrow.
The visible case is the Calendar app's Dashboard widget (OCA\Calendar\Dashboard\CalendarWidget::getItems()), which builds each item's subtitle with IDateTimeFormatter::formatTimeSpan(). Every caller of formatDateSpan() / formatTimeSpan() is affected.
Steps to reproduce
- Set your user's time zone to
America/Los_Angeles(Personal settings, orcore/timezone). - Create events for tomorrow at 09:15, tomorrow at 18:00 and the day after tomorrow at 10:00.
- On a weekday afternoon, open the Dashboard and look at the Upcoming events widget.
Expected behaviour
| Event (local time) | Expected |
|---|---|
| Tomorrow 09:15 | in 19 hours / tomorrow |
| Tomorrow 18:00 | tomorrow |
| Day after tomorrow 10:00 | in 2 days |
Actual behaviour
Tested on Thursday at ~14:00 PST:
Morning event | in 19 hours | Fri 09:15
Lesson A | in 2 days | Fri 18:00 <- should be "tomorrow"
Lesson B | in 2 days | Fri 19:15 <- should be "tomorrow"
Lesson C | in 2 days | Fri 20:30 <- should be "tomorrow"
Lesson D | in 2 days | Sat 10:00
Fri 18:00 PST is Sat 02:00 UTC, so after truncation to midnight UTC it's compared as Saturday vs. Thursday.
Cause
lib/private/DateTimeFormatter.php, formatDateSpan():
$timestamp = $this->getDateTime($timestamp); // no time zone applied
$timestamp->setTime(0, 0, 0);
...
$baseTimestamp = $this->getDateTime($baseTimestamp); // no time zone applied
$baseTimestamp->setTime(0, 0, 0);
getDateTime() accepts a time zone, and format() passes $this->defaultTimeZone, but formatDateSpan() doesn't, so the day boundary is UTC rather than the user's local midnight.
Suggested fix
--- a/lib/private/DateTimeFormatter.php
+++ b/lib/private/DateTimeFormatter.php
@@ public function formatDateSpan($timestamp, $baseTimestamp = null, ?IL10N $l = null) {
$l = $this->getLocale($l);
- $timestamp = $this->getDateTime($timestamp);
+ $timestamp = $this->getDateTime($timestamp, $this->defaultTimeZone);
$timestamp->setTime(0, 0, 0);
if ($baseTimestamp === null) {
$baseTimestamp = time();
}
- $baseTimestamp = $this->getDateTime($baseTimestamp);
+ $baseTimestamp = $this->getDateTime($baseTimestamp, $this->defaultTimeZone);
$baseTimestamp->setTime(0, 0, 0);
With this applied locally, the widget shows Fri 18:00/19:15/20:30 as "tomorrow" and Sat 10:00 as "in 2 days". Note that getDateTime() calls setTimezone() on a passed-in DateTime, so the caller's object is changed. The method already does that with setTime(0, 0, 0), so this adds no new side effect, but cloning first would be cleaner.
Nextcloud Server version
35.0.3.2 (Calendar app bundled with it). The same code is still on master as of 2026-12-03.
Operating system / PHP / web server
Debian 13 (aarch64), PHP 8.4 (mod_php), Apache 2.4. User time zone America/Los_Angeles; no default_timezone set in config.php.
- Ngôn ngữ chính
- PHP
- Star
- 37.7k
- Fork
- 5.4k
- Merge trung bình
- 1 ngày 23 giờ
- Pull request đã merge (30 ngày)
- 585
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của nextcloud/server
-
1. to develop bug feature: tags good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
1. to develop bug feature: federation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
1. to develop bug feature: files feature: previews and thumbnails
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
1. to develop bug feature: dashboard
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
1. to develop bug feature: sharing
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của nextcloud/server
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 91/100
nhrrob/nhrrob-secure#35 ·
-
area/tests type/bug when/soon
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày