Skip to content

Commit

Permalink
Fix timezone issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mangstadt committed Oct 28, 2023
1 parent ca2231d commit c762c6b
Show file tree
Hide file tree
Showing 6 changed files with 1,254 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/main/java/biweekly/io/ICalTimeZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,20 +408,26 @@ private DateValue getObservanceDateClosestToTheGivenDate(Observance observance,
RecurrenceIterator it = createIterator(observance);

/*
* The "advanceTo()" method skips all dates that are less than the
* given date. I would have thought that we would have to call
* "next()" once because we want it to skip the date that is equal
* to the "last" date. But this causes all the unit tests to fail,
* so I guess not.
* Calling "it.advanceTo()" here causes problems.
*
* See: https://github.com/mangstadt/biweekly/issues/126
*/
it.advanceTo(last);
//it.next();
//it.advanceTo(last);

DateValue prev = null, cur = null;
boolean stopped = false;
while (it.hasNext()) {
cur = it.next();
dateCache.add(cur);
int curCompareToLast = cur.compareTo(last);
if (curCompareToLast < 0) {
continue;
}
if (curCompareToLast > 0) {
dateCache.add(cur);
}
if (curCompareToLast == 0) {
//do nothing; don't add to dateCache
}

if (givenDate.compareTo(cur) < 0) {
//stop if we have passed the givenTime
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/biweekly/issues/Issue115.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package biweekly.issues;

import static biweekly.util.TestUtils.date;
import static org.junit.Assert.assertEquals;

import java.util.Date;

import org.junit.Test;

import biweekly.Biweekly;
import biweekly.ICalendar;
import biweekly.component.VEvent;

/**
* @author Michael Angstadt
* @see "https://github.com/mangstadt/biweekly/issues/115"
*/
public class Issue115 {
@Test
public void test1() throws Exception {
ICalendar ical = Biweekly.parse(getClass().getResourceAsStream("issue115-1.ics")).first();

/*
* The event comes before the last Sunday in October, so it should
* be in daylight time (offset of +02:00).
*/
VEvent event = ical.getEvents().get(0);

Date expected = date(2022, 10, 29, 8, 0, 0, "+0200");
Date actual = event.getDateStart().getValue();
assertEquals(expected, actual);

expected = date(2022, 10, 29, 8, 30, 0, "+0200");
actual = event.getDateEnd().getValue();
assertEquals(expected, actual);
}

@Test
public void test2() throws Exception {
ICalendar ical = Biweekly.parse(getClass().getResourceAsStream("issue115-2.ics")).first();

/*
* The event comes after the last Sunday in October, so it should
* be in standard time (offset of +01:00).
*/
VEvent event = ical.getEvents().get(0);

Date expected = date(2022, 10, 31, 8, 0, 0, "+0100");
System.out.println(expected);
Date actual = event.getDateStart().getValue();
assertEquals(expected, actual);

expected = date(2022, 10, 31, 8, 30, 0, "+0100");
actual = event.getDateEnd().getValue();
assertEquals(expected, actual);
}
}
55 changes: 55 additions & 0 deletions src/test/java/biweekly/issues/Issue126.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package biweekly.issues;

import static biweekly.util.TestUtils.date;
import static org.junit.Assert.assertEquals;

import java.util.Date;

import org.junit.Test;

import biweekly.Biweekly;
import biweekly.ICalendar;
import biweekly.component.VEvent;

/**
* @author Michael Angstadt
* @see "https://github.com/mangstadt/biweekly/issues/126"
*/
public class Issue126 {
@Test
public void test() throws Exception {
ICalendar ical = Biweekly.parse(getClass().getResourceAsStream("issue126.ics")).first();

/*
* The first event comes after the last Sunday in October, so it should
* be in standard time (offset of +01:00).
*/
{
VEvent event = ical.getEvents().get(0);

Date expected = date(2023, 11, 22, 9, 30, 0, "+0100");
Date actual = event.getDateStart().getValue();
assertEquals(expected, actual);

expected = date(2023, 11, 22, 12, 0, 0, "+0100");
actual = event.getDateEnd().getValue();
assertEquals(expected, actual);
}

/*
* The second event comes after the last Sunday in March, so it should
* be in daylight time (offset of +02:00).
*/
{
VEvent event = ical.getEvents().get(1);

Date expected = date(2023, 4, 22, 9, 30, 0, "+0200");
Date actual = event.getDateStart().getValue();
assertEquals(expected, actual);

expected = date(2023, 4, 22, 12, 0, 0, "+0200");
actual = event.getDateEnd().getValue();
assertEquals(expected, actual);
}
}
}
Loading

0 comments on commit c762c6b

Please sign in to comment.