Skip to content

Commit 2d4228f

Browse files
committed
fix(agenda): all day event offsets, locale display
Currently, allDay events are off by one day for negative timezones. Per the CalendarContract: If allDay is set to 1 eventTimezone must be "UTC" and the time must correspond to a midnight boundary. For example, in GMT-2:00, an all day event on December 2nd (beginning at 00:00:00) will be wrongly displayed as starting on December 1st, since the locale will determine that the event's start time is actually 22:00:00 on Dec 1. Source: https://developer.android.com/reference/android/provider/CalendarContract.Events.html This commit: * Corrects the offset back to UTC 00:00:00 for allDay events * Fixes the conditional for single-day all day events in showEvent() * Fixes the display of formatDateShort() for some English locales by also removing any trailing commas or whitespace when the year is removed
1 parent 7238266 commit 2d4228f

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

apps/agenda/agenda.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ var settings = require("Storage").readJSON("agenda.settings.json",true)||{};
3030

3131
CALENDAR=CALENDAR.sort((a,b)=>a.timestamp - b.timestamp);
3232

33-
function getDate(timestamp) {
34-
return new Date(timestamp*1000);
33+
function getDate(timestamp, allDay) {
34+
// All day events are always in UTC and always start at 00:00:00, so we
35+
// need to "undo" the timezone offsetting to make sure that the day is
36+
// correct.
37+
var offset = allDay ? new Date().getTimezoneOffset() * 60 : 0
38+
return new Date((timestamp+offset)*1000);
3539
}
40+
3641
function formatDay(date) {
37-
let formattedDate = Locale.dow(date,1) + " " + Locale.date(date).replace(/\d\d\d\d/,"");
42+
let formattedDate = Locale.dow(date,1) + " " + Locale.date(date).replace(/,*\s*\d\d\d\d/,"");
3843
if (!settings.useToday) {
3944
return formattedDate;
4045
}
@@ -57,8 +62,9 @@ function formatDateLong(date, includeDay, allDay) {
5762
}
5863
return shortTime;
5964
}
65+
6066
function formatDateShort(date, allDay) {
61-
return formatDay(date)+(allDay?"":Locale.time(date,1)+Locale.meridian(date));
67+
return formatDay(date)+(allDay?"":" "+Locale.time(date,1)+Locale.meridian(date));
6268
}
6369

6470
var lines = [];
@@ -69,16 +75,19 @@ function showEvent(ev) {
6975
//var lines = [];
7076
if (ev.title) lines = g.wrapString(ev.title, g.getWidth()-10);
7177
var titleCnt = lines.length;
72-
var start = getDate(ev.timestamp);
73-
var end = getDate((+ev.timestamp) + (+ev.durationInSeconds));
78+
var start = getDate(ev.timestamp, ev.allDay);
79+
// All day events end at the midnight boundary of the following day. Here, we
80+
// subtract one second for all day events so the days display correctly.
81+
const allDayEndCorrection = ev.allDay ? 1 : 0;
82+
var end = getDate((+ev.timestamp) + (+ev.durationInSeconds) - allDayEndCorrection, ev.allDay);
7483
var includeDay = true;
7584
if (titleCnt) lines.push(""); // add blank line after title
7685
if(start.getDay() == end.getDay() && start.getMonth() == end.getMonth())
7786
includeDay = false;
78-
if(includeDay && ev.allDay) {
79-
//single day all day (average to avoid getting previous day)
87+
if(!includeDay && ev.allDay) {
88+
//single day all day
8089
lines = lines.concat(
81-
g.wrapString(formatDateLong(new Date((start+end)/2), includeDay, ev.allDay), g.getWidth()-10));
90+
g.wrapString(formatDateLong(start, includeDay, ev.allDay), g.getWidth()-10));
8291
} else if(includeDay || ev.allDay) {
8392
lines = lines.concat(
8493
/*LANG*/"Start"+":",
@@ -137,7 +146,7 @@ function showList() {
137146
if (!ev) return;
138147
var isPast = false;
139148
var x = r.x+2, title = ev.title;
140-
var body = formatDateShort(getDate(ev.timestamp),ev.allDay)+"\n"+(ev.location?ev.location:/*LANG*/"No location");
149+
var body = formatDateShort(getDate(ev.timestamp, ev.allDay),ev.allDay)+"\n"+(ev.location?ev.location:/*LANG*/"No location");
141150
if(settings.pastEvents) isPast = ev.timestamp + ev.durationInSeconds < (new Date())/1000;
142151
if (title) g.setFontAlign(-1,-1).setFont(fontBig)
143152
.setColor(isPast ? "#888" : g.theme.fg).drawString(title, x+4,r.y+2);

0 commit comments

Comments
 (0)