// TaiwanCalendar.cs
//
// (C) Ulrich Kunitz 2002
//
namespace System.Globalization {
using System;
///
/// This is the Japanese calendar. It differs from the Gregorian calendar
/// only in the years.
///
///
/// The Japanese calendar support a single era starting at January 1,
/// 1912
/// The implementation uses the
/// namespace.
///
///
[Serializable]
public class TaiwanCalendar : Calendar {
///
/// Static protected field storing the
/// .
///
internal static readonly CCGregorianEraHandler M_EraHandler;
///
/// Static constructor, who creates and initializes
/// .
///
static TaiwanCalendar() {
M_EraHandler = new CCGregorianEraHandler();
M_EraHandler.appendEra(1,
CCGregorianCalendar.fixed_from_dmy(1, 1, 1912));
}
///
/// Default constructor.
///
public TaiwanCalendar() {
M_AbbrEraNames = new string[] {"T.C.E."};
M_EraNames = new string[] {"Taiwan current era"};
}
/// Overridden. Gives the eras supported by the
/// calendar as an array of integers.
///
public override int[] Eras {
get {
return (int[])M_EraHandler.Eras.Clone();
}
}
///
/// A protected member checking a
/// value.
///
/// The
///
/// to check.
///
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
internal void M_CheckDateTime(DateTime time) {
M_EraHandler.CheckDateTime(time);
}
///
/// A protected method checking the era number.
///
/// The era number as reference. It is set
/// to , if the input value is 0.
///
/// The exception is thrown if the era is not supported by the class.
///
internal void M_CheckEra(ref int era) {
if (era == CurrentEra)
era = 1;
if (!M_EraHandler.ValidEra(era))
throw new ArgumentException("Era value was not valid.");
}
///
/// A protected method checking calendar year and the era number.
///
/// An integer representing the calendar year.
///
/// The era number as reference.
///
/// The exception is thrown if the era is not supported by the class.
///
///
/// The exception is thrown if the calendar year is outside of
/// the supported range.
///
internal int M_CheckYEG(int year, ref int era) {
M_CheckEra(ref era);
return M_EraHandler.GregorianYear(year, era);
}
///
/// Checks whether the year is the era is valid, if era = CurrentEra
/// the right value is set.
///
/// The year to check.
/// The era to check.
///
/// The exception will be thrown, if the year is not valid.
///
internal override void M_CheckYE(int year, ref int era) {
M_CheckYEG(year, ref era);
}
///
/// A protected method checking the calendar year, month, and
/// era number.
///
/// An integer representing the calendar year.
///
/// An integer giving the calendar month.
///
/// The era number as reference.
///
/// The exception is thrown if the era is not supported by the class.
///
///
/// The exception is thrown if the calendar year or month is
/// outside of the supported range.
///
internal int M_CheckYMEG(int year, int month, ref int era) {
int gregorianYear = M_CheckYEG(year, ref era);
if (month < 1 || month > 12)
throw new ArgumentOutOfRangeException("month",
"Month must be between one and twelve.");
return gregorianYear;
}
///
/// A protected method checking the calendar day, month, and year
/// and the era number.
///
/// An integer representing the calendar year.
///
/// An integer giving the calendar month.
///
/// An integer giving the calendar day.
///
/// The era number as reference.
///
/// The exception is thrown if the era is not supported by the class.
///
///
/// The exception is thrown if the calendar year, month, or day is
/// outside of the supported range.
///
internal int M_CheckYMDEG(int year, int month, int day, ref int era)
{
int gregorianYear = M_CheckYMEG(year, month, ref era);
M_ArgumentInRange("day", day, 1,
GetDaysInMonth(year, month, era));
return gregorianYear;
}
///
/// Overridden. Adds days to a given date.
///
/// The
/// to which to add
/// days.
///
/// The number of days to add.
/// A new value, that
/// results from adding to the specified
/// DateTime.
///
/// The exception is thrown if the
/// return value is outside all
/// supported eras.
///
public override DateTime AddDays(DateTime time, int days) {
DateTime t = base.AddDays(time, days);
M_CheckDateTime(t);
return t;
}
///
/// Overridden. Adds hours to a given date.
///
/// The
/// to which to add
/// hours.
///
/// The number of hours to add.
/// A new value, that
/// results from adding to the specified
/// DateTime.
///
/// The exception is thrown if the
/// return value is outside all
/// supported eras.
///
public override DateTime AddHours(DateTime time, int hours) {
DateTime t = base.AddHours(time, hours);
M_CheckDateTime(t);
return t;
}
///
/// Overridden. Adds milliseconds to a given date.
///
/// The
/// to which to add
/// milliseconds.
///
/// The number of milliseconds given as
/// double to add. Keep in mind the 100 nanosecond resolution of
/// .
///
/// A new value, that
/// results from adding to the specified
/// DateTime.
///
/// The exception is thrown if the
/// return value is outside all
/// supported eras.
///
public override DateTime AddMilliseconds(DateTime time,
double milliseconds)
{
DateTime t = base.AddMilliseconds(time, milliseconds);
M_CheckDateTime(t);
return t;
}
///
/// Overridden. Adds minutes to a given date.
///
/// The
/// to which to add
/// minutes.
///
/// The number of minutes to add.
/// A new value, that
/// results from adding to the specified
/// DateTime.
///
/// The exception is thrown if the
/// return value is outside all
/// supported eras.
///
public override DateTime AddMinutes(DateTime time, int minutes) {
DateTime t = base.AddMinutes(time, minutes);
M_CheckDateTime(t);
return t;
}
///
/// Overridden. Adds seconds to a given date.
///
/// The
/// to which to add
/// seconds.
///
/// The number of seconds to add.
/// A new value, that
/// results from adding to the specified
/// DateTime.
///
/// The exception is thrown if the
/// return value is outside all
/// supported eras.
///
public override DateTime AddSeconds(DateTime time, int seconds) {
DateTime t = base.AddSeconds(time, seconds);
M_CheckDateTime(t);
return t;
}
///
/// Overridden. Adds weeks to a given date.
///
/// The
/// to which to add
/// weeks.
///
/// The number of weeks to add.
/// A new value, that
/// results from adding to the specified
/// DateTime.
///
/// The exception is thrown if the
/// return value is outside all
/// supported eras.
///
public override DateTime AddWeeks(DateTime time, int weeks) {
DateTime t = base.AddWeeks(time, weeks);
M_CheckDateTime(t);
return t;
}
///
/// Overridden. Gives the hour of the specified time.
///
/// The
/// that specifies the
/// time.
///
/// An integer that gives the hour of the specified time,
/// starting with 0.
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override int GetHour(DateTime time) {
M_CheckDateTime(time);
return base.GetHour(time);
}
///
/// Overridden. Gives the milliseconds in the current second
/// of the specified time.
///
/// The
/// that specifies the
/// time.
///
/// An integer that gives the milliseconds in the seconds
/// of the specified time, starting with 0.
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override double GetMilliseconds(DateTime time) {
M_CheckDateTime(time);
return base.GetMilliseconds(time);
}
///
/// Overridden. Gives the minute of the specified time.
///
/// The
/// that specifies the
/// time.
///
/// An integer that gives the minute of the specified time,
/// starting with 0.
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override int GetMinute(DateTime time) {
M_CheckDateTime(time);
return base.GetMinute(time);
}
///
/// Overridden. Gives the second of the specified time.
///
/// The
/// that specifies the
/// time.
///
/// An integer that gives the second of the specified time,
/// starting with 0.
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override int GetSecond(DateTime time) {
M_CheckDateTime(time);
return base.GetMinute(time);
}
///
/// Overrideden. Adds months to a given date.
///
/// The
/// to which to add
/// months.
///
/// The number of months to add.
/// A new value, that
/// results from adding to the specified
/// DateTime.
///
/// The exception is thrown if
/// return value is outside all
/// supported eras.
///
public override DateTime AddMonths(DateTime time, int months) {
DateTime t = CCGregorianCalendar.AddMonths(time, months);
M_CheckDateTime(t);
return t;
}
///
/// Overridden. Adds years to a given date.
///
/// The
/// to which to add
/// years.
///
/// The number of years to add.
/// A new value, that
/// results from adding to the specified
/// DateTime.
///
/// The exception is thrown if
/// return value is outside all
/// supported eras.
///
public override DateTime AddYears(DateTime time, int years) {
DateTime t = CCGregorianCalendar.AddYears(time, years);
M_CheckDateTime(t);
return t;
}
///
/// Overriden. Gets the day of the month from
/// .
///
/// The
/// that specifies a
/// date.
///
/// An integer giving the day of months, starting with 1.
///
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override int GetDayOfMonth(DateTime time) {
M_CheckDateTime(time);
return CCGregorianCalendar.GetDayOfMonth(time);
}
///
/// Overriden. Gets the day of the week from the specified date.
///
/// The
/// that specifies a
/// date.
///
/// An integer giving the day of months, starting with 1.
///
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override DayOfWeek GetDayOfWeek(DateTime time) {
M_CheckDateTime(time);
int rd = CCFixed.FromDateTime(time);
return (DayOfWeek)CCFixed.day_of_week(rd);
}
///
/// Overridden. Gives the number of the day in the year.
///
/// The
/// that specifies a
/// date.
///
/// An integer representing the day of the year,
/// starting with 1.
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override int GetDayOfYear(DateTime time) {
M_CheckDateTime(time);
return CCGregorianCalendar.GetDayOfYear(time);
}
///
/// Overridden. Gives the number of days in the specified month
/// of the given year and era.
///
/// An integer that gives the year.
///
/// An integer that gives the month, starting
/// with 1.
/// An integer that gives the era of the specified
/// year.
/// An integer that gives the number of days of the
/// specified month.
///
/// The exception is thrown, if ,
/// ,or is outside
/// the allowed range.
///
public override int GetDaysInMonth(int year, int month, int era) {
int gregorianYear = M_CheckYMEG(year, month, ref era);
return CCGregorianCalendar.GetDaysInMonth(gregorianYear, month);
}
///
/// Overridden. Gives the number of days of the specified
/// year of the given era.
///
/// An integer that specifies the year.
///
/// An ineger that specifies the era.
///
/// An integer that gives the number of days of the
/// specified year.
///
/// The exception is thrown, if
/// or are outside the
/// allowed range.
///
public override int GetDaysInYear(int year, int era) {
int gregorianYear = M_CheckYEG(year, ref era);
return CCGregorianCalendar.GetDaysInYear(gregorianYear);
}
///
/// Overridden. Gives the era of the specified date.
///
/// The
/// that specifies a
/// date.
///
/// An integer representing the era of the calendar.
///
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override int GetEra(DateTime time) {
// M_CheckDateTime not needed, because EraYear does the
// right thing.
int rd = CCFixed.FromDateTime(time);
int era;
M_EraHandler.EraYear(out era, rd);
return era;
}
///
/// Overridden. Gives the number of the month of the specified
/// date.
///
/// The
/// that specifies a
/// date.
///
/// An integer representing the month,
/// starting with 1.
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override int GetMonth(DateTime time) {
M_CheckDateTime(time);
return CCGregorianCalendar.GetMonth(time);
}
///
/// Overridden. Gives the number of months in the specified year
/// and era.
///
/// An integer that specifies the year.
///
/// An integer that specifies the era.
///
/// An integer that gives the number of the months in the
/// specified year.
///
/// The exception is thrown, if the year or the era are not valid.
///
public override int GetMonthsInYear(int year, int era) {
M_CheckYEG(year, ref era);
return 12;
}
///
/// Overridden. Gives the number of the year of the specified
/// date.
///
/// The
/// that specifies a
/// date.
///
/// An integer representing the year,
/// starting with 1.
///
/// The exception is thrown if the
/// parameter is outside all
/// supported eras.
///
public override int GetYear(DateTime time) {
// M_CheckDateTime not needed, because EraYeat does the
// right thing.
int rd = CCFixed.FromDateTime(time);
int era;
return M_EraHandler.EraYear(out era, rd);
}
///
/// Overridden. Tells whether the given day
/// is a leap day.
///
/// An integer that specifies the year in the
/// given era.
///
/// An integer that specifies the month.
///
/// An integer that specifies the day.
///
/// An integer that specifies the era.
///
/// A boolean that tells whether the given day is a leap
/// day.
///
///
/// The exception is thrown, if the year, month, day, or era is not
/// valid.
///
public override bool IsLeapDay(int year, int month, int day, int era)
{
int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
}
///
/// Overridden. Tells whether the given month
/// is a leap month.
///
/// An integer that specifies the year in the
/// given era.
///
/// An integer that specifies the month.
///
/// An integer that specifies the era.
///
/// A boolean that tells whether the given month is a leap
/// month.
///
///
/// The exception is thrown, if the year, month, or era is not
/// valid.
///
public override bool IsLeapMonth(int year, int month, int era) {
M_CheckYMEG(year, month, ref era);
return false;
}
///
/// Overridden. Tells whether the given year
/// is a leap year.
///
/// An integer that specifies the year in the
/// given era.
///
/// An integer that specifies the era.
///
/// A boolean that tells whether the given year is a leap
/// year.
///
///
/// The exception is thrown, if the year or era is not
/// valid.
///
public override bool IsLeapYear(int year, int era) {
int gregorianYear = M_CheckYEG(year, ref era);
return CCGregorianCalendar.is_leap_year(gregorianYear);
}
///
/// Overridden. Creates the
/// from the parameters.
///
/// An integer that gives the year in the
/// .
///
/// An integer that specifies the month.
///
/// An integer that specifies the day.
///
/// An integer that specifies the hour.
///
/// An integer that specifies the minute.
///
/// An integer that gives the second.
///
/// An integer that gives the
/// milliseconds.
///
/// An integer that specifies the era.
///
/// A
/// representig the date and time.
///
///
/// The exception is thrown, if at least one of the parameters
/// is out of range.
///
public override DateTime ToDateTime(int year, int month, int day,
int hour, int minute, int second, int milliseconds,
int era)
{
int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
M_CheckHMSM(hour, minute, second, milliseconds);
return CCGregorianCalendar.ToDateTime(
gregorianYear, month, day,
hour, minute, second, milliseconds);
}
///
/// This functions returns simply the year for the Taiwan calendar.
///
/// An integer that gives the year.
///
/// The same argument as the year.
///
///
/// The exception is thrown if the year is negative or the resulting
/// year is invalid.
///
public override int ToFourDigitYear(int year) {
if (year < 0)
throw new ArgumentOutOfRangeException(
"year", "Non-negative number required.");
int era = CurrentEra;
M_CheckYE(year, ref era);
return year;
}
} // class TaiwanCalendar
} // namespace System.Globalization