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

The following examples show how to use org.dmfs.rfc5545.DateTime#isAllDay() . 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: 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 2
Source File: NotifyAction.java    From opentasks with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a string representation for the time, with a relative date and an absolute time
 */
public static String formatTime(Context context, DateTime time)
{
    String dateString;
    if (time.isAllDay())
    {
        dateString = DateUtils.getRelativeTimeSpanString(time.getTimestamp(), DateTime.today().getTimestamp(), DateUtils.DAY_IN_MILLIS).toString();
    }
    else
    {
        dateString = DateUtils.getRelativeTimeSpanString(time.getTimestamp(), DateTime.now().getTimestamp(), DateUtils.DAY_IN_MILLIS).toString();
    }

    // return combined date and time
    String timeString = new DateFormatter(context).format(time, DateFormatter.DateFormatContext.NOTIFICATION_VIEW_TIME);
    return dateString + ", " + timeString;
}
 
Example 3
Source File: RecurrenceRule.java    From lib-recur with Apache License 2.0 5 votes vote down vote up
/**
 * Get a new {@link RuleIterator} that iterates all instances of this rule. <p> <strong>Note:</strong> If the rule contains an UNTIL part with a floating
 * value, you have to provide <code>null</code> as the timezone. </p>
 *
 * @param start
 *         The time of the first instance in milliseconds since the epoch.
 * @param timezone
 *         The {@link TimeZone} of the first instance or <code>null</code> for floating times.
 *
 * @return A {@link RecurrenceRuleIterator}.
 */
public RecurrenceRuleIterator iterator(long start, TimeZone timezone)
{
    // TODO: avoid creating a temporary DATETIME instance.
    DateTime dt = new DateTime(mCalendarMetrics, timezone, start);
    DateTime until = getUntil();
    if (until != null && until.isAllDay())
    {
        dt = dt.toAllDay();
    }
    return iterator(dt);
}
 
Example 4
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 5
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 6
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 7
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 8
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 9
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;
}