use of sun.util.calendar.CalendarDate in project j2objc by google.
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.CalendarDate in project jdk8u_jdk by JetBrains.
the class ZoneInfoOld method getOffset.
/**
* Returns the difference in milliseconds between local time and
* UTC, taking into account both the raw offset and the effect of
* daylight savings, for the specified date and time. This method
* assumes that the start and end month are distinct. This method
* assumes a Gregorian calendar for calculations.
* <p>
* <em>Note: In general, clients should use
* {@link Calendar#ZONE_OFFSET Calendar.get(ZONE_OFFSET)} +
* {@link Calendar#DST_OFFSET Calendar.get(DST_OFFSET)}
* instead of calling this method.</em>
*
* @param era The era of the given date. The value must be either
* GregorianCalendar.AD or GregorianCalendar.BC.
* @param year The year in the given date.
* @param month The month in the given date. Month is 0-based. e.g.,
* 0 for January.
* @param day The day-in-month of the given date.
* @param dayOfWeek The day-of-week of the given date.
* @param millis The milliseconds in day in <em>standard</em> local time.
* @return The milliseconds to add to UTC to get local time.
*/
public int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) {
if (milliseconds < 0 || milliseconds >= DAY_IN_MILLIS) {
throw new IllegalArgumentException();
}
if (era == java.util.GregorianCalendar.BC) {
// BC
year = 1 - year;
} else if (era != java.util.GregorianCalendar.AD) {
throw new IllegalArgumentException();
}
CalendarDate date = gcal.newCalendarDate(null);
date.setDate(year, month + 1, day);
if (gcal.validate(date) == false) {
throw new IllegalArgumentException();
}
// bug-for-bug compatible argument checking
if (dayOfWeek < java.util.GregorianCalendar.SUNDAY || dayOfWeek > java.util.GregorianCalendar.SATURDAY) {
throw new IllegalArgumentException();
}
if (transitions == null) {
return getLastRawOffset();
}
long dateInMillis = gcal.getTime(date) + milliseconds;
// make it UTC
dateInMillis -= (long) rawOffset;
return getOffsets(dateInMillis, null, UTC_TIME);
}
use of sun.util.calendar.CalendarDate in project jdk8u_jdk by JetBrains.
the class Time method getLocalTime.
/**
* Converts the given Gregorian calendar field values to local time.
* Local time is represented by the amount of milliseconds from
* January 1, 1970 0:00 GMT.
* @param year the year value
* @param month the Month value
* @param day the day represented by {@link RuleDay}
* @param time the time of the day represented by {@link Time}
* @return local time
*/
static long getLocalTime(int year, Month month, RuleDay day, int time) {
CalendarDate cdate = gcal.newCalendarDate(null);
int monthValue = month.value();
if (day.isLast()) {
// e.g., "lastSun"
cdate.setDate(year, monthValue, 1);
cdate.setDayOfMonth(gcal.getMonthLength(cdate));
cdate = gcal.getNthDayOfWeek(-1, day.getDayOfWeekNum(), cdate);
} else if (day.isLater()) {
// e.g., "Sun>=1"
cdate.setDate(year, monthValue, day.getDay());
cdate = gcal.getNthDayOfWeek(1, day.getDayOfWeekNum(), cdate);
} else if (day.isExact()) {
cdate.setDate(year, monthValue, day.getDay());
} else if (day.isEarlier()) {
// e.g., "Sun<=15"
cdate.setDate(year, monthValue, day.getDay());
cdate = gcal.getNthDayOfWeek(-1, day.getDayOfWeekNum(), cdate);
} else {
Main.panic("invalid day type: " + day);
}
return gcal.getTime(cdate) + time;
}
use of sun.util.calendar.CalendarDate in project jdk8u_jdk by JetBrains.
the class DerInputBuffer method getTime.
/**
* Private helper routine to extract time from the der value.
* @param len the number of bytes to use
* @param generalized true if Generalized Time is to be read, false
* if UTC Time is to be read.
*/
private Date getTime(int len, boolean generalized) throws IOException {
/*
* UTC time encoded as ASCII chars:
* YYMMDDhhmmZ
* YYMMDDhhmmssZ
* YYMMDDhhmm+hhmm
* YYMMDDhhmm-hhmm
* YYMMDDhhmmss+hhmm
* YYMMDDhhmmss-hhmm
* UTC Time is broken in storing only two digits of year.
* If YY < 50, we assume 20YY;
* if YY >= 50, we assume 19YY, as per RFC 3280.
*
* Generalized time has a four-digit year and allows any
* precision specified in ISO 8601. However, for our purposes,
* we will only allow the same format as UTC time, except that
* fractional seconds (millisecond precision) are supported.
*/
int year, month, day, hour, minute, second, millis;
String type = null;
if (generalized) {
type = "Generalized";
year = 1000 * Character.digit((char) buf[pos++], 10);
year += 100 * Character.digit((char) buf[pos++], 10);
year += 10 * Character.digit((char) buf[pos++], 10);
year += Character.digit((char) buf[pos++], 10);
// For the two extra YY
len -= 2;
} else {
type = "UTC";
year = 10 * Character.digit((char) buf[pos++], 10);
year += Character.digit((char) buf[pos++], 10);
if (// origin 2000
year < 50)
year += 2000;
else
// origin 1900
year += 1900;
}
month = 10 * Character.digit((char) buf[pos++], 10);
month += Character.digit((char) buf[pos++], 10);
day = 10 * Character.digit((char) buf[pos++], 10);
day += Character.digit((char) buf[pos++], 10);
hour = 10 * Character.digit((char) buf[pos++], 10);
hour += Character.digit((char) buf[pos++], 10);
minute = 10 * Character.digit((char) buf[pos++], 10);
minute += Character.digit((char) buf[pos++], 10);
// YYMMDDhhmm
len -= 10;
/*
* We allow for non-encoded seconds, even though the
* IETF-PKIX specification says that the seconds should
* always be encoded even if it is zero.
*/
millis = 0;
if (len > 2 && len < 12) {
second = 10 * Character.digit((char) buf[pos++], 10);
second += Character.digit((char) buf[pos++], 10);
len -= 2;
// handle fractional seconds (if present)
if (buf[pos] == '.' || buf[pos] == ',') {
len--;
pos++;
// handle upto milisecond precision only
int precision = 0;
int peek = pos;
while (buf[peek] != 'Z' && buf[peek] != '+' && buf[peek] != '-') {
peek++;
precision++;
}
switch(precision) {
case 3:
millis += 100 * Character.digit((char) buf[pos++], 10);
millis += 10 * Character.digit((char) buf[pos++], 10);
millis += Character.digit((char) buf[pos++], 10);
break;
case 2:
millis += 100 * Character.digit((char) buf[pos++], 10);
millis += 10 * Character.digit((char) buf[pos++], 10);
break;
case 1:
millis += 100 * Character.digit((char) buf[pos++], 10);
break;
default:
throw new IOException("Parse " + type + " time, unsupported precision for seconds value");
}
len -= precision;
}
} else
second = 0;
if (month == 0 || day == 0 || month > 12 || day > 31 || hour >= 24 || minute >= 60 || second >= 60)
throw new IOException("Parse " + type + " time, invalid format");
/*
* Generalized time can theoretically allow any precision,
* but we're not supporting that.
*/
CalendarSystem gcal = CalendarSystem.getGregorianCalendar();
// no time zone
CalendarDate date = gcal.newCalendarDate(null);
date.setDate(year, month, day);
date.setTimeOfDay(hour, minute, second, millis);
long time = gcal.getTime(date);
/*
* Finally, "Z" or "+hhmm" or "-hhmm" ... offsets change hhmm
*/
if (!(len == 1 || len == 5))
throw new IOException("Parse " + type + " time, invalid offset");
int hr, min;
switch(buf[pos++]) {
case '+':
hr = 10 * Character.digit((char) buf[pos++], 10);
hr += Character.digit((char) buf[pos++], 10);
min = 10 * Character.digit((char) buf[pos++], 10);
min += Character.digit((char) buf[pos++], 10);
if (hr >= 24 || min >= 60)
throw new IOException("Parse " + type + " time, +hhmm");
time -= ((hr * 60) + min) * 60 * 1000;
break;
case '-':
hr = 10 * Character.digit((char) buf[pos++], 10);
hr += Character.digit((char) buf[pos++], 10);
min = 10 * Character.digit((char) buf[pos++], 10);
min += Character.digit((char) buf[pos++], 10);
if (hr >= 24 || min >= 60)
throw new IOException("Parse " + type + " time, -hhmm");
time += ((hr * 60) + min) * 60 * 1000;
break;
case 'Z':
break;
default:
throw new IOException("Parse " + type + " time, garbage offset");
}
return new Date(time);
}
use of sun.util.calendar.CalendarDate in project jdk8u_jdk by JetBrains.
the class JapaneseDate method ofYearDay.
/**
* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
* system from the era, year-of-era and day-of-year fields.
* <p>
* This returns a {@code JapaneseDate} with the specified fields.
* The day must be valid for the year, otherwise an exception will be thrown.
* <p>
* The day-of-year in this factory is expressed relative to the start of the year-of-era.
* This definition changes the normal meaning of day-of-year only in those years
* where the year-of-era is reset to one due to a change in the era.
* For example:
* <pre>
* 6th Jan Showa 64 = day-of-year 6
* 7th Jan Showa 64 = day-of-year 7
* 8th Jan Heisei 1 = day-of-year 1
* 9th Jan Heisei 1 = day-of-year 2
* </pre>
*
* @param era the Japanese era, not null
* @param yearOfEra the Japanese year-of-era
* @param dayOfYear the chronology day-of-year, from 1 to 366
* @return the date in Japanese calendar system, not null
* @throws DateTimeException if the value of any field is out of range,
* or if the day-of-year is invalid for the year
*/
static JapaneseDate ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear) {
Objects.requireNonNull(era, "era");
CalendarDate firstDay = era.getPrivateEra().getSinceDate();
LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
jdate.setEra(era.getPrivateEra());
if (yearOfEra == 1) {
jdate.setDate(yearOfEra, firstDay.getMonth(), firstDay.getDayOfMonth() + dayOfYear - 1);
} else {
jdate.setDate(yearOfEra, 1, dayOfYear);
}
JapaneseChronology.JCAL.normalize(jdate);
if (era.getPrivateEra() != jdate.getEra() || yearOfEra != jdate.getYear()) {
throw new DateTimeException("Invalid parameters");
}
LocalDate localdate = LocalDate.of(jdate.getNormalizedYear(), jdate.getMonth(), jdate.getDayOfMonth());
return new JapaneseDate(era, yearOfEra, localdate);
}
Aggregations