use of sun.util.calendar.BaseCalendar in project checker-framework by typetools.
the class Date method normalize.
// fastTime and the returned data are in sync upon return.
private final BaseCalendar.Date normalize(BaseCalendar.Date date) {
int y = date.getNormalizedYear();
int m = date.getMonth();
int d = date.getDayOfMonth();
int hh = date.getHours();
int mm = date.getMinutes();
int ss = date.getSeconds();
int ms = date.getMillis();
TimeZone tz = date.getZone();
// transition here.
if (y == 1582 || y > 280000000 || y < -280000000) {
if (tz == null) {
tz = TimeZone.getTimeZone("GMT");
}
GregorianCalendar gc = new GregorianCalendar(tz);
gc.clear();
gc.set(gc.MILLISECOND, ms);
gc.set(y, m - 1, d, hh, mm, ss);
fastTime = gc.getTimeInMillis();
BaseCalendar cal = getCalendarSystem(fastTime);
date = (BaseCalendar.Date) cal.getCalendarDate(fastTime, tz);
return date;
}
BaseCalendar cal = getCalendarSystem(y);
if (cal != getCalendarSystem(date)) {
date = (BaseCalendar.Date) cal.newCalendarDate(tz);
date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
}
// Perform the GregorianCalendar-style normalization.
fastTime = cal.getTime(date);
// In case the normalized date requires the other calendar
// system, we need to recalculate it using the other one.
BaseCalendar ncal = getCalendarSystem(fastTime);
if (ncal != cal) {
date = (BaseCalendar.Date) ncal.newCalendarDate(tz);
date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
fastTime = ncal.getTime(date);
}
return date;
}
use of sun.util.calendar.BaseCalendar in project checker-framework by typetools.
the class GregorianCalendar method getCalendarDate.
/**
* Returns a CalendarDate produced from the specified fixed date.
*
* @param fd the fixed date
*/
private final BaseCalendar.Date getCalendarDate(long fd) {
BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
cal.getCalendarDateFromFixedDate(d, fd);
return d;
}
use of sun.util.calendar.BaseCalendar in project checker-framework by typetools.
the class GregorianCalendar method getActualMaximum.
/**
* Returns the maximum value that this calendar field could have,
* taking into consideration the given time value and the current
* values of the
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
* {@link #getGregorianChange() getGregorianChange} and
* {@link Calendar#getTimeZone() getTimeZone} methods.
* For example, if the date of this instance is February 1, 2004,
* the actual maximum value of the <code>DAY_OF_MONTH</code> field
* is 29 because 2004 is a leap year, and if the date of this
* instance is February 1, 2005, it's 28.
*
* <p>This method calculates the maximum value of {@link
* Calendar#WEEK_OF_YEAR WEEK_OF_YEAR} based on the {@link
* Calendar#YEAR YEAR} (calendar year) value, not the <a
* href="#week_year">week year</a>. Call {@link
* #getWeeksInWeekYear()} to get the maximum value of {@code
* WEEK_OF_YEAR} in the week year of this {@code GregorianCalendar}.
*
* @param field the calendar field
* @return the maximum of the given field for the time value of
* this <code>GregorianCalendar</code>
* @see #getMinimum(int)
* @see #getMaximum(int)
* @see #getGreatestMinimum(int)
* @see #getLeastMaximum(int)
* @see #getActualMinimum(int)
* @since 1.2
*/
public int getActualMaximum(int field) {
final int fieldsForFixedMax = ERA_MASK | DAY_OF_WEEK_MASK | HOUR_MASK | AM_PM_MASK | HOUR_OF_DAY_MASK | MINUTE_MASK | SECOND_MASK | MILLISECOND_MASK | ZONE_OFFSET_MASK | DST_OFFSET_MASK;
if ((fieldsForFixedMax & (1 << field)) != 0) {
return getMaximum(field);
}
GregorianCalendar gc = getNormalizedCalendar();
BaseCalendar.Date date = gc.cdate;
BaseCalendar cal = gc.calsys;
int normalizedYear = date.getNormalizedYear();
int value = -1;
switch(field) {
case MONTH:
{
if (!gc.isCutoverYear(normalizedYear)) {
value = DECEMBER;
break;
}
// January 1 of the next year may or may not exist.
long nextJan1;
do {
nextJan1 = gcal.getFixedDate(++normalizedYear, BaseCalendar.JANUARY, 1, null);
} while (nextJan1 < gregorianCutoverDate);
BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
cal.getCalendarDateFromFixedDate(d, nextJan1 - 1);
value = d.getMonth() - 1;
}
break;
case DAY_OF_MONTH:
{
value = cal.getMonthLength(date);
if (!gc.isCutoverYear(normalizedYear) || date.getDayOfMonth() == value) {
break;
}
// Handle cutover year.
long fd = gc.getCurrentFixedDate();
if (fd >= gregorianCutoverDate) {
break;
}
int monthLength = gc.actualMonthLength();
long monthEnd = gc.getFixedDateMonth1(gc.cdate, fd) + monthLength - 1;
// Convert the fixed date to its calendar date.
BaseCalendar.Date d = gc.getCalendarDate(monthEnd);
value = d.getDayOfMonth();
}
break;
case DAY_OF_YEAR:
{
if (!gc.isCutoverYear(normalizedYear)) {
value = cal.getYearLength(date);
break;
}
// Handle cutover year.
long jan1;
if (gregorianCutoverYear == gregorianCutoverYearJulian) {
BaseCalendar cocal = gc.getCutoverCalendarSystem();
jan1 = cocal.getFixedDate(normalizedYear, 1, 1, null);
} else if (normalizedYear == gregorianCutoverYearJulian) {
jan1 = cal.getFixedDate(normalizedYear, 1, 1, null);
} else {
jan1 = gregorianCutoverDate;
}
// January 1 of the next year may or may not exist.
long nextJan1 = gcal.getFixedDate(++normalizedYear, 1, 1, null);
if (nextJan1 < gregorianCutoverDate) {
nextJan1 = gregorianCutoverDate;
}
assert jan1 <= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(), date.getDayOfMonth(), date);
assert nextJan1 >= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(), date.getDayOfMonth(), date);
value = (int) (nextJan1 - jan1);
}
break;
case WEEK_OF_YEAR:
{
if (!gc.isCutoverYear(normalizedYear)) {
// Get the day of week of January 1 of the year
CalendarDate d = cal.newCalendarDate(TimeZone.NO_TIMEZONE);
d.setDate(date.getYear(), BaseCalendar.JANUARY, 1);
int dayOfWeek = cal.getDayOfWeek(d);
// Normalize the day of week with the firstDayOfWeek value
dayOfWeek -= getFirstDayOfWeek();
if (dayOfWeek < 0) {
dayOfWeek += 7;
}
value = 52;
int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;
if ((magic == 6) || (date.isLeapYear() && (magic == 5 || magic == 12))) {
value++;
}
break;
}
if (gc == this) {
gc = (GregorianCalendar) gc.clone();
}
int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);
gc.set(DAY_OF_YEAR, maxDayOfYear);
value = gc.get(WEEK_OF_YEAR);
if (internalGet(YEAR) != gc.getWeekYear()) {
gc.set(DAY_OF_YEAR, maxDayOfYear - 7);
value = gc.get(WEEK_OF_YEAR);
}
}
break;
case WEEK_OF_MONTH:
{
if (!gc.isCutoverYear(normalizedYear)) {
CalendarDate d = cal.newCalendarDate(null);
d.setDate(date.getYear(), date.getMonth(), 1);
int dayOfWeek = cal.getDayOfWeek(d);
int monthLength = cal.getMonthLength(d);
dayOfWeek -= getFirstDayOfWeek();
if (dayOfWeek < 0) {
dayOfWeek += 7;
}
// # of days in the first week
int nDaysFirstWeek = 7 - dayOfWeek;
value = 3;
if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {
value++;
}
monthLength -= nDaysFirstWeek + 7 * 3;
if (monthLength > 0) {
value++;
if (monthLength > 7) {
value++;
}
}
break;
}
// Cutover year handling
if (gc == this) {
gc = (GregorianCalendar) gc.clone();
}
int y = gc.internalGet(YEAR);
int m = gc.internalGet(MONTH);
do {
value = gc.get(WEEK_OF_MONTH);
gc.add(WEEK_OF_MONTH, +1);
} while (gc.get(YEAR) == y && gc.get(MONTH) == m);
}
break;
case DAY_OF_WEEK_IN_MONTH:
{
// may be in the Gregorian cutover month
int ndays, dow1;
int dow = date.getDayOfWeek();
if (!gc.isCutoverYear(normalizedYear)) {
BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
ndays = cal.getMonthLength(d);
d.setDayOfMonth(1);
cal.normalize(d);
dow1 = d.getDayOfWeek();
} else {
// Let a cloned GregorianCalendar take care of the cutover cases.
if (gc == this) {
gc = (GregorianCalendar) clone();
}
ndays = gc.actualMonthLength();
gc.set(DAY_OF_MONTH, gc.getActualMinimum(DAY_OF_MONTH));
dow1 = gc.get(DAY_OF_WEEK);
}
int x = dow - dow1;
if (x < 0) {
x += 7;
}
ndays -= x;
value = (ndays + 6) / 7;
}
break;
case YEAR:
/* The year computation is no different, in principle, from the
* others, however, the range of possible maxima is large. In
* addition, the way we know we've exceeded the range is different.
* For these reasons, we use the special case code below to handle
* this field.
*
* The actual maxima for YEAR depend on the type of calendar:
*
* Gregorian = May 17, 292275056 BCE - Aug 17, 292278994 CE
* Julian = Dec 2, 292269055 BCE - Jan 3, 292272993 CE
* Hybrid = Dec 2, 292269055 BCE - Aug 17, 292278994 CE
*
* We know we've exceeded the maximum when either the month, date,
* time, or era changes in response to setting the year. We don't
* check for month, date, and time here because the year and era are
* sufficient to detect an invalid year setting. NOTE: If code is
* added to check the month and date in the future for some reason,
* Feb 29 must be allowed to shift to Mar 1 when setting the year.
*/
{
if (gc == this) {
gc = (GregorianCalendar) clone();
}
// Calculate the millisecond offset from the beginning
// of the year of this calendar and adjust the max
// year value if we are beyond the limit in the max
// year.
long current = gc.getYearOffsetInMillis();
if (gc.internalGetEra() == CE) {
gc.setTimeInMillis(Long.MAX_VALUE);
value = gc.get(YEAR);
long maxEnd = gc.getYearOffsetInMillis();
if (current > maxEnd) {
value--;
}
} else {
CalendarSystem mincal = gc.getTimeInMillis() >= gregorianCutover ? gcal : getJulianCalendarSystem();
CalendarDate d = mincal.getCalendarDate(Long.MIN_VALUE, getZone());
long maxEnd = (cal.getDayOfYear(d) - 1) * 24 + d.getHours();
maxEnd *= 60;
maxEnd += d.getMinutes();
maxEnd *= 60;
maxEnd += d.getSeconds();
maxEnd *= 1000;
maxEnd += d.getMillis();
value = d.getYear();
if (value <= 0) {
assert mincal == gcal;
value = 1 - value;
}
if (current < maxEnd) {
value--;
}
}
}
break;
default:
throw new ArrayIndexOutOfBoundsException(field);
}
return value;
}
use of sun.util.calendar.BaseCalendar in project Bytecoder by mirkosertic.
the class Date method normalize.
// fastTime and the returned data are in sync upon return.
private final BaseCalendar.Date normalize(BaseCalendar.Date date) {
int y = date.getNormalizedYear();
int m = date.getMonth();
int d = date.getDayOfMonth();
int hh = date.getHours();
int mm = date.getMinutes();
int ss = date.getSeconds();
int ms = date.getMillis();
TimeZone tz = date.getZone();
// transition here.
if (y == 1582 || y > 280000000 || y < -280000000) {
if (tz == null) {
tz = TimeZone.getTimeZone("GMT");
}
GregorianCalendar gc = new GregorianCalendar(tz);
gc.clear();
gc.set(GregorianCalendar.MILLISECOND, ms);
gc.set(y, m - 1, d, hh, mm, ss);
fastTime = gc.getTimeInMillis();
BaseCalendar cal = getCalendarSystem(fastTime);
date = (BaseCalendar.Date) cal.getCalendarDate(fastTime, tz);
return date;
}
BaseCalendar cal = getCalendarSystem(y);
if (cal != getCalendarSystem(date)) {
date = (BaseCalendar.Date) cal.newCalendarDate(tz);
date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
}
// Perform the GregorianCalendar-style normalization.
fastTime = cal.getTime(date);
// In case the normalized date requires the other calendar
// system, we need to recalculate it using the other one.
BaseCalendar ncal = getCalendarSystem(fastTime);
if (ncal != cal) {
date = (BaseCalendar.Date) ncal.newCalendarDate(tz);
date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
fastTime = ncal.getTime(date);
}
return date;
}
use of sun.util.calendar.BaseCalendar in project Bytecoder by mirkosertic.
the class Date method parse.
/**
* Attempts to interpret the string {@code s} as a representation
* of a date and time. If the attempt is successful, the time
* indicated is returned represented as the distance, measured in
* milliseconds, of that time from the epoch (00:00:00 GMT on
* January 1, 1970). If the attempt fails, an
* {@code IllegalArgumentException} is thrown.
* <p>
* It accepts many syntaxes; in particular, it recognizes the IETF
* standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It also
* understands the continental U.S. time-zone abbreviations, but for
* general use, a time-zone offset should be used: "Sat, 12 Aug 1995
* 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich
* meridian). If no time zone is specified, the local time zone is
* assumed. GMT and UTC are considered equivalent.
* <p>
* The string {@code s} is processed from left to right, looking for
* data of interest. Any material in {@code s} that is within the
* ASCII parenthesis characters {@code (} and {@code )} is ignored.
* Parentheses may be nested. Otherwise, the only characters permitted
* within {@code s} are these ASCII characters:
* <blockquote><pre>
* abcdefghijklmnopqrstuvwxyz
* ABCDEFGHIJKLMNOPQRSTUVWXYZ
* 0123456789,+-:/</pre></blockquote>
* and whitespace characters.<p>
* A consecutive sequence of decimal digits is treated as a decimal
* number:<ul>
* <li>If a number is preceded by {@code +} or {@code -} and a year
* has already been recognized, then the number is a time-zone
* offset. If the number is less than 24, it is an offset measured
* in hours. Otherwise, it is regarded as an offset in minutes,
* expressed in 24-hour time format without punctuation. A
* preceding {@code -} means a westward offset. Time zone offsets
* are always relative to UTC (Greenwich). Thus, for example,
* {@code -5} occurring in the string would mean "five hours west
* of Greenwich" and {@code +0430} would mean "four hours and
* thirty minutes east of Greenwich." It is permitted for the
* string to specify {@code GMT}, {@code UT}, or {@code UTC}
* redundantly-for example, {@code GMT-5} or {@code utc+0430}.
* <li>The number is regarded as a year number if one of the
* following conditions is true:
* <ul>
* <li>The number is equal to or greater than 70 and followed by a
* space, comma, slash, or end of string
* <li>The number is less than 70, and both a month and a day of
* the month have already been recognized</li>
* </ul>
* If the recognized year number is less than 100, it is
* interpreted as an abbreviated year relative to a century of
* which dates are within 80 years before and 19 years after
* the time when the Date class is initialized.
* After adjusting the year number, 1900 is subtracted from
* it. For example, if the current year is 1999 then years in
* the range 19 to 99 are assumed to mean 1919 to 1999, while
* years from 0 to 18 are assumed to mean 2000 to 2018. Note
* that this is slightly different from the interpretation of
* years less than 100 that is used in {@link java.text.SimpleDateFormat}.
* <li>If the number is followed by a colon, it is regarded as an hour,
* unless an hour has already been recognized, in which case it is
* regarded as a minute.
* <li>If the number is followed by a slash, it is regarded as a month
* (it is decreased by 1 to produce a number in the range {@code 0}
* to {@code 11}), unless a month has already been recognized, in
* which case it is regarded as a day of the month.
* <li>If the number is followed by whitespace, a comma, a hyphen, or
* end of string, then if an hour has been recognized but not a
* minute, it is regarded as a minute; otherwise, if a minute has
* been recognized but not a second, it is regarded as a second;
* otherwise, it is regarded as a day of the month. </ul><p>
* A consecutive sequence of letters is regarded as a word and treated
* as follows:<ul>
* <li>A word that matches {@code AM}, ignoring case, is ignored (but
* the parse fails if an hour has not been recognized or is less
* than {@code 1} or greater than {@code 12}).
* <li>A word that matches {@code PM}, ignoring case, adds {@code 12}
* to the hour (but the parse fails if an hour has not been
* recognized or is less than {@code 1} or greater than {@code 12}).
* <li>Any word that matches any prefix of {@code SUNDAY, MONDAY, TUESDAY,
* WEDNESDAY, THURSDAY, FRIDAY}, or {@code SATURDAY}, ignoring
* case, is ignored. For example, {@code sat, Friday, TUE}, and
* {@code Thurs} are ignored.
* <li>Otherwise, any word that matches any prefix of {@code JANUARY,
* FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,
* OCTOBER, NOVEMBER}, or {@code DECEMBER}, ignoring case, and
* considering them in the order given here, is recognized as
* specifying a month and is converted to a number ({@code 0} to
* {@code 11}). For example, {@code aug, Sept, april}, and
* {@code NOV} are recognized as months. So is {@code Ma}, which
* is recognized as {@code MARCH}, not {@code MAY}.
* <li>Any word that matches {@code GMT, UT}, or {@code UTC}, ignoring
* case, is treated as referring to UTC.
* <li>Any word that matches {@code EST, CST, MST}, or {@code PST},
* ignoring case, is recognized as referring to the time zone in
* North America that is five, six, seven, or eight hours west of
* Greenwich, respectively. Any word that matches {@code EDT, CDT,
* MDT}, or {@code PDT}, ignoring case, is recognized as
* referring to the same time zone, respectively, during daylight
* saving time.</ul><p>
* Once the entire string s has been scanned, it is converted to a time
* result in one of two ways. If a time zone or time-zone offset has been
* recognized, then the year, month, day of month, hour, minute, and
* second are interpreted in UTC and then the time-zone offset is
* applied. Otherwise, the year, month, day of month, hour, minute, and
* second are interpreted in the local time zone.
*
* @param s a string to be parsed as a date.
* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
* represented by the string argument.
* @see java.text.DateFormat
* @deprecated As of JDK version 1.1,
* replaced by {@code DateFormat.parse(String s)}.
*/
@Deprecated
public static long parse(String s) {
int year = Integer.MIN_VALUE;
int mon = -1;
int mday = -1;
int hour = -1;
int min = -1;
int sec = -1;
int millis = -1;
int c = -1;
int i = 0;
int n = -1;
int wst = -1;
int tzoffset = -1;
int prevc = 0;
syntax: {
if (s == null)
break syntax;
int limit = s.length();
while (i < limit) {
c = s.charAt(i);
i++;
if (c <= ' ' || c == ',')
continue;
if (c == '(') {
// skip comments
int depth = 1;
while (i < limit) {
c = s.charAt(i);
i++;
if (c == '(')
depth++;
else if (c == ')')
if (--depth <= 0)
break;
}
continue;
}
if ('0' <= c && c <= '9') {
n = c - '0';
while (i < limit && '0' <= (c = s.charAt(i)) && c <= '9') {
n = n * 10 + c - '0';
i++;
}
if (prevc == '+' || prevc == '-' && year != Integer.MIN_VALUE) {
// timezone offset
if (n < 24)
// EG. "GMT-3"
n = n * 60;
else
// eg "GMT-0430"
n = n % 100 + n / 100 * 60;
if (// plus means east of GMT
prevc == '+')
n = -n;
if (tzoffset != 0 && tzoffset != -1)
break syntax;
tzoffset = n;
} else if (n >= 70)
if (year != Integer.MIN_VALUE)
break syntax;
else if (c <= ' ' || c == ',' || c == '/' || i >= limit)
// year = n < 1900 ? n : n - 1900;
year = n;
else
break syntax;
else if (c == ':')
if (hour < 0)
hour = (byte) n;
else if (min < 0)
min = (byte) n;
else
break syntax;
else if (c == '/')
if (mon < 0)
mon = (byte) (n - 1);
else if (mday < 0)
mday = (byte) n;
else
break syntax;
else if (i < limit && c != ',' && c > ' ' && c != '-')
break syntax;
else if (hour >= 0 && min < 0)
min = (byte) n;
else if (min >= 0 && sec < 0)
sec = (byte) n;
else if (mday < 0)
mday = (byte) n;
else // Handle two-digit years < 70 (70-99 handled above).
if (year == Integer.MIN_VALUE && mon >= 0 && mday >= 0)
year = n;
else
break syntax;
prevc = 0;
} else if (c == '/' || c == ':' || c == '+' || c == '-')
prevc = c;
else {
int st = i - 1;
while (i < limit) {
c = s.charAt(i);
if (!('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'))
break;
i++;
}
if (i <= st + 1)
break syntax;
int k;
for (k = wtb.length; --k >= 0; ) if (wtb[k].regionMatches(true, 0, s, st, i - st)) {
int action = ttb[k];
if (action != 0) {
if (action == 1) {
// pm
if (hour > 12 || hour < 1)
break syntax;
else if (hour < 12)
hour += 12;
} else if (action == 14) {
// am
if (hour > 12 || hour < 1)
break syntax;
else if (hour == 12)
hour = 0;
} else if (action <= 13) {
// month!
if (mon < 0)
mon = (byte) (action - 2);
else
break syntax;
} else {
tzoffset = action - 10000;
}
}
break;
}
if (k < 0)
break syntax;
prevc = 0;
}
}
if (year == Integer.MIN_VALUE || mon < 0 || mday < 0)
break syntax;
// Parse 2-digit years within the correct default century.
if (year < 100) {
synchronized (Date.class) {
if (defaultCenturyStart == 0) {
defaultCenturyStart = gcal.getCalendarDate().getYear() - 80;
}
}
year += (defaultCenturyStart / 100) * 100;
if (year < defaultCenturyStart)
year += 100;
}
if (sec < 0)
sec = 0;
if (min < 0)
min = 0;
if (hour < 0)
hour = 0;
BaseCalendar cal = getCalendarSystem(year);
if (tzoffset == -1) {
// no time zone specified, have to use local
BaseCalendar.Date ldate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());
ldate.setDate(year, mon + 1, mday);
ldate.setTimeOfDay(hour, min, sec, 0);
return cal.getTime(ldate);
}
// no time zone
BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null);
udate.setDate(year, mon + 1, mday);
udate.setTimeOfDay(hour, min, sec, 0);
return cal.getTime(udate) + tzoffset * (60 * 1000);
}
// syntax error
throw new IllegalArgumentException();
}
Aggregations