org.dmfs.rfc5545.DateTime Java Examples

The following examples show how to use org.dmfs.rfc5545.DateTime. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DateTimeIterableFieldAdapter.java    From opentasks with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<DateTime> getFrom(Cursor cursor)
{
    int tdLIdx = cursor.getColumnIndex(mDateTimeListFieldName);
    int tzIdx = mTimeZoneFieldName == null ? -1 : cursor.getColumnIndex(mTimeZoneFieldName);

    if (tdLIdx < 0 || (mTimeZoneFieldName != null && tzIdx < 0))
    {
        throw new IllegalArgumentException("At least one column is missing in cursor.");
    }

    if (cursor.isNull(tdLIdx))
    {
        // if the time stamp list is null we return an empty Iterable
        return EmptyIterable.instance();
    }

    String datetimeList = cursor.getString(tdLIdx);

    // create a new TimeZone for the given time zone string
    String timezoneString = mTimeZoneFieldName == null ? null : cursor.getString(tzIdx);
    TimeZone timeZone = timezoneString == null ? null : TimeZone.getTimeZone(timezoneString);

    return new DateTimeList(timeZone, datetimeList);
}
 
Example #2
Source File: RecurrenceRuleAdapterTest.java    From lib-recur with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetIteratorSyncedStartWithCount() throws Exception
{
    AbstractRecurrenceAdapter.InstanceIterator iterator = new RecurrenceRuleAdapter(new RecurrenceRule("FREQ=MONTHLY;COUNT=3"))
            .getIterator(TimeZone.getTimeZone("Europe/Berlin"), DateTime.parse("Europe/Berlin", "20170110T113012").getTimestamp());

    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170110T113012").getTimestamp()));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170110T113012").getTimestamp()));
    assertThat(iterator.next(), is(DateTime.parse("Europe/Berlin", "20170110T113012").getTimestamp()));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170210T113012").getTimestamp()));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170210T113012").getTimestamp()));
    assertThat(iterator.next(), is(DateTime.parse("Europe/Berlin", "20170210T113012").getTimestamp()));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170310T113012").getTimestamp()));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170310T113012").getTimestamp()));
    assertThat(iterator.next(), is(DateTime.parse("Europe/Berlin", "20170310T113012").getTimestamp()));
    assertThat(iterator.hasNext(), is(false));
    assertThat(iterator.hasNext(), is(false));
}
 
Example #3
Source File: BeforeMatcherTest.java    From lib-recur with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception
{
    assertThat(before(DateTime.parse("20180101T010000Z")),
            AllOf.<Matcher<DateTime>>allOf(
                    matches(DateTime.parse("20180101T000059Z")),
                    mismatches(DateTime.parse("20180101T010001Z"), "not before 20180101T010000Z"),
                    mismatches(DateTime.parse("20180101T010000Z"), "not before 20180101T010000Z"),
                    describesAs("before 20180101T010000Z")
            ));

    assertThat(before("20180101T010000Z"),
            AllOf.<Matcher<DateTime>>allOf(
                    matches(DateTime.parse("20180101T000059Z")),
                    mismatches(DateTime.parse("20180101T010001Z"), "not before 20180101T010000Z"),
                    mismatches(DateTime.parse("20180101T010000Z"), "not before 20180101T010000Z"),
                    describesAs("before 20180101T010000Z")
            ));
}
 
Example #4
Source File: RecurrenceRuleAdapterTest.java    From lib-recur with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetIteratorSyncedStartInfinite() throws Exception
{
    AbstractRecurrenceAdapter.InstanceIterator iterator = new RecurrenceRuleAdapter(new RecurrenceRule("FREQ=MONTHLY"))
            .getIterator(TimeZone.getTimeZone("Europe/Berlin"), DateTime.parse("Europe/Berlin", "20170110T113012").getTimestamp());

    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170110T113012").getTimestamp()));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170110T113012").getTimestamp()));
    assertThat(iterator.next(), is(DateTime.parse("Europe/Berlin", "20170110T113012").getTimestamp()));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170210T113012").getTimestamp()));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170210T113012").getTimestamp()));
    assertThat(iterator.next(), is(DateTime.parse("Europe/Berlin", "20170210T113012").getTimestamp()));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170310T113012").getTimestamp()));
    assertThat(iterator.peek(), is(DateTime.parse("Europe/Berlin", "20170310T113012").getTimestamp()));
    assertThat(iterator.next(), is(DateTime.parse("Europe/Berlin", "20170310T113012").getTimestamp()));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
}
 
Example #5
Source File: AfterMatcherTest.java    From lib-recur with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception
{
    assertThat(after(DateTime.parse("20180101T010000Z")),
            AllOf.<Matcher<DateTime>>allOf(
                    matches(DateTime.parse("20180101T010001Z")),
                    mismatches(DateTime.parse("20180101T005959Z"), "not after 20180101T010000Z"),
                    mismatches(DateTime.parse("20180101T010000Z"), "not after 20180101T010000Z"),
                    describesAs("after 20180101T010000Z")
            ));

    assertThat(after("20180101T010000Z"),
            AllOf.<Matcher<DateTime>>allOf(
                    matches(DateTime.parse("20180101T010001Z")),
                    mismatches(DateTime.parse("20180101T005959Z"), "not after 20180101T010000Z"),
                    mismatches(DateTime.parse("20180101T010000Z"), "not after 20180101T010000Z"),
                    describesAs("after 20180101T010000Z")
            ));
}
 
Example #6
Source File: InstanceTestDataTest.java    From opentasks with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDueDateAndOriginalTime()
{
    DateTime due = DateTime.now();
    DateTime original = due.addDuration(Duration.parse("P2DT2H"));
    assertThat(new InstanceTestData(absent(), new Present<>(due), new Present<>(original), 5),
            builds(
                    withValuesOnly(
                            withNullValue(TaskContract.Instances.INSTANCE_START),
                            withNullValue(TaskContract.Instances.INSTANCE_START_SORTING),
                            containing(TaskContract.Instances.INSTANCE_DUE, due.getTimestamp()),
                            containing(TaskContract.Instances.INSTANCE_DUE_SORTING, due.shiftTimeZone(TimeZone.getDefault()).getInstance()),
                            withNullValue(TaskContract.Instances.INSTANCE_DURATION),
                            containing(TaskContract.Instances.INSTANCE_ORIGINAL_TIME, original.getTimestamp()),
                            containing(TaskContract.Instances.DISTANCE_FROM_CURRENT, 5),
                            withNullValue(TaskContract.Instances.DTSTART),
                            containing(TaskContract.Instances.DUE, due.getTimestamp()),
                            containing(TaskContract.Instances.ORIGINAL_INSTANCE_TIME, original.getTimestamp()),
                            withNullValue(TaskContract.Instances.DURATION),
                            withNullValue(TaskContract.Instances.RRULE),
                            withNullValue(TaskContract.Instances.RDATE),
                            withNullValue(TaskContract.Instances.EXDATE)
                    )
            ));
}
 
Example #7
Source File: TimeDataTest.java    From opentasks with Apache License 2.0 6 votes vote down vote up
@Test
public void test_whenStartHasAllDayFlag_correspondingValueIsOne()
{
    DateTime start = DateTime.now().toAllDay();
    DateTime due = start.addDuration(new Duration(1, 3, 0));

    assertThat(new TimeData<>(start, due),
            builds(
                    withValuesOnly(
                            containing(Tasks.DTSTART, start.getTimestamp()),
                            containing(Tasks.TZ, "UTC"),
                            containing(Tasks.IS_ALLDAY, 1),
                            containing(Tasks.DUE, due.getTimestamp()),
                            withNullValue(Tasks.DURATION)
                    )));
}
 
Example #8
Source File: InstanceTestDataTest.java    From opentasks with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDate()
{
    DateTime start = DateTime.now();
    DateTime due = start.addDuration(Duration.parse("P1DT1H"));
    assertThat(new InstanceTestData(start, due, absent(), 5),
            builds(
                    withValuesOnly(
                            containing(TaskContract.Instances.INSTANCE_START, start.getTimestamp()),
                            containing(TaskContract.Instances.INSTANCE_START_SORTING, start.shiftTimeZone(TimeZone.getDefault()).getInstance()),
                            containing(TaskContract.Instances.INSTANCE_DUE, due.getTimestamp()),
                            containing(TaskContract.Instances.INSTANCE_DUE_SORTING, due.shiftTimeZone(TimeZone.getDefault()).getInstance()),
                            containing(TaskContract.Instances.INSTANCE_DURATION, due.getTimestamp() - start.getTimestamp()),
                            withNullValue(TaskContract.Instances.INSTANCE_ORIGINAL_TIME),
                            containing(TaskContract.Instances.DISTANCE_FROM_CURRENT, 5),
                            containing(TaskContract.Instances.DTSTART, start.getTimestamp()),
                            containing(TaskContract.Instances.DUE, due.getTimestamp()),
                            withNullValue(TaskContract.Instances.ORIGINAL_INSTANCE_TIME),
                            withNullValue(TaskContract.Instances.DURATION),
                            withNullValue(TaskContract.Instances.RRULE),
                            withNullValue(TaskContract.Instances.RDATE),
                            withNullValue(TaskContract.Instances.EXDATE)
                    )
            ));
}
 
Example #9
Source File: InstanceTestDataTest.java    From opentasks with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithStartDateAndOriginalTime()
{
    DateTime start = DateTime.now();
    DateTime original = start.addDuration(Duration.parse("P2DT2H"));
    assertThat(new InstanceTestData(new Present<>(start), absent(), new Present<>(original), 5),
            builds(
                    withValuesOnly(
                            containing(TaskContract.Instances.INSTANCE_START, start.getTimestamp()),
                            containing(TaskContract.Instances.INSTANCE_START_SORTING, start.shiftTimeZone(TimeZone.getDefault()).getInstance()),
                            withNullValue(TaskContract.Instances.INSTANCE_DUE),
                            withNullValue(TaskContract.Instances.INSTANCE_DUE_SORTING),
                            withNullValue(TaskContract.Instances.INSTANCE_DURATION),
                            containing(TaskContract.Instances.INSTANCE_ORIGINAL_TIME, original.getTimestamp()),
                            containing(TaskContract.Instances.DISTANCE_FROM_CURRENT, 5),
                            containing(TaskContract.Instances.DTSTART, start.getTimestamp()),
                            containing(TaskContract.Instances.ORIGINAL_INSTANCE_TIME, original.getTimestamp()),
                            withNullValue(TaskContract.Instances.DUE),
                            withNullValue(TaskContract.Instances.DURATION),
                            withNullValue(TaskContract.Instances.RRULE),
                            withNullValue(TaskContract.Instances.RDATE),
                            withNullValue(TaskContract.Instances.EXDATE)
                    )
            ));
}
 
Example #10
Source File: Detaching.java    From opentasks with Apache License 2.0 6 votes vote down vote up
private void updateStart(TaskAdapter task, DateTime newStart)
{
    // this new instance becomes the new start (or due if we don't have a start)
    if (task.valueOf(TaskAdapter.DTSTART) != null)
    {
        DateTime oldStart = task.valueOf(TaskAdapter.DTSTART);
        task.set(TaskAdapter.DTSTART, newStart);
        if (task.valueOf(TaskAdapter.DUE) != null)
        {
            long duration = task.valueOf(TaskAdapter.DUE).getTimestamp() - oldStart.getTimestamp();
            task.set(TaskAdapter.DUE,
                    newStart.addDuration(
                            new Duration(1, (int) (duration / (3600 * 24 * 1000)), (int) (duration % (3600 * 24 * 1000)) / 1000)));
        }
    }
    else
    {
        task.set(TaskAdapter.DUE, newStart);
    }

}
 
Example #11
Source File: TimeDataTest.java    From opentasks with Apache License 2.0 6 votes vote down vote up
@Test
public void test_whenStartAndDurationAreProvided_setsThemAndNullsDue()
{
    DateTime start = DateTime.now();
    Duration duration = new Duration(1, 3, 0);

    assertThat(new TimeData<>(start, duration),
            builds(
                    withValuesOnly(
                            containing(Tasks.DTSTART, start.getTimestamp()),
                            containing(Tasks.TZ, "UTC"),
                            containing(Tasks.IS_ALLDAY, 0),
                            withNullValue(Tasks.DUE),
                            containing(Tasks.DURATION, duration.toString())
                    )));
}
 
Example #12
Source File: DayOfMonthMatcherTest.java    From lib-recur with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception
{
    assertThat(onDayOfMonth(10),
            AllOf.<Matcher<DateTime>>allOf(
                    matches(DateTime.parse("20180710T010001Z")),
                    mismatches(DateTime.parse("20181001T005959Z"), "day of month was <1>"),
                    mismatches(DateTime.parse("20181011T010000Z"), "day of month was <11>"),
                    describesAs("day of month (<10>)")
            ));
    assertThat(onDayOfMonth(6, 8, 10),
            AllOf.<Matcher<DateTime>>allOf(
                    matches(DateTime.parse("20180706T010001Z")),
                    matches(DateTime.parse("20180708T010002Z")),
                    matches(DateTime.parse("20180710T010003Z")),
                    mismatches(DateTime.parse("20180605T005959Z"), "day of month was <5>"),
                    mismatches(DateTime.parse("20180811T005958Z"), "day of month was <11>"),
                    mismatches(DateTime.parse("20181009T005957Z"), "day of month was <9>"),
                    mismatches(DateTime.parse("20180907T010000Z"), "day of month was <7>"),
                    describesAs("day of month (<6> or <8> or <10>)")
            ));
}
 
Example #13
Source File: WeekDayMatcherTest.java    From lib-recur with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception
{
    assertThat(onWeekDay(Weekday.WE),
            AllOf.<Matcher<DateTime>>allOf(
                    matches(DateTime.parse("20180808T010001Z")),
                    mismatches(DateTime.parse("20180809T005959Z"), "weekday was <TH>"),
                    mismatches(DateTime.parse("20180807T010000Z"), "weekday was <TU>"),
                    describesAs("weekday (<WE>)")
            ));
    assertThat(onWeekDay(Weekday.MO, Weekday.WE, Weekday.FR),
            AllOf.<Matcher<DateTime>>allOf(
                    matches(DateTime.parse("20180808T010001Z")),
                    matches(DateTime.parse("20180806T010002Z")),
                    matches(DateTime.parse("20180810T010003Z")),
                    mismatches(DateTime.parse("20180805T005959Z"), "weekday was <SU>"),
                    mismatches(DateTime.parse("20180804T005958Z"), "weekday was <SA>"),
                    mismatches(DateTime.parse("20180814T005957Z"), "weekday was <TU>"),
                    mismatches(DateTime.parse("20180816T010000Z"), "weekday was <TH>"),
                    describesAs("weekday (<MO> or <WE> or <FR>)")
            ));

}
 
Example #14
Source File: TaskInstanceIterableTest.java    From opentasks with Apache License 2.0 6 votes vote down vote up
@Test
public void testAbsolute() throws Exception
{
    TaskAdapter taskAdapter = new ContentValuesTaskAdapter(new ContentValues());
    taskAdapter.set(TaskAdapter.DTSTART, DateTime.parse("Europe/Berlin", "20170606T121314"));
    taskAdapter.set(TaskAdapter.RRULE, new RecurrenceRule("FREQ=DAILY;INTERVAL=2;COUNT=10"));

    assertThat(new TaskInstanceIterable(taskAdapter),
            iteratesTo(
                    DateTime.parse("Europe/Berlin", "20170606T121314"),
                    DateTime.parse("Europe/Berlin", "20170608T121314"),
                    DateTime.parse("Europe/Berlin", "20170610T121314"),
                    DateTime.parse("Europe/Berlin", "20170612T121314"),
                    DateTime.parse("Europe/Berlin", "20170614T121314"),
                    DateTime.parse("Europe/Berlin", "20170616T121314"),
                    DateTime.parse("Europe/Berlin", "20170618T121314"),
                    DateTime.parse("Europe/Berlin", "20170620T121314"),
                    DateTime.parse("Europe/Berlin", "20170622T121314"),
                    DateTime.parse("Europe/Berlin", "20170624T121314")
            ));
}
 
Example #15
Source File: DateTimeFieldAdapter.java    From opentasks with Apache License 2.0 6 votes vote down vote up
@Override
public DateTime getFrom(ContentValues values)
{
    Long timestamp = values.getAsLong(mTimestampField);
    if (timestamp == null)
    {
        // if the time stamp is null we return null
        return null;
    }
    // create a new Time for the given time zone, falling back to UTC if none is given
    String timezone = mTzField == null ? null : values.getAsString(mTzField);
    DateTime value = new DateTime(timezone == null ? DateTime.UTC : TimeZone.getTimeZone(timezone), timestamp);

    // cache mAlldayField locally
    String allDayField = mAllDayField;

    // set the allday flag appropriately
    Integer allDayInt = allDayField == null ? null : values.getAsInteger(allDayField);

    if ((allDayInt != null && allDayInt != 0) || (allDayField == null && mAllDayDefault))
    {
        value = value.toAllDay();
    }

    return value;
}
 
Example #16
Source File: EffectiveDueDateTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Test
public void test_whenDueIsPresent_shouldUseThat()
{
    long timestamp = System.currentTimeMillis();

    RowDataSnapshot<Tasks> mockData = failingMock(RowDataSnapshot.class);
    doReturn(new Present<>(timestamp)).when(mockData).data(eq(Tasks.DUE), any());
    doReturn(absent()).when(mockData).data(eq(Tasks.DTSTART), any());
    doReturn(absent()).when(mockData).data(eq(Tasks.DURATION), any());
    doReturn(new Present<>(false)).when(mockData).data(eq(Tasks.IS_ALLDAY), any());
    doReturn(new Present<>(TimeZone.getTimeZone("UTC"))).when(mockData).data(eq(Tasks.TZ), any());

    DateTime actual = new EffectiveDueDate(mockData).value();
    assertEquals(timestamp, actual.getTimestamp());
}
 
Example #17
Source File: Overridden.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Override
public ContentValues value()
{
    ContentValues values = mDelegate.value();
    values.put(TaskContract.Instances.INSTANCE_ORIGINAL_TIME,
            new Backed<Long>(
                    new FirstPresent<>(
                            new Seq<>(
                                    new Mapped<>(DateTime::getTimestamp, mOriginalTime),
                                    new NullSafe<>(values.getAsLong(TaskContract.Instances.INSTANCE_START)),
                                    new NullSafe<>(values.getAsLong(TaskContract.Instances.INSTANCE_DUE)))),
                    () -> null).value());
    return values;
}
 
Example #18
Source File: TaskProviderTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
/**
 * Create task with start and due and update it with new values, check datetime values including generated duration.
 */
@Test
public void testInsertTaskWithStartAndDueMovedForward()
{
    RowSnapshot<TaskLists> taskList = new VirtualRowSnapshot<>(new LocalTaskListsTable(mAuthority));
    RowSnapshot<Tasks> task = new VirtualRowSnapshot<>(new TaskListScoped(taskList, new TasksTable(mAuthority)));

    DateTime start = DateTime.now();
    DateTime due = start.addDuration(new Duration(1, 1, 0));
    Duration duration = new Duration(1, 2, 0);

    DateTime startNew = start.addDuration(duration);
    DateTime dueNew = due.addDuration(duration);

    assertThat(new Seq<>(
            new Put<>(taskList, new EmptyRowData<TaskLists>()),
            new Put<>(task, new TimeData<>(start, due)),
            new Put<>(task, new TimeData<>(startNew, dueNew))
    ), resultsIn(mClient,
            new Assert<>(task, new Composite<>(
                    new TimeData<>(startNew, dueNew),
                    new VersionData(1))),
            new AssertRelated<>(
                    new InstanceTable(mAuthority), Instances.TASK_ID, task,
                    new Composite<Instances>(
                            new InstanceTestData(
                                    startNew.shiftTimeZone(TimeZone.getDefault()),
                                    dueNew.shiftTimeZone(TimeZone.getDefault()),
                                    absent(),
                                    0),
                            new CharSequenceRowData<>(Tasks.TZ, "UTC"))
            )));
}
 
Example #19
Source File: AfterMatcher.java    From lib-recur with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean matchesSafely(DateTime item, Description mismatchDescription)
{
    if (!item.after(mDateTime))
    {
        mismatchDescription.appendText(String.format("not after %s", mDateTime));
        return false;
    }
    return true;
}
 
Example #20
Source File: DateTimeIterableFieldAdapterTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetInMultiAbsolute()
{
    ContentValues values = new ContentValues();
    FieldAdapter<Iterable<DateTime>, ?> adapter = new DateTimeIterableFieldAdapter<TaskAdapter>("x", "y");
    adapter.setIn(values, new Seq<>(DateTime.parse("Europe/Berlin", "20180109T150000"), DateTime.parse("Europe/Berlin", "20180110T150000"),
            DateTime.parse("Europe/Berlin", "20180111T150000")));
    assertThat(values.getAsString("x"), is("20180109T140000Z,20180110T140000Z,20180111T140000Z"));
}
 
Example #21
Source File: RDatesTaskDataTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingle()
{
    assertThat(new RDatesTaskData(new SingletonIterable<>(DateTime.parse("Europe/Berlin", "20171212T123456"))),
            builds(
                    withValuesOnly(
                            containing(TaskContract.Tasks.RDATE, "20171212T113456Z"))));
}
 
Example #22
Source File: OverriddenTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Test
public void testPresentWithStartAndDue()
{
    ContentValues values = new ContentValues();
    values.put(TaskContract.Instances.INSTANCE_START, 10);
    values.put(TaskContract.Instances.INSTANCE_DUE, 20);

    ContentValues instanceData = new Overridden(new Present<>(new DateTime(40)), () -> new ContentValues(values)).value();
    assertThat(instanceData, new ContentValuesWithLong(TaskContract.Instances.INSTANCE_ORIGINAL_TIME, 40));
    assertThat(instanceData.size(), is(3));
}
 
Example #23
Source File: DueDatedTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartEurope()
{
    DateTime start = DateTime.parse("Europe/Berlin", "20171208T125500");

    ContentValues instanceData = new DueDated(new Present<>(start), ContentValues::new).value();

    assertThat(instanceData, new ContentValuesWithLong(TaskContract.Instances.INSTANCE_DUE, start.getTimestamp()));
    assertThat(instanceData,
            new ContentValuesWithLong(TaskContract.Instances.INSTANCE_DUE_SORTING, start.shiftTimeZone(TimeZone.getDefault()).getInstance()));
    assertThat(instanceData.size(), is(2));
}
 
Example #24
Source File: RDatesTaskDataTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFloating()
{
    assertThat(new RDatesTaskData(new SingletonIterable<>(DateTime.parse("20171212T123456"))),
            builds(
                    withValuesOnly(
                            containing(TaskContract.Tasks.RDATE, "20171212T123456"))));
}
 
Example #25
Source File: TestRule.java    From lib-recur with Apache License 2.0 5 votes vote down vote up
public void assertUntil(DateTime instance)
{
    if (count == -1 && until != null)
    {
        String errMsg = "";
        // errMsg = "instance " + instance + " after " + until + " in rule " + rule;
        assertTrue(errMsg, !instance.after(until));
    }
}
 
Example #26
Source File: TaskInstanceIterable.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<DateTime> iterator()
{
    DateTime dtstart = new Backed<DateTime>(new NullSafe<>(mTaskAdapter.valueOf(TaskAdapter.DTSTART)), () -> mTaskAdapter.valueOf(TaskAdapter.DUE)).value();

    RecurrenceSet set = new RecurrenceSet();
    RecurrenceRule rule = mTaskAdapter.valueOf(TaskAdapter.RRULE);
    if (rule != null)
    {
        if (rule.getUntil() != null && dtstart.isFloating() != rule.getUntil().isFloating())
        {
            // rule UNTIL date mismatches start. This is merely a workaround for existing users. In future we should make sure
            // such tasks don't exist
            if (dtstart.isFloating())
            {
                // make until floating too by making it floating in the current time zone
                rule.setUntil(rule.getUntil().shiftTimeZone(TimeZone.getDefault()).swapTimeZone(null));
            }
            else
            {
                // anchor UNTIL in the current time zone
                rule.setUntil(new DateTime(null, rule.getUntil().getTimestamp()).swapTimeZone(TimeZone.getDefault()));
            }
        }
        set.addInstances(new RecurrenceRuleAdapter(rule));
    }

    set.addInstances(new RecurrenceList(new Timestamps(mTaskAdapter.valueOf(TaskAdapter.RDATE)).value()));
    set.addExceptions(new RecurrenceList(new Timestamps(mTaskAdapter.valueOf(TaskAdapter.EXDATE)).value()));

    RecurrenceSetIterator setIterator = set.iterator(dtstart.getTimeZone(), dtstart.getTimestamp(),
            System.currentTimeMillis() + 10L * 356L * 3600L * 1000L);

    return new TaskInstanceIterator(dtstart, setIterator, mTaskAdapter.valueOf(TaskAdapter.TIMEZONE_RAW));
}
 
Example #27
Source File: DateTimeIterableFieldAdapterTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFromCVFloating1()
{
    ContentValues values = new ContentValues();
    FieldAdapter<Iterable<DateTime>, ?> adapter = new DateTimeIterableFieldAdapter<TaskAdapter>("x", "y");
    values.put("x", "20180109T140000");
    values.putNull("y");
    assertThat(adapter.getFrom(values), iteratesTo(DateTime.parse("20180109T140000")));
}
 
Example #28
Source File: RFC5545Schedule.java    From Singularity with Apache License 2.0 5 votes vote down vote up
public Date getNextValidTime() {
  final long now = System.currentTimeMillis();
  DateTime startDateTime = new DateTime(
    dtStart.getYear(),
    (dtStart.getMonthOfYear() - 1),
    dtStart.getDayOfMonth(),
    dtStart.getHourOfDay(),
    dtStart.getMinuteOfHour(),
    dtStart.getSecondOfMinute()
  );
  RecurrenceRuleIterator timeIterator = recurrenceRule.iterator(startDateTime);

  int count = 0;
  while (
    timeIterator.hasNext() &&
    (
      count < MAX_ITERATIONS ||
      (recurrenceRule.hasPart(Part.COUNT) && count < recurrenceRule.getCount())
    )
  ) {
    count++;
    long nextRunAtTimestamp = timeIterator.nextMillis();
    if (nextRunAtTimestamp >= now) {
      return new Date(nextRunAtTimestamp);
    }
  }
  return null;
}
 
Example #29
Source File: TestDate.java    From lib-recur with Apache License 2.0 5 votes vote down vote up
public void testEvent()
{
	cal = DateTime.parse(dateTime);
	testFlags();
	testDate();
	testTime();
	testToString();
	testTimeZone();
	testToAllDay();
	testDate();

}
 
Example #30
Source File: JsonAccessToken.java    From oauth2-essentials with Apache License 2.0 5 votes vote down vote up
@Override
public DateTime expirationDate() throws ProtocolException
{
    try
    {
        return mIssueDate.addDuration(new Duration(1, 0, mTokenResponse.getInt("expires_in")));
    }
    catch (JSONException e)
    {
        throw new ProtocolException("Can't read expires_in from token response", e);
    }
}