Java Code Examples for org.dmfs.rfc5545.DateTime#isFloating()

The following examples show how to use org.dmfs.rfc5545.DateTime#isFloating() . 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: RecurrenceRule.java    From lib-recur with Apache License 2.0 6 votes vote down vote up
/**
 * Set the latest possible date of an instance. This will remove any COUNT rule if present. If the time zone of <code>until</code> is not UTC and until is
 * not floating it's automatically converted to UTC.
 *
 * @param until
 *         The UNTIL part of this rule or <code>null</code> to let the instances recur forever.
 */
public void setUntil(DateTime until)
{
    if (until == null)
    {
        mParts.remove(Part.UNTIL);
        mParts.remove(Part.COUNT);
    }
    else
    {
        if ((!until.isFloating() && !DateTime.UTC.equals(until.getTimeZone())) || !mCalendarMetrics.equals(
                until.getCalendarMetrics()))
        {
            mParts.put(Part.UNTIL, new DateTime(mCalendarMetrics, DateTime.UTC, until.getTimestamp()));
        }
        else
        {
            mParts.put(Part.UNTIL, until);
        }
        mParts.remove(Part.COUNT);
    }
}
 
Example 2
Source File: DateFormatter.java    From opentasks with Apache License 2.0 6 votes vote down vote up
/**
 * {@link Time} will eventually be replaced with {@link DateTime} in the project.
 * This conversion function is only needed in the transition period.
 */
@VisibleForTesting
Time toTime(DateTime dateTime)
{
    if (dateTime.isFloating() && !dateTime.isAllDay())
    {
        throw new IllegalArgumentException("Cannot support floating DateTime that is not all-day, can't represent it with Time");
    }

    // Time always needs a TimeZone (default ctor falls back to TimeZone.getDefault())
    String timeZoneId = dateTime.getTimeZone() == null ? "UTC" : dateTime.getTimeZone().getID();
    Time time = new Time(timeZoneId);

    time.set(dateTime.getTimestamp());

    // TODO Would using time.set(monthDay, month, year) be better?
    if (dateTime.isAllDay())
    {
        time.allDay = true;
        // This is needed as per time.allDay docs:
        time.hour = 0;
        time.minute = 0;
        time.second = 0;
    }
    return time;
}
 
Example 3
Source File: RecurrenceRuleIterator.java    From lib-recur with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link RecurrenceRuleIterator} that gets its input from <code>ruleIterator</code>.
 *
 * @param ruleIterator
 *         The last {@link RuleIterator} in the chain of iterators.
 * @param start
 *         The first instance to iterate.
 */
RecurrenceRuleIterator(RuleIterator ruleIterator, DateTime start, CalendarMetrics calendarMetrics)
{
    mRuleIterator = ruleIterator;
    mAllDay = start.isAllDay();
    mCalendarMetrics = calendarMetrics;
    mTimeZone = start.isFloating() ? null : start.getTimeZone();
    fetchNextInstance();
}
 
Example 4
Source File: UntilLimiter.java    From lib-recur with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new limiter for the UNTIL part.
 *
 * @param rule
 *         The {@link RecurrenceRule} to filter.
 * @param previous
 *         The previous filter instance.
 * @param start
 *         The first instance. This is used to determine if the iterated instances are floating or not.
 */
public UntilLimiter(RecurrenceRule rule, RuleIterator previous, CalendarMetrics calendarMetrics, TimeZone startTimezone)
{
    super(previous);
    DateTime until = rule.getUntil();
    if (!until.isFloating())
    {
        until = until.shiftTimeZone(startTimezone);
    }
    mUntil = until.getInstance();
}
 
Example 5
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 6
Source File: RowStateInfo.java    From opentasks with Apache License 2.0 5 votes vote down vote up
private boolean isPast(@NonNull DateTime dt)
{
    DateTime now = DateTime.nowAndHere();
    dt = dt.isAllDay() ? dt.startOfDay() : dt;
    dt = dt.isFloating() ? dt.swapTimeZone(now.getTimeZone()) : dt;
    return !now.before(dt);
}
 
Example 7
Source File: DateTimeToTimeConversionTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
/**
 * Contains the definition/requirement of when a {@link DateTime} and {@link Time} is considered equivalent in this project.
 */
private boolean isEquivalentDateTimeAndTime(DateTime dateTime, Time time)
{
    // android.text.Time doesn't seem to store in millis precision, there is a 1000 multiplier used there internally
    // when calculating millis, so we can only compare in this precision:
    boolean millisMatch =
            dateTime.getTimestamp() / 1000
                    ==
                    time.toMillis(false) / 1000;

    boolean yearMatch = dateTime.getYear() == time.year;
    boolean monthMatch = dateTime.getMonth() == time.month;
    boolean dayMatch = dateTime.getDayOfMonth() == time.monthDay;
    boolean hourMatch = dateTime.getHours() == time.hour;
    boolean minuteMatch = dateTime.getMinutes() == time.minute;
    boolean secondsMatch = dateTime.getSeconds() == time.second;

    boolean allDaysMatch = time.allDay == dateTime.isAllDay();

    boolean timeZoneMatch =
            (dateTime.isFloating() && dateTime.isAllDay() && time.timezone.equals("UTC"))
                    ||
                    // This is the regular case with non-floating DateTime
                    (dateTime.getTimeZone() != null && time.timezone.equals(dateTime.getTimeZone().getID()));

    return millisMatch
            && yearMatch
            && monthMatch
            && dayMatch
            && hourMatch
            && minuteMatch
            && secondsMatch
            && allDaysMatch
            && timeZoneMatch;
}
 
Example 8
Source File: DateTimeArrayFieldAdapter.java    From opentasks-provider with Apache License 2.0 5 votes vote down vote up
@Override
public DateTime[] getFrom(ContentValues values)
{
	String datetimeList = values.getAsString(mDateTimeListFieldName);
	if (datetimeList == null)
	{
		// no list, return null
		return null;
	}

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

	String[] datetimes = SEPARATOR_PATTERN.split(datetimeList);

	DateTime[] result = new DateTime[datetimes.length];
	for (int i = 0, count = datetimes.length; i < count; ++i)
	{
		DateTime value = DateTime.parse(timeZone, datetimes[i]);

		if (!value.isAllDay() && value.isFloating())
		{
			throw new IllegalArgumentException("DateTime values must not be floating, unless they are all-day.");
		}

		result[i] = value;
		if (i > 0 && result[0].isAllDay() != value.isAllDay())
		{
			throw new IllegalArgumentException("DateTime values must all be of the same type.");
		}
	}

	return result;
}
 
Example 9
Source File: DateTimeArrayFieldAdapter.java    From opentasks-provider with Apache License 2.0 5 votes vote down vote up
@Override
public void setIn(ContentValues values, DateTime[] value)
{
	if (value != null && value.length > 0)
	{
		try
		{
			// Note: we only store the datetime strings, not the timezone
			StringBuilder result = new StringBuilder(value.length * 17 /* this is the maximum length */);

			boolean first = true;
			for (DateTime datetime : value)
			{
				if (first)
				{
					first = false;
				}
				else
				{
					result.append(',');
				}
				DateTime outvalue = datetime.isFloating() ? datetime : datetime.shiftTimeZone(DateTime.UTC);
				outvalue.writeTo(result);
			}
			values.put(mDateTimeListFieldName, result.toString());
		}
		catch (IOException e)
		{
			throw new RuntimeException("Can not serialize datetime list.");
		}

	}
	else
	{
		values.put(mDateTimeListFieldName, (Long) null);
	}
}
 
Example 10
Source File: DateTimeArrayFieldAdapter.java    From opentasks-provider with Apache License 2.0 4 votes vote down vote up
@Override
public 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 null
		return null;
	}

	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);

	String[] datetimes = SEPARATOR_PATTERN.split(datetimeList);

	DateTime[] result = new DateTime[datetimes.length];
	for (int i = 0, count = datetimes.length; i < count; ++i)
	{
		DateTime value = DateTime.parse(timeZone, datetimes[i]);

		if (!value.isAllDay() && value.isFloating())
		{
			throw new IllegalArgumentException("DateTime values must not be floating, unless they are all-day.");
		}

		result[i] = value;
		if (i > 0 && result[0].isAllDay() != value.isAllDay())
		{
			throw new IllegalArgumentException("DateTime values must all be of the same type.");
		}
	}

	return result;
}
 
Example 11
Source File: DateTimeArrayFieldAdapter.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;
	String datetimeList;
	String timeZoneId = null;

	if (values != null && values.containsKey(mDateTimeListFieldName))
	{
		if (values.getAsLong(mDateTimeListFieldName) == null)
		{
			// the date times are null, so we return null
			return null;
		}
		datetimeList = values.getAsString(mDateTimeListFieldName);
	}
	else if (cursor != null && (tsIdx = cursor.getColumnIndex(mDateTimeListFieldName)) >= 0)
	{
		if (cursor.isNull(tsIdx))
		{
			// the date times are null, so we return null
			return null;
		}
		datetimeList = cursor.getString(tsIdx);
	}
	else
	{
		throw new IllegalArgumentException("Missing date time list column.");
	}

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

	// create a new TimeZone for the given time zone string
	TimeZone timeZone = timeZoneId == null ? null : TimeZone.getTimeZone(timeZoneId);

	String[] datetimes = SEPARATOR_PATTERN.split(datetimeList);

	DateTime[] result = new DateTime[datetimes.length];
	for (int i = 0, count = datetimes.length; i < count; ++i)
	{
		DateTime value = DateTime.parse(timeZone, datetimes[i]);

		if (!value.isAllDay() && value.isFloating())
		{
			throw new IllegalArgumentException("DateTime values must not be floating, unless they are all-day.");
		}

		result[i] = value;
		if (i > 0 && result[0].isAllDay() != value.isAllDay())
		{
			throw new IllegalArgumentException("DateTime values must all be of the same type.");
		}
	}

	return result;
}