Improved mechanism for allDay calendar events

Using newer java apis instead of GregorianCalendar and fixed wrong date
on negative time zones
This commit is contained in:
Gabriele Monaco 2022-08-23 17:03:58 +02:00 committed by Gitea
parent 29e0879381
commit e6411d8f68

View File

@ -28,9 +28,11 @@ import org.slf4j.LoggerFactory;
import java.util.Calendar; import java.util.Calendar;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import de.greenrobot.dao.query.QueryBuilder; import de.greenrobot.dao.query.QueryBuilder;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
@ -190,10 +192,12 @@ public class CalendarReceiver extends BroadcastReceiver {
calendarEventSpec.durationInSeconds = calendarEvent.getDurationSeconds(); //FIXME: leads to problems right now calendarEventSpec.durationInSeconds = calendarEvent.getDurationSeconds(); //FIXME: leads to problems right now
if (calendarEvent.isAllDay()) { if (calendarEvent.isAllDay()) {
//force the all day events to begin at midnight and last a whole day //force the all day events to begin at midnight and last a whole day
Calendar c = GregorianCalendar.getInstance(); OffsetDateTime o = OffsetDateTime.now();
c.setTimeInMillis(calendarEvent.getBegin()); LocalDateTime d = LocalDateTime.ofEpochSecond(calendarEvent.getBegin()/1000, 0, o.getOffset());
c.set(Calendar.HOUR, 0); o = OffsetDateTime.of(d, o.getOffset()).withHour(0);
calendarEventSpec.timestamp = (int) (c.getTimeInMillis() / 1000); //workaround for negative timezones
if(o.getOffset().compareTo(ZoneOffset.UTC)>0) o = o.plusDays(1);
calendarEventSpec.timestamp = (int)o.toEpochSecond();
calendarEventSpec.durationInSeconds = 24 * 60 * 60; calendarEventSpec.durationInSeconds = 24 * 60 * 60;
} }
calendarEventSpec.description = calendarEvent.getDescription(); calendarEventSpec.description = calendarEvent.getDescription();