Java Code Examples for org.dmfs.rfc5545.DateTime#UTC

The following examples show how to use org.dmfs.rfc5545.DateTime#UTC . 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: 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 2
Source File: DateTimeFieldAdapter.java    From opentasks-provider 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 3
Source File: RecurrenceSetIteratorTest.java    From lib-recur with Apache License 2.0 5 votes vote down vote up
/**
 * See https://github.com/dmfs/lib-recur/issues/61
 */
@Test
public void testMultipleRules() throws InvalidRecurrenceRuleException
{
    DateTime start = new DateTime(DateTime.UTC, 2019, 1, 1, 0, 0, 0);

    // Combine all Recurrence Rules into a RecurrenceSet
    RecurrenceSet ruleSet = new RecurrenceSet();
    ruleSet.addInstances(new RecurrenceRuleAdapter(new RecurrenceRule("FREQ=HOURLY;INTERVAL=5")));
    ruleSet.addInstances(new RecurrenceRuleAdapter(new RecurrenceRule("FREQ=DAILY;INTERVAL=1")));

    // Create an iterator using the RecurrenceSet
    RecurrenceSetIterator it = ruleSet.iterator(start.getTimeZone(), start.getTimestamp());

    assertThat(() -> it::next, startsWith(
            new DateTime(DateTime.UTC, 2019, 1, 1, 0, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 1, 5, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 1, 10, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 1, 15, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 1, 20, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 0, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 1, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 6, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 11, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 16, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 21, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 3, 0, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 3, 2, 0, 0).getTimestamp()
    ));
}
 
Example 4
Source File: RecurrenceSetIteratorTest.java    From lib-recur with Apache License 2.0 5 votes vote down vote up
/**
 * See https://github.com/dmfs/lib-recur/issues/61
 */
@Test
public void testMultipleRulesWithFastForward() throws InvalidRecurrenceRuleException
{
    DateTime start = new DateTime(DateTime.UTC, 2019, 1, 1, 0, 0, 0);

    // Combine all Recurrence Rules into a RecurrenceSet
    RecurrenceSet ruleSet = new RecurrenceSet();
    ruleSet.addInstances(new RecurrenceRuleAdapter(new RecurrenceRule("FREQ=HOURLY;INTERVAL=5")));
    ruleSet.addInstances(new RecurrenceRuleAdapter(new RecurrenceRule("FREQ=DAILY;INTERVAL=1")));

    // Create an iterator using the RecurrenceSet
    RecurrenceSetIterator it = ruleSet.iterator(start.getTimeZone(), start.getTimestamp());

    // Fast forward to the time of calculation (1/1/2019 at 10pm).
    it.fastForward(new DateTime(DateTime.UTC, 2019, 1, 1, 22, 0, 0).getTimestamp());

    assertThat(() -> it::next, startsWith(
            new DateTime(DateTime.UTC, 2019, 1, 2, 0, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 1, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 6, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 11, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 16, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 2, 21, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 3, 0, 0, 0).getTimestamp(),
            new DateTime(DateTime.UTC, 2019, 1, 3, 2, 0, 0).getTimestamp()
    ));
}
 
Example 5
Source File: DateTimeFieldAdapter.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Override
public DateTime getFrom(Cursor cursor)
{
    int tsIdx = cursor.getColumnIndex(mTimestampField);
    int tzIdx = mTzField == null ? -1 : cursor.getColumnIndex(mTzField);
    int adIdx = mAllDayField == null ? -1 : cursor.getColumnIndex(mAllDayField);

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

    if (cursor.isNull(tsIdx))
    {
        // if the time stamp is null we return null
        return null;
    }

    Long timestamp = cursor.getLong(tsIdx);

    // create a new Time for the given time zone, falling back to UTC if none is given
    String timezone = mTzField == null ? null : cursor.getString(tzIdx);
    DateTime value = new DateTime(timezone == null ? DateTime.UTC : TimeZone.getTimeZone(timezone), timestamp);

    // set the allday flag appropriately
    Integer allDayInt = adIdx < 0 ? null : cursor.getInt(adIdx);

    if ((allDayInt != null && allDayInt != 0) || (mAllDayField == null && mAllDayDefault))
    {
        value = value.toAllDay();
    }
    return value;
}
 
Example 6
Source File: DateTimeFieldAdapter.java    From opentasks-provider with Apache License 2.0 5 votes vote down vote up
@Override
public DateTime getFrom(Cursor cursor)
{
	int tsIdx = cursor.getColumnIndex(mTimestampField);
	int tzIdx = mTzField == null ? -1 : cursor.getColumnIndex(mTzField);
	int adIdx = mAllDayField == null ? -1 : cursor.getColumnIndex(mAllDayField);

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

	if (cursor.isNull(tsIdx))
	{
		// if the time stamp is null we return null
		return null;
	}

	Long timestamp = cursor.getLong(tsIdx);

	// create a new Time for the given time zone, falling back to UTC if none is given
	String timezone = mTzField == null ? null : cursor.getString(tzIdx);
	DateTime value = new DateTime(timezone == null ? DateTime.UTC : TimeZone.getTimeZone(timezone), timestamp);

	// set the allday flag appropriately
	Integer allDayInt = adIdx < 0 ? null : cursor.getInt(adIdx);

	if ((allDayInt != null && allDayInt != 0) || (mAllDayField == null && mAllDayDefault))
	{
		value = value.toAllDay();
	}
	return value;
}
 
Example 7
Source File: RecurrenceIteratorTest.java    From lib-recur with Apache License 2.0 4 votes vote down vote up
@Test
public void testMonotonicallyIncreasing() throws InvalidRecurrenceRuleException
{
    for (TestRule rule : mTestRules)
    {
        RecurrenceRule r = new RecurrenceRule(rule.rule, rule.mode);
        DateTime start = null;
        if (rule.start != null)
        {
            start = rule.start;
        }
        else if (!rule.floating)
        {
            start = ABSOLUTE_TEST_START_DATE;
        }
        else if (!rule.allday)
        {
            start = FLOATING_TEST_START_DATE;
        }
        else
        {
            start = ALLDAY_TEST_START_DATE;
        }

        // no instance should be before this day
        DateTime lastInstance = new DateTime(DateTime.UTC, 1900, 0, 1, 0, 0, 0);

        int count = 0;
        RecurrenceRuleIterator it = r.iterator(start);
        while (it.hasNext())
        {
            DateTime instance = it.nextDateTime();
            // check that the previous instance is always before the next instance
            String errMsg = "";
            errMsg = "instance no " + count + " " + lastInstance + " not before " + instance + " in rule "
                    + new RecurrenceRule(rule.rule, RfcMode.RFC5545_LAX).toString();
            assertTrue(errMsg, lastInstance.before(instance));

            lastInstance = instance;
            count++;

            if (count == MAX_ITERATIONS)
            {
                break;
            }
        }
    }
}
 
Example 8
Source File: DateTimeFieldAdapter.java    From opentasks with Apache License 2.0 4 votes vote down vote up
@Override
public DateTime getFrom(Cursor cursor, ContentValues values)
{
    int tsIdx;
    int tzIdx;
    int adIdx;
    long timestamp;
    String timeZoneId = null;
    Integer allDay = 0;

    if (values != null && values.containsKey(mTimestampField))
    {
        if (values.getAsLong(mTimestampField) == null)
        {
            // if the time stamp is null we return null
            return null;
        }
        timestamp = values.getAsLong(mTimestampField);
    }
    else if (cursor != null && (tsIdx = cursor.getColumnIndex(mTimestampField)) >= 0)
    {
        if (cursor.isNull(tsIdx))
        {
            // if the time stamp is null we return null
            return null;
        }
        timestamp = cursor.getLong(tsIdx);
    }
    else
    {
        throw new IllegalArgumentException("Missing timestamp column.");
    }

    if (mTzField != null)
    {
        if (values != null && values.containsKey(mTzField))
        {
            timeZoneId = values.getAsString(mTzField);
        }
        else if (cursor != null && (tzIdx = cursor.getColumnIndex(mTzField)) >= 0)
        {
            timeZoneId = cursor.getString(tzIdx);
        }
        else
        {
            throw new IllegalArgumentException("Missing timezone column.");
        }
    }

    if (mAllDayField != null)
    {
        if (values != null && values.containsKey(mAllDayField))
        {
            allDay = values.getAsInteger(mAllDayField);
        }
        else if (cursor != null && (adIdx = cursor.getColumnIndex(mAllDayField)) >= 0)
        {
            allDay = cursor.getInt(adIdx);
        }
        else
        {
            throw new IllegalArgumentException("Missing timezone column.");
        }
    }

    // create a new Time for the given time zone, falling back to UTC if none is given
    DateTime value = new DateTime(timeZoneId == null ? DateTime.UTC : TimeZone.getTimeZone(timeZoneId), timestamp);

    if (allDay != 0)
    {
        value = value.toAllDay();
    }
    return value;
}
 
Example 9
Source File: DateTimeFieldAdapter.java    From opentasks-provider with Apache License 2.0 4 votes vote down vote up
@Override
public DateTime getFrom(Cursor cursor, ContentValues values)
{
	int tsIdx;
	int tzIdx;
	int adIdx;
	long timestamp;
	String timeZoneId = null;
	Integer allDay = 0;

	if (values != null && values.containsKey(mTimestampField))
	{
		if (values.getAsLong(mTimestampField) == null)
		{
			// if the time stamp is null we return null
			return null;
		}
		timestamp = values.getAsLong(mTimestampField);
	}
	else if (cursor != null && (tsIdx = cursor.getColumnIndex(mTimestampField)) >= 0)
	{
		if (cursor.isNull(tsIdx))
		{
			// if the time stamp is null we return null
			return null;
		}
		timestamp = cursor.getLong(tsIdx);
	}
	else
	{
		throw new IllegalArgumentException("Missing timestamp column.");
	}

	if (mTzField != null)
	{
		if (values != null && values.containsKey(mTzField))
		{
			timeZoneId = values.getAsString(mTzField);
		}
		else if (cursor != null && (tzIdx = cursor.getColumnIndex(mTzField)) >= 0)
		{
			timeZoneId = cursor.getString(tzIdx);
		}
		else
		{
			throw new IllegalArgumentException("Missing timezone column.");
		}
	}

	if (mAllDayField != null)
	{
		if (values != null && values.containsKey(mAllDayField))
		{
			allDay = values.getAsInteger(mAllDayField);
		}
		else if (cursor != null && (adIdx = cursor.getColumnIndex(mAllDayField)) >= 0)
		{
			allDay = cursor.getInt(adIdx);
		}
		else
		{
			throw new IllegalArgumentException("Missing timezone column.");
		}
	}

	// create a new Time for the given time zone, falling back to UTC if none is given
	DateTime value = new DateTime(timeZoneId == null ? DateTime.UTC : TimeZone.getTimeZone(timeZoneId), timestamp);

	if (allDay != 0)
	{
		value = value.toAllDay();
	}
	return value;
}