Skip to content

Commit

Permalink
Use start of day to peform time-based calculcations (e.g. interval) o…
Browse files Browse the repository at this point in the history
…n date-based temporal values
  • Loading branch information
benfortuna committed Jul 9, 2020
1 parent 769f301 commit 109457a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/main/java/net/fortuna/ical4j/model/Period.java
Expand Up @@ -474,7 +474,9 @@ public Interval toInterval() {

public Interval toInterval(ZoneId zoneId) {
if (start instanceof LocalDate) {
throw new UnsupportedOperationException("Unable to create Interval from date-only temporal.");
return Interval.of(((LocalDate) start).atStartOfDay(zoneId).toInstant(),
((LocalDate) end).atStartOfDay(zoneId).toInstant());
// throw new UnsupportedOperationException("Unable to create Interval from date-only temporal.");
} else if (start instanceof Instant) {
return Interval.of((Instant) start, (Instant) end);
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/TemporalAdapter.java
Expand Up @@ -276,6 +276,10 @@ public static <T extends Temporal> boolean isBefore(T date1, T date2) {
return ((LocalDate) date1).isBefore((LocalDate) date2);
} else if (date1 instanceof LocalDateTime && date2 instanceof LocalDateTime) {
return ((LocalDateTime) date1).isBefore((LocalDateTime) date2);
} else if (date2 instanceof LocalDate) {
return (Instant.from(date1).isAfter(((LocalDate) date2).atStartOfDay(ZoneId.systemDefault()).toInstant()));
// } else if (date2 instanceof LocalDateTime) {
// return (Instant.from(date1).isAfter(((LocalDateTime) date2).atZone(ZoneId.systemDefault()).toInstant()));
}
return Instant.from(date1).isBefore(Instant.from(date2));
}
Expand All @@ -285,6 +289,8 @@ public static <T extends Temporal> boolean isAfter(T date1, T date2) {
return ((LocalDate) date1).isAfter((LocalDate) date2);
} else if (date1 instanceof LocalDateTime && date2 instanceof LocalDateTime) {
return ((LocalDateTime) date1).isAfter((LocalDateTime) date2);
} else if (date2 instanceof LocalDate) {
return (Instant.from(date1).isAfter(((LocalDate) date2).atStartOfDay(ZoneId.systemDefault()).toInstant()));
}
return Instant.from(date1).isAfter(Instant.from(date2));
}
Expand Down
Expand Up @@ -171,7 +171,7 @@ public final void testVFreeBusyComponentList() {
VEvent event2 = new VEvent().add(new DtStart<>(tzParams, startDate))
.add(new DtEnd<>(endDate));

VFreeBusy request = new VFreeBusy(startDate, endDate);
VFreeBusy request = new VFreeBusy(startDate.toInstant(), endDate.toInstant());

ComponentList<CalendarComponent> components = new ComponentList<>(Arrays.asList(event, event2));
VFreeBusy fb = new VFreeBusy(request, components.getAll());
Expand Down Expand Up @@ -233,9 +233,7 @@ public final void testVFreeBusyComponentList3() throws ConstraintViolationExcept
log.debug("\n==\n" + event.toString());
}

ZonedDateTime requestEnd = ZonedDateTime.now();

VFreeBusy request = new VFreeBusy(eventStart, requestEnd);
VFreeBusy request = new VFreeBusy(eventStart.toInstant(), Instant.now());

ComponentList<CalendarComponent> components = new ComponentList<>(Collections.singletonList(event));
VFreeBusy fb = new VFreeBusy(request, components.getAll());
Expand Down Expand Up @@ -271,19 +269,19 @@ public final void testAngelites() {

// add an event
LocalDate start = LocalDate.now();
LocalDate end = start.plusDays(-1);
LocalDate end = start.plusDays(1);

VEvent dteEnd = new VEvent(start, end, "DATE END INCLUDED");

VEvent duration = new VEvent(start, java.time.Duration.ofHours(1), "DURATION");
VEvent duration = new VEvent(start, java.time.Period.ofDays(1), "DURATION");

freeBusyTest.add(dteEnd).add(duration);

Instant dtstart = Instant.now();
Instant dtend = ZonedDateTime.now().plusDays(-2).toInstant();
Instant dtend = ZonedDateTime.now().plusDays(2).toInstant();

VFreeBusy getBusy = new VFreeBusy(dtstart, dtend);
VFreeBusy requestFree = new VFreeBusy(dtstart, dtend, java.time.Duration.ofMinutes(30));
VFreeBusy requestFree = new VFreeBusy(dtstart, dtend, java.time.Period.ofDays(30));

log.debug("GET BUSY: \n" + getBusy.toString());
log.debug("REQUEST FREE: \n" + requestFree.toString());
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/net/fortuna/ical4j/model/property/DtEndTest.java
Expand Up @@ -43,6 +43,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.Collections;

Expand Down Expand Up @@ -87,9 +88,9 @@ public static TestSuite suite() throws IOException, URISyntaxException, ParseExc
//
ParameterList newParams;
newParams = (ParameterList) dtEnd.getParameters().replace(Value.DATE);
dtEnd = new DtEnd<>(newParams, dtEnd.getDate());
log.info(dtEnd.toString());
suite.addTest(new DtEndTest("testValidationException", dtEnd));
DtEnd<LocalDate> dtEndLocalDate = new DtEnd<>(newParams, dtEnd.getDate().toLocalDate());
log.info(dtEndLocalDate.toString());
suite.addTest(new DtEndTest("testValidationException", dtEndLocalDate));

//
dtEnd = new DtEnd<>(dtEnd.getParameters(), dtEnd.getDate());
Expand All @@ -98,9 +99,9 @@ public static TestSuite suite() throws IOException, URISyntaxException, ParseExc

//
newParams = (ParameterList) dtEnd.getParameters().replace(Value.DATE);
dtEnd = new DtEnd<>(newParams, dtEnd.getDate());
log.info(dtEnd.toString());
suite.addTest(new DtEndTest("testValidation", dtEnd));
dtEndLocalDate = new DtEnd<>(newParams, dtEnd.getDate().toLocalDate());
log.info(dtEndLocalDate.toString());
suite.addTest(new DtEndTest("testValidation", dtEndLocalDate));

//
newParams = (ParameterList) dtEnd.getParameters().removeAll(Parameter.VALUE);
Expand Down
10 changes: 4 additions & 6 deletions src/test/java/net/fortuna/ical4j/model/property/ExDateTest.java
Expand Up @@ -36,15 +36,14 @@
import net.fortuna.ical4j.model.*;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.component.VTimeZone;
import net.fortuna.ical4j.model.parameter.TzId;
import net.fortuna.ical4j.util.CompatibilityHints;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Optional;

/**
* $Id$
Expand Down Expand Up @@ -97,12 +96,11 @@ public void testDstOnlyVTimeZones() throws Exception {

String id = vTZ.getProperties().getRequired(Property.TZID).getValue();
assertEquals("Europe/Berlin", id);
assertEquals(vTZ.getObservances().getAll().get(0), vTZ.getApplicableObservance(TemporalAdapter.parse("20180403").getTemporal()));
assertEquals(vTZ.getObservances().getAll().get(0),
vTZ.getApplicableObservance(TemporalAdapter.parse("20180403T000000Z").getTemporal()));

VEvent vEvent = ical.getComponents().getRequired(VEvent.VEVENT);
DtStart<?> start = vEvent.getProperties().getRequired("DTSTART");
Optional<TzId> startTzId = start.getParameters().getFirst(Parameter.TZID);
assertEquals(startTzId, vTZ.getProperties().getRequired("TZID").getParameters().getFirst(Parameter.TZID));
DtStart<ZonedDateTime> start = vEvent.getProperties().getRequired("DTSTART");
assertEquals(1522738800000L, Instant.from(start.getDate()).toEpochMilli());
}

Expand Down

0 comments on commit 109457a

Please sign in to comment.