Search in sources :

Example 6 with TimeFormatException

use of android.util.TimeFormatException in project android-betterpickers by code-troopers.

the class RecurrencePickerDialogFragment method copyEventRecurrenceToModel.

// TODO don't lose data when getting data that our UI can't handle
private static void copyEventRecurrenceToModel(final EventRecurrence er, RecurrenceModel model) {
    // Freq:
    switch(er.freq) {
        case EventRecurrence.HOURLY:
            model.freq = RecurrenceModel.FREQ_HOURLY;
            break;
        case EventRecurrence.DAILY:
            model.freq = RecurrenceModel.FREQ_DAILY;
            break;
        case EventRecurrence.MONTHLY:
            model.freq = RecurrenceModel.FREQ_MONTHLY;
            break;
        case EventRecurrence.YEARLY:
            model.freq = RecurrenceModel.FREQ_YEARLY;
            break;
        case EventRecurrence.WEEKLY:
            model.freq = RecurrenceModel.FREQ_WEEKLY;
            break;
        default:
            throw new IllegalStateException("freq=" + er.freq);
    }
    // Interval:
    if (er.interval > 0) {
        model.interval = er.interval;
    }
    // End:
    // End by count:
    model.endCount = er.count;
    if (model.endCount > 0) {
        model.end = RecurrenceModel.END_BY_COUNT;
    }
    // End by date:
    if (!TextUtils.isEmpty(er.until)) {
        if (model.endDate == null) {
            model.endDate = new Time();
        }
        try {
            model.endDate.parse(er.until);
        } catch (TimeFormatException e) {
            model.endDate = null;
        }
        // LIMITATION: The UI can only handle END_BY_DATE or END_BY_COUNT
        if (model.end == RecurrenceModel.END_BY_COUNT && model.endDate != null) {
            throw new IllegalStateException("freq=" + er.freq);
        }
        model.end = RecurrenceModel.END_BY_DATE;
    }
    // Weekly: repeat by day of week or Monthly: repeat by nth day of week
    // in the month
    Arrays.fill(model.weeklyByDayOfWeek, false);
    if (er.bydayCount > 0) {
        int count = 0;
        for (int i = 0; i < er.bydayCount; i++) {
            int dayOfWeek = EventRecurrence.day2TimeDay(er.byday[i]);
            model.weeklyByDayOfWeek[dayOfWeek] = true;
            if (model.freq == RecurrenceModel.FREQ_MONTHLY && isSupportedMonthlyByNthDayOfWeek(er.bydayNum[i])) {
                // LIMITATION: Can handle only (one) weekDayNum in nth or last and only
                // when
                // monthly
                model.monthlyByDayOfWeek = dayOfWeek;
                model.monthlyByNthDayOfWeek = er.bydayNum[i];
                model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK;
                count++;
            }
        }
        if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
            if (er.bydayCount != 1) {
                // Can't handle 1st Monday and 2nd Wed
                throw new IllegalStateException("Can handle only 1 byDayOfWeek in monthly");
            }
            if (count != 1) {
                throw new IllegalStateException("Didn't specify which nth day of week to repeat for a monthly");
            }
        }
    }
    // Monthly by day of month
    if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
        if (er.bymonthdayCount == 1) {
            if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
                throw new IllegalStateException("Can handle only by monthday or by nth day of week, not both");
            }
            model.monthlyByMonthDay = er.bymonthday[0];
            model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_DATE;
        } else if (er.bymonthCount > 1) {
            // LIMITATION: Can handle only one month day
            throw new IllegalStateException("Can handle only one bymonthday");
        }
    }
}
Also used : Time(android.text.format.Time) TimeFormatException(android.util.TimeFormatException)

Example 7 with TimeFormatException

use of android.util.TimeFormatException in project SublimePicker by vikramkakkar.

the class EventRecurrenceFormatter method getRepeatString.

public static String getRepeatString(Context context, Resources r, EventRecurrence recurrence, boolean includeEndString) {
    String endString = "";
    if (includeEndString) {
        StringBuilder sb = new StringBuilder();
        if (recurrence.until != null) {
            try {
                Time t = new Time();
                t.parse(recurrence.until);
                final String dateStr = DateUtils.formatDateTime(context, t.toMillis(false), DateUtils.FORMAT_NUMERIC_DATE);
                sb.append(r.getString(R.string.endByDate, dateStr));
            } catch (TimeFormatException e) {
            }
        }
        if (recurrence.count > 0) {
            sb.append(r.getQuantityString(R.plurals.endByCount, recurrence.count, recurrence.count));
        }
        endString = sb.toString();
    }
    int interval = recurrence.interval <= 1 ? 1 : recurrence.interval;
    switch(recurrence.freq) {
        case EventRecurrence.DAILY:
            return r.getQuantityString(R.plurals.daily, interval, interval) + endString;
        case EventRecurrence.WEEKLY:
            {
                if (recurrence.repeatsOnEveryWeekDay()) {
                    return r.getString(R.string.every_weekday) + endString;
                } else {
                    String string;
                    int dayOfWeekLength = DateUtils.LENGTH_MEDIUM;
                    if (recurrence.bydayCount == 1) {
                        dayOfWeekLength = DateUtils.LENGTH_LONG;
                    }
                    StringBuilder days = new StringBuilder();
                    if (recurrence.bydayCount > 0) {
                        int count = recurrence.bydayCount - 1;
                        for (int i = 0; i < count; i++) {
                            days.append(dayToString(recurrence.byday[i], dayOfWeekLength));
                            days.append(", ");
                        }
                        days.append(dayToString(recurrence.byday[count], dayOfWeekLength));
                        string = days.toString();
                    } else {
                        // date of the first event in the recurrence.
                        if (recurrence.startDate == null) {
                            return null;
                        }
                        int day = EventRecurrence.timeDay2Day(recurrence.startDate.weekDay);
                        string = dayToString(day, DateUtils.LENGTH_LONG);
                    }
                    return r.getQuantityString(R.plurals.weekly, interval, interval, string) + endString;
                }
            }
        case EventRecurrence.MONTHLY:
            {
                String monthlyStart = interval == 1 ? r.getString(R.string.monthly) : r.getQuantityString(R.plurals.recurrence_interval_monthly, interval, interval);
                if (recurrence.bydayCount == 1) {
                    int weekday = recurrence.startDate.weekDay;
                    // Cache this stuff so we won't have to redo work again later.
                    cacheMonthRepeatStrings(r, weekday);
                    int dayNumber = (recurrence.startDate.monthDay - 1) / 7;
                    StringBuilder sb = new StringBuilder();
                    sb.append(monthlyStart);
                    sb.append(" (");
                    sb.append(mMonthRepeatByDayOfWeekStrs[weekday][dayNumber]);
                    sb.append(")");
                    sb.append(endString);
                    return sb.toString();
                }
                return monthlyStart + endString;
            }
        case EventRecurrence.YEARLY:
            String yearlyStart = interval == 1 ? r.getString(R.string.yearly_plain) : r.getQuantityString(R.plurals.recurrence_interval_yearly, interval, interval);
            return yearlyStart + endString;
    }
    return null;
}
Also used : Time(android.text.format.Time) TimeFormatException(android.util.TimeFormatException)

Example 8 with TimeFormatException

use of android.util.TimeFormatException in project SublimePicker by vikramkakkar.

the class RecurrenceOptionCreator method copyEventRecurrenceToModel.

// TODO don't lose data when getting data that our UI can't handle
private static void copyEventRecurrenceToModel(final EventRecurrence er, RecurrenceModel model) {
    // Freq:
    switch(er.freq) {
        case EventRecurrence.DAILY:
            model.freq = RecurrenceModel.FREQ_DAILY;
            break;
        case EventRecurrence.MONTHLY:
            model.freq = RecurrenceModel.FREQ_MONTHLY;
            break;
        case EventRecurrence.YEARLY:
            model.freq = RecurrenceModel.FREQ_YEARLY;
            break;
        case EventRecurrence.WEEKLY:
            model.freq = RecurrenceModel.FREQ_WEEKLY;
            break;
        default:
            throw new IllegalStateException("freq=" + er.freq);
    }
    // Interval:
    if (er.interval > 0) {
        model.interval = er.interval;
    }
    // End:
    // End by count:
    model.endCount = er.count;
    if (model.endCount > 0) {
        model.end = RecurrenceModel.END_BY_COUNT;
    }
    // End by date:
    if (!TextUtils.isEmpty(er.until)) {
        if (model.endDate == null) {
            model.endDate = new Time();
        }
        try {
            model.endDate.parse(er.until);
        } catch (TimeFormatException e) {
            model.endDate = null;
        }
        // LIMITATION: The UI can only handle END_BY_DATE or END_BY_COUNT
        if (model.end == RecurrenceModel.END_BY_COUNT && model.endDate != null) {
            throw new IllegalStateException("freq=" + er.freq);
        }
        model.end = RecurrenceModel.END_BY_DATE;
    }
    // Weekly: repeat by day of week or Monthly: repeat by nth day of week
    // in the month
    Arrays.fill(model.weeklyByDayOfWeek, false);
    if (er.bydayCount > 0) {
        int count = 0;
        for (int i = 0; i < er.bydayCount; i++) {
            int dayOfWeek = EventRecurrence.day2TimeDay(er.byday[i]);
            model.weeklyByDayOfWeek[dayOfWeek] = true;
            if (model.freq == RecurrenceModel.FREQ_MONTHLY && isSupportedMonthlyByNthDayOfWeek(er.bydayNum[i])) {
                // LIMITATION: Can handle only (one) weekDayNum in nth or last and only
                // when
                // monthly
                model.monthlyByDayOfWeek = dayOfWeek;
                model.monthlyByNthDayOfWeek = er.bydayNum[i];
                model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK;
                count++;
            }
        }
        if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
            if (er.bydayCount != 1) {
                // Can't handle 1st Monday and 2nd Wed
                throw new IllegalStateException("Can handle only 1 byDayOfWeek in monthly");
            }
            if (count != 1) {
                throw new IllegalStateException("Didn't specify which nth day of week to repeat for a monthly");
            }
        }
    }
    // Monthly by day of month
    if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
        if (er.bymonthdayCount == 1) {
            if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
                throw new IllegalStateException("Can handle only by monthday or by nth day of week, not both");
            }
            model.monthlyByMonthDay = er.bymonthday[0];
            model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_DATE;
        } else if (er.bymonthCount > 1) {
            // LIMITATION: Can handle only one month day
            throw new IllegalStateException("Can handle only one bymonthday");
        }
    }
}
Also used : Time(android.text.format.Time) TimeFormatException(android.util.TimeFormatException)

Example 9 with TimeFormatException

use of android.util.TimeFormatException in project platform_frameworks_base by android.

the class TimeTest method testParse33390.

@SmallTest
public void testParse33390() throws Exception {
    Time t = new Time(Time.TIMEZONE_UTC);
    t.parse3339("1980-05-23");
    if (!t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23) {
        fail("Did not parse all-day date correctly");
    }
    t.parse3339("1980-05-23T09:50:50");
    if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
        fail("Did not parse timezone-offset-less date correctly");
    }
    t.parse3339("1980-05-23T09:50:50Z");
    if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
        fail("Did not parse UTC date correctly");
    }
    t.parse3339("1980-05-23T09:50:50.0Z");
    if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
        fail("Did not parse UTC date correctly");
    }
    t.parse3339("1980-05-23T09:50:50.12Z");
    if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
        fail("Did not parse UTC date correctly");
    }
    t.parse3339("1980-05-23T09:50:50.123Z");
    if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
        fail("Did not parse UTC date correctly");
    }
    // The time should be normalized to UTC
    t.parse3339("1980-05-23T09:50:50-01:05");
    if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 10 || t.minute != 55 || t.second != 50 || t.gmtoff != 0) {
        fail("Did not parse timezone-offset date correctly");
    }
    // The time should be normalized to UTC
    t.parse3339("1980-05-23T09:50:50.123-01:05");
    if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 10 || t.minute != 55 || t.second != 50 || t.gmtoff != 0) {
        fail("Did not parse timezone-offset date correctly");
    }
    try {
        t.parse3339("1980");
        fail("Did not throw error on truncated input length");
    } catch (TimeFormatException e) {
    // Successful
    }
    try {
        t.parse3339("1980-05-23T09:50:50.123+");
        fail("Did not throw error on truncated timezone offset");
    } catch (TimeFormatException e1) {
    // Successful
    }
    try {
        t.parse3339("1980-05-23T09:50:50.123+05:0");
        fail("Did not throw error on truncated timezone offset");
    } catch (TimeFormatException e1) {
    // Successful
    }
}
Also used : Time(android.text.format.Time) TimeFormatException(android.util.TimeFormatException) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 10 with TimeFormatException

use of android.util.TimeFormatException in project Etar-Calendar by Etar-Group.

the class EventRecurrenceFormatter method getRepeatString.

public static String getRepeatString(Context context, Resources r, EventRecurrence recurrence, boolean includeEndString) {
    String endString = "";
    if (includeEndString) {
        StringBuilder sb = new StringBuilder();
        if (recurrence.until != null) {
            try {
                Time t = new Time();
                t.parse(recurrence.until);
                final String dateStr = DateUtils.formatDateTime(context, t.toMillis(false), DateUtils.FORMAT_NUMERIC_DATE);
                sb.append(r.getString(R.string.endByDate, dateStr));
            } catch (TimeFormatException e) {
            }
        }
        if (recurrence.count > 0) {
            sb.append(r.getQuantityString(R.plurals.endByCount, recurrence.count, recurrence.count));
        }
        endString = sb.toString();
    }
    // TODO Implement "Until" portion of string, as well as custom settings
    int interval = recurrence.interval <= 1 ? 1 : recurrence.interval;
    switch(recurrence.freq) {
        case EventRecurrence.DAILY:
            return r.getQuantityString(R.plurals.daily, interval, interval) + endString;
        case EventRecurrence.WEEKLY:
            {
                if (recurrence.repeatsOnEveryWeekDay()) {
                    return r.getString(R.string.every_weekday) + endString;
                } else {
                    String string;
                    int dayOfWeekLength = DateUtils.LENGTH_MEDIUM;
                    if (recurrence.bydayCount == 1) {
                        dayOfWeekLength = DateUtils.LENGTH_LONG;
                    }
                    StringBuilder days = new StringBuilder();
                    if (recurrence.bydayCount > 0) {
                        int count = recurrence.bydayCount - 1;
                        for (int i = 0; i < count; i++) {
                            days.append(dayToString(recurrence.byday[i], dayOfWeekLength));
                            days.append(", ");
                        }
                        days.append(dayToString(recurrence.byday[count], dayOfWeekLength));
                        string = days.toString();
                    } else {
                        // date of the first event in the recurrence.
                        if (recurrence.startDate == null) {
                            return null;
                        }
                        int day = EventRecurrence.timeDay2Day(recurrence.startDate.weekDay);
                        string = dayToString(day, DateUtils.LENGTH_LONG);
                    }
                    return r.getQuantityString(R.plurals.weekly, interval, interval, string) + endString;
                }
            }
        case EventRecurrence.MONTHLY:
            {
                if (recurrence.bydayCount == 1) {
                    int weekday = recurrence.startDate.weekDay;
                    // Cache this stuff so we won't have to redo work again later.
                    cacheMonthRepeatStrings(r, weekday);
                    int dayNumber = (recurrence.startDate.monthDay - 1) / 7;
                    StringBuilder sb = new StringBuilder();
                    sb.append(r.getString(R.string.monthly));
                    sb.append(" (");
                    sb.append(mMonthRepeatByDayOfWeekStrs[weekday][dayNumber]);
                    sb.append(")");
                    sb.append(endString);
                    return sb.toString();
                }
                return r.getString(R.string.monthly) + endString;
            }
        case EventRecurrence.YEARLY:
            return r.getString(R.string.yearly_plain) + endString;
    }
    return null;
}
Also used : Time(android.text.format.Time) TimeFormatException(android.util.TimeFormatException)

Aggregations

Time (android.text.format.Time)14 TimeFormatException (android.util.TimeFormatException)14 SmallTest (android.test.suitebuilder.annotation.SmallTest)6 Cursor (android.database.Cursor)1 ArrayList (java.util.ArrayList)1