www.pudn.com > j2me_cldc-1_1-fcs-src-winunix.rar > CalendarImpl.java
/*
* Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.sun.cldc.util.j2me;
import java.util.*;
import java.io.*;
/**
* This class is an implementation of the subsetted
* CLDC 1.1 Calendar class.
*
* @author Kinsley Wong
* @see java.util.Calendar
* @see java.util.TimeZone
*/
public class CalendarImpl extends Calendar {
/* ERA */
private static final int BC = 0;
private static final int AD = 1;
/* January 1, year 1 (Gregorian) */
private static final int JAN_1_1_JULIAN_DAY = 1721426;
/* January 1, 1970 (Gregorian) */
private static final int EPOCH_JULIAN_DAY = 2440588;
/* 0-based, for day-in-year */
private static final int NUM_DAYS[]
= {0,31,59,90,120,151,181,212,243,273,304,334};
/* 0-based, for day-in-year */
private static final int LEAP_NUM_DAYS[]
= {0,31,60,91,121,152,182,213,244,274,305,335};
/**
* Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit
* into ints, they must be longs in order to prevent arithmetic overflow
* when performing (bug 4173516).
*/
private static final int ONE_SECOND = 1000;
private static final int ONE_MINUTE = 60*ONE_SECOND;
private static final int ONE_HOUR = 60*ONE_MINUTE;
private static final long ONE_DAY = 24*ONE_HOUR;
private static final long ONE_WEEK = 7*ONE_DAY;
/**
* The point at which the Gregorian calendar rules are used, measured in
* milliseconds from the standard epoch. Default is October 15, 1582
* (Gregorian) 00:00:00 UTC or -12219292800000L. For this value, October 4,
* 1582 (Julian) is followed by October 15, 1582 (Gregorian). This
* corresponds to Julian day number 2299161.
*/
private static final long gregorianCutover = -12219292800000L;
/**
* The year of the gregorianCutover, with 0 representing
* 1 BC, -1 representing 2 BC, etc.
*/
private static final int gregorianCutoverYear = 1582;
public CalendarImpl() {
super();
}
/**
* Converts UTC as milliseconds to time field values.
*/
protected void computeFields() {
int rawOffset = getTimeZone().getRawOffset();
long localMillis = time + rawOffset;
// Check for very extreme values -- millis near Long.MIN_VALUE or
// Long.MAX_VALUE. For these values, adding the zone offset can push
// the millis past MAX_VALUE to MIN_VALUE, or vice versa. This produces
// the undesirable effect that the time can wrap around at the ends,
// yielding, for example, a Date(Long.MAX_VALUE) with a big BC year
// (should be AD). Handle this by pinning such values to Long.MIN_VALUE
// or Long.MAX_VALUE. - liu 8/11/98 bug 4149677
if (time > 0 && localMillis < 0 && rawOffset > 0) {
localMillis = Long.MAX_VALUE;
} else if (time < 0 && localMillis > 0 && rawOffset < 0) {
localMillis = Long.MIN_VALUE;
}
// Time to fields takes the wall millis (Standard or DST).
timeToFields(localMillis);
long days = (long)(localMillis / ONE_DAY);
int millisInDay = (int)(localMillis - (days * ONE_DAY));
if (millisInDay < 0) millisInDay += ONE_DAY;
// Call getOffset() to get the TimeZone offset.
// The millisInDay value must be standard local millis.
int dstOffset = getTimeZone().getOffset(AD,
this.fields[YEAR],
this.fields[MONTH],
this.fields[DATE],
this.fields[DAY_OF_WEEK],
millisInDay) - rawOffset;
// Adjust our millisInDay for DST, if necessary.
millisInDay += dstOffset;
// If DST has pushed us into the next day,
// we must call timeToFields() again.
// This happens in DST between 12:00 am and 1:00 am every day.
// The call to timeToFields() will give the wrong day,
// since the Standard time is in the previous day
if (millisInDay >= ONE_DAY) {
long dstMillis = localMillis + dstOffset;
millisInDay -= ONE_DAY;
// As above, check for and pin extreme values
if (localMillis > 0 && dstMillis < 0 && dstOffset > 0) {
dstMillis = Long.MAX_VALUE;
} else if (localMillis < 0 && dstMillis > 0 && dstOffset < 0) {
dstMillis = Long.MIN_VALUE;
}
timeToFields(dstMillis);
}
// Fill in all time-related fields based on millisInDay.
// so as not to perturb flags.
this.fields[MILLISECOND] = millisInDay % 1000;
millisInDay /= 1000;
this.fields[SECOND] = millisInDay % 60;
millisInDay /= 60;
this.fields[MINUTE] = millisInDay % 60;
millisInDay /= 60;
this.fields[HOUR_OF_DAY] = millisInDay;
this.fields[AM_PM] = millisInDay / 12;
this.fields[HOUR] = millisInDay % 12;
}
/**
* Convert the time as milliseconds to the date fields. Millis must be
* given as local wall millis to get the correct local day. For example,
* if it is 11:30 pm Standard, and DST is in effect, the correct DST millis
* must be passed in to get the right date.
*
* Fields that are completed by this method: YEAR, MONTH, DATE, DAY_OF_WEEK.
* @param theTime the time in wall millis (either Standard or DST),
* whichever is in effect
*/
private final void timeToFields(long theTime) {
int dayOfYear, weekCount, rawYear;
boolean isLeap;
// Compute the year, month, and day of month from the given millis
if (theTime >= gregorianCutover) {
// The Gregorian epoch day is zero for Monday January 1, year 1.
long gregorianEpochDay =
millisToJulianDay(theTime) - JAN_1_1_JULIAN_DAY;
// Here we convert from the day number to the multiple radix
// representation. We use 400-year, 100-year, and 4-year cycles.
// For example, the 4-year cycle has 4 years + 1 leap day; giving
// 1461 == 365*4 + 1 days.
int[] rem = new int[1];
// 400-year cycle length
int n400 = floorDivide(gregorianEpochDay, 146097, rem);
// 100-year cycle length
int n100 = floorDivide(rem[0], 36524, rem);
// 4-year cycle length
int n4 = floorDivide(rem[0], 1461, rem);
int n1 = floorDivide(rem[0], 365, rem);
rawYear = 400*n400 + 100*n100 + 4*n4 + n1;
// zero-based day of year
dayOfYear = rem[0];
// Dec 31 at end of 4- or 400-yr cycle
if (n100 == 4 || n1 == 4) {
dayOfYear = 365;
} else {
++rawYear;
}
// equiv. to (rawYear%4 == 0)
isLeap =
((rawYear&0x3) == 0) && (rawYear%100 != 0 || rawYear%400 == 0);
// Gregorian day zero is a Monday
this.fields[DAY_OF_WEEK] = (int)((gregorianEpochDay+1) % 7);
} else {
// The Julian epoch day (not the same as Julian Day)
// is zero on Saturday December 30, 0 (Gregorian).
long julianEpochDay =
millisToJulianDay(theTime) - (JAN_1_1_JULIAN_DAY - 2);
rawYear = (int) floorDivide(4*julianEpochDay + 1464, 1461);
// Compute the Julian calendar day number for January 1, year
long january1 = 365*(rawYear-1) + floorDivide(rawYear-1, 4);
dayOfYear = (int)(julianEpochDay - january1); // 0-based
// Julian leap years occurred historically every 4 years starting
// with 8 AD. Before 8 AD the spacing is irregular; every 3 years
// from 45 BC to 9 BC, and then none until 8 AD. However, we don't
// implement this historical detail; instead, we implement the
// computationally cleaner proleptic calendar, which assumes
// consistent 4-year cycles throughout time.
// equiv. to (rawYear%4 == 0)
isLeap = ((rawYear&0x3) == 0);
// Julian calendar day zero is a Saturday
this.fields[DAY_OF_WEEK] = (int)((julianEpochDay-1) % 7);
}
// Common Julian/Gregorian calculation
int correction = 0;
// zero-based DOY for March 1
int march1 = isLeap ? 60 : 59;
if (dayOfYear >= march1) correction = isLeap ? 1 : 2;
// zero-based month
int month_field = (12 * (dayOfYear + correction) + 6) / 367;
// one-based DOM
int date_field = dayOfYear -
(isLeap ? LEAP_NUM_DAYS[month_field] : NUM_DAYS[month_field]) + 1;
// Normalize day of week
this.fields[DAY_OF_WEEK] += (this.fields[DAY_OF_WEEK] < 0) ? (SUNDAY+7) : SUNDAY;
this.fields[YEAR] = rawYear;
// If year is < 1 we are in BC
if (this.fields[YEAR] < 1) {
this.fields[YEAR] = 1 - this.fields[YEAR];
}
// 0-based
this.fields[MONTH] = month_field + JANUARY;
this.fields[DATE] = date_field;
}
/*
* The following two static arrays are used privately by the
* toString(Calendar calendar) function below.
*/
static String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
/**
* Converts this Date object to a String
* of the form:
*
* dow mon dd hh:mm:ss zzz yyyy
* where:Date object to a String.
* The output format is as follows:
* * where:yyyy MM dd hh mm ss +zzzz
* Unlike the built-in division, this is mathematically well-behaved.
* E.g., -1/4 => 0
* but floorDivide(-1,4) => -1.
* @param numerator the numerator
* @param denominator a divisor which must be > 0
* @return the floor of the quotient.
*/
private static final long floorDivide(long numerator, long denominator) {
// We do this computation in order to handle
// a numerator of Long.MIN_VALUE correctly
return (numerator >= 0) ?
numerator / denominator :
((numerator + 1) / denominator) - 1;
}
/**
* Divide two integers, returning the floor of the quotient.
*
* Unlike the built-in division, this is mathematically well-behaved.
* E.g., -1/4 => 0
* but floorDivide(-1,4) => -1.
* @param numerator the numerator
* @param denominator a divisor which must be > 0
* @return the floor of the quotient.
*/
private static final int floorDivide(int numerator, int denominator) {
// We do this computation in order to handle
// a numerator of Integer.MIN_VALUE correctly
return (numerator >= 0) ?
numerator / denominator :
((numerator + 1) / denominator) - 1;
}
/**
* Divide two integers, returning the floor of the quotient, and
* the modulus remainder.
*
* Unlike the built-in division, this is mathematically well-behaved.
* E.g., -1/4 => 0 and -1%4 => -1,
* but floorDivide(-1,4) => -1 with remainder[0] => 3.
* @param numerator the numerator
* @param denominator a divisor which must be > 0
* @param remainder an array of at least one element in which the value
* numerator mod denominator is returned. Unlike numerator
* % denominator, this will always be non-negative.
* @return the floor of the quotient.
*/
private static final int
floorDivide(int numerator, int denominator, int[] remainder) {
if (numerator >= 0) {
remainder[0] = numerator % denominator;
return numerator / denominator;
}
int quotient = ((numerator + 1) / denominator) - 1;
remainder[0] = numerator - (quotient * denominator);
return quotient;
}
/**
* Divide two integers, returning the floor of the quotient, and
* the modulus remainder.
*
* Unlike the built-in division, this is mathematically well-behaved.
* E.g., -1/4 => 0 and -1%4 => -1,
* but floorDivide(-1,4) => -1 with remainder[0] => 3.
* @param numerator the numerator
* @param denominator a divisor which must be > 0
* @param remainder an array of at least one element in which the value
* numerator mod denominator is returned. Unlike numerator
* % denominator, this will always be non-negative.
* @return the floor of the quotient.
*/
private static final int
floorDivide(long numerator, int denominator, int[] remainder) {
if (numerator >= 0) {
remainder[0] = (int)(numerator % denominator);
return (int)(numerator / denominator);
}
int quotient = (int)(((numerator + 1) / denominator) - 1);
remainder[0] = (int)(numerator - (quotient * denominator));
return quotient;
}
}