summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Globalization/Calendar.cs
diff options
context:
space:
mode:
authorJiyoung Yun <jy910.yun@samsung.com>2017-04-13 14:17:19 +0900
committerJiyoung Yun <jy910.yun@samsung.com>2017-04-13 14:17:19 +0900
commita56e30c8d33048216567753d9d3fefc2152af8ac (patch)
tree7e5d979695fc4a431740982eb1cfecc2898b23a5 /src/mscorlib/src/System/Globalization/Calendar.cs
parent4b11dc566a5bbfa1378d6266525c281b028abcc8 (diff)
downloadcoreclr-a56e30c8d33048216567753d9d3fefc2152af8ac.tar.gz
coreclr-a56e30c8d33048216567753d9d3fefc2152af8ac.tar.bz2
coreclr-a56e30c8d33048216567753d9d3fefc2152af8ac.zip
Imported Upstream version 2.0.0.11353upstream/2.0.0.11353
Diffstat (limited to 'src/mscorlib/src/System/Globalization/Calendar.cs')
-rw-r--r--src/mscorlib/src/System/Globalization/Calendar.cs297
1 files changed, 151 insertions, 146 deletions
diff --git a/src/mscorlib/src/System/Globalization/Calendar.cs b/src/mscorlib/src/System/Globalization/Calendar.cs
index 1ff795ddc5..c23e1088c1 100644
--- a/src/mscorlib/src/System/Globalization/Calendar.cs
+++ b/src/mscorlib/src/System/Globalization/Calendar.cs
@@ -2,14 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-namespace System.Globalization {
- using System;
- using System.Runtime.CompilerServices;
- using System.Globalization;
- using System.Runtime.Versioning;
- using System.Diagnostics;
- using System.Diagnostics.Contracts;
+using System.Diagnostics;
+using System.Diagnostics.Contracts;
+using System.Runtime.Serialization;
+namespace System.Globalization
+{
// This abstract class represents a calendar. A calendar reckons time in
// divisions such as weeks, months and years. The number, length and start of
// the divisions vary in each calendar.
@@ -31,69 +29,49 @@ namespace System.Globalization {
// since most of the calendars (or all?) have the same way of calcuating hour/minute/second.
[Serializable]
- public abstract class Calendar : ICloneable
+ public abstract partial class Calendar : ICloneable
{
-
// Number of 100ns (10E-7 second) ticks per time unit
- internal const long TicksPerMillisecond = 10000;
- internal const long TicksPerSecond = TicksPerMillisecond * 1000;
- internal const long TicksPerMinute = TicksPerSecond * 60;
- internal const long TicksPerHour = TicksPerMinute * 60;
- internal const long TicksPerDay = TicksPerHour * 24;
+ internal const long TicksPerMillisecond = 10000;
+ internal const long TicksPerSecond = TicksPerMillisecond * 1000;
+ internal const long TicksPerMinute = TicksPerSecond * 60;
+ internal const long TicksPerHour = TicksPerMinute * 60;
+ internal const long TicksPerDay = TicksPerHour * 24;
// Number of milliseconds per time unit
- internal const int MillisPerSecond = 1000;
- internal const int MillisPerMinute = MillisPerSecond * 60;
- internal const int MillisPerHour = MillisPerMinute * 60;
- internal const int MillisPerDay = MillisPerHour * 24;
+ internal const int MillisPerSecond = 1000;
+ internal const int MillisPerMinute = MillisPerSecond * 60;
+ internal const int MillisPerHour = MillisPerMinute * 60;
+ internal const int MillisPerDay = MillisPerHour * 24;
// Number of days in a non-leap year
- internal const int DaysPerYear = 365;
+ internal const int DaysPerYear = 365;
// Number of days in 4 years
- internal const int DaysPer4Years = DaysPerYear * 4 + 1;
+ internal const int DaysPer4Years = DaysPerYear * 4 + 1;
// Number of days in 100 years
- internal const int DaysPer100Years = DaysPer4Years * 25 - 1;
+ internal const int DaysPer100Years = DaysPer4Years * 25 - 1;
// Number of days in 400 years
- internal const int DaysPer400Years = DaysPer100Years * 4 + 1;
+ internal const int DaysPer400Years = DaysPer100Years * 4 + 1;
// Number of days from 1/1/0001 to 1/1/10000
- internal const int DaysTo10000 = DaysPer400Years * 25 - 366;
-
- internal const long MaxMillis = (long)DaysTo10000 * MillisPerDay;
-
- //
- // Calendar ID Values. This is used to get data from calendar.nlp.
- // The order of calendar ID means the order of data items in the table.
- //
-
- internal const int CAL_GREGORIAN = 1 ; // Gregorian (localized) calendar
- internal const int CAL_GREGORIAN_US = 2 ; // Gregorian (U.S.) calendar
- internal const int CAL_JAPAN = 3 ; // Japanese Emperor Era calendar
- internal const int CAL_TAIWAN = 4 ; // Taiwan Era calendar
- internal const int CAL_KOREA = 5 ; // Korean Tangun Era calendar
- internal const int CAL_HIJRI = 6 ; // Hijri (Arabic Lunar) calendar
- internal const int CAL_THAI = 7 ; // Thai calendar
- internal const int CAL_HEBREW = 8 ; // Hebrew (Lunar) calendar
- internal const int CAL_GREGORIAN_ME_FRENCH = 9 ; // Gregorian Middle East French calendar
- internal const int CAL_GREGORIAN_ARABIC = 10; // Gregorian Arabic calendar
- internal const int CAL_GREGORIAN_XLIT_ENGLISH = 11; // Gregorian Transliterated English calendar
- internal const int CAL_GREGORIAN_XLIT_FRENCH = 12;
- internal const int CAL_JULIAN = 13;
- internal const int CAL_JAPANESELUNISOLAR = 14;
- internal const int CAL_CHINESELUNISOLAR = 15;
- internal const int CAL_SAKA = 16; // reserved to match Office but not implemented in our code
- internal const int CAL_LUNAR_ETO_CHN = 17; // reserved to match Office but not implemented in our code
- internal const int CAL_LUNAR_ETO_KOR = 18; // reserved to match Office but not implemented in our code
- internal const int CAL_LUNAR_ETO_ROKUYOU = 19; // reserved to match Office but not implemented in our code
- internal const int CAL_KOREANLUNISOLAR = 20;
- internal const int CAL_TAIWANLUNISOLAR = 21;
- internal const int CAL_PERSIAN = 22;
- internal const int CAL_UMALQURA = 23;
-
- internal int m_currentEraValue = -1;
-
- [System.Runtime.Serialization.OptionalField(VersionAdded = 2)]
- private bool m_isReadOnly = false;
+ internal const int DaysTo10000 = DaysPer400Years * 25 - 366;
+
+ internal const long MaxMillis = (long)DaysTo10000 * MillisPerDay;
+
+ private int _currentEraValue = -1;
+
+ [OptionalField(VersionAdded = 2)]
+ private bool _isReadOnly = false;
+
+#if CORECLR
+ internal const CalendarId CAL_HEBREW = CalendarId.HEBREW;
+ internal const CalendarId CAL_HIJRI = CalendarId.HIJRI;
+ internal const CalendarId CAL_JAPAN = CalendarId.JAPAN;
+ internal const CalendarId CAL_JULIAN = CalendarId.JULIAN;
+ internal const CalendarId CAL_TAIWAN = CalendarId.TAIWAN;
+ internal const CalendarId CAL_UMALQURA = CalendarId.UMALQURA;
+ internal const CalendarId CAL_PERSIAN = CalendarId.PERSIAN;
+#endif
// The minimum supported DateTime range for the calendar.
@@ -115,19 +93,27 @@ namespace System.Globalization {
}
}
+ public virtual CalendarAlgorithmType AlgorithmType
+ {
+ get
+ {
+ return CalendarAlgorithmType.Unknown;
+ }
+ }
-
-
- protected Calendar() {
+ protected Calendar()
+ {
//Do-nothing constructor.
}
///
// This can not be abstract, otherwise no one can create a subclass of Calendar.
//
- internal virtual int ID {
- get {
- return (-1);
+ internal virtual CalendarId ID
+ {
+ get
+ {
+ return CalendarId.UNINITIALIZED_VALUE;
}
}
@@ -135,21 +121,11 @@ namespace System.Globalization {
// Return the Base calendar ID for calendars that didn't have defined data in calendarData
//
- internal virtual int BaseCalendarID
+ internal virtual CalendarId BaseCalendarID
{
get { return ID; }
}
- // Returns the type of the calendar.
- //
- public virtual CalendarAlgorithmType AlgorithmType
- {
- get
- {
- return CalendarAlgorithmType.Unknown;
- }
- }
-
////////////////////////////////////////////////////////////////////////
//
// IsReadOnly
@@ -159,7 +135,7 @@ namespace System.Globalization {
////////////////////////////////////////////////////////////////////////
public bool IsReadOnly
{
- get { return (m_isReadOnly); }
+ get { return (_isReadOnly); }
}
////////////////////////////////////////////////////////////////////////
@@ -169,13 +145,13 @@ namespace System.Globalization {
// Is the implementation of ICloneable.
//
////////////////////////////////////////////////////////////////////////
- public virtual Object Clone()
+ public virtual object Clone()
{
object o = MemberwiseClone();
- ((Calendar) o).SetReadOnlyState(false);
+ ((Calendar)o).SetReadOnlyState(false);
return (o);
}
-
+
////////////////////////////////////////////////////////////////////////
//
// ReadOnly
@@ -184,29 +160,29 @@ namespace System.Globalization {
// readonly.
//
////////////////////////////////////////////////////////////////////////
- public static Calendar ReadOnly(Calendar calendar)
+ public static Calendar ReadOnly(Calendar calendar)
{
- if (calendar == null) { throw new ArgumentNullException(nameof(calendar)); }
+ if (calendar == null) { throw new ArgumentNullException(nameof(calendar)); }
Contract.EndContractBlock();
- if (calendar.IsReadOnly) { return (calendar); }
-
+ if (calendar.IsReadOnly) { return (calendar); }
+
Calendar clonedCalendar = (Calendar)(calendar.MemberwiseClone());
clonedCalendar.SetReadOnlyState(true);
-
+
return (clonedCalendar);
}
internal void VerifyWritable()
{
- if (m_isReadOnly)
+ if (_isReadOnly)
{
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ReadOnly"));
+ throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
}
internal void SetReadOnlyState(bool readOnly)
{
- m_isReadOnly = readOnly;
+ _isReadOnly = readOnly;
}
@@ -219,14 +195,17 @@ namespace System.Globalization {
** The value is from calendar.nlp.
============================================================================*/
- internal virtual int CurrentEraValue {
- get {
+ internal virtual int CurrentEraValue
+ {
+ get
+ {
// The following code assumes that the current era value can not be -1.
- if (m_currentEraValue == -1) {
- Debug.Assert(BaseCalendarID > 0, "[Calendar.CurrentEraValue] Expected ID > 0");
- m_currentEraValue = CalendarData.GetCalendarData(BaseCalendarID).iCurrentEra;
+ if (_currentEraValue == -1)
+ {
+ Debug.Assert(BaseCalendarID != CalendarId.UNINITIALIZED_VALUE, "[Calendar.CurrentEraValue] Expected a real calendar ID");
+ _currentEraValue = CalendarData.GetCalendarData(BaseCalendarID).iCurrentEra;
}
- return (m_currentEraValue);
+ return (_currentEraValue);
}
}
@@ -236,16 +215,19 @@ namespace System.Globalization {
internal int twoDigitYearMax = -1;
- internal static void CheckAddResult(long ticks, DateTime minValue, DateTime maxValue) {
- if (ticks < minValue.Ticks || ticks > maxValue.Ticks) {
+ internal static void CheckAddResult(long ticks, DateTime minValue, DateTime maxValue)
+ {
+ if (ticks < minValue.Ticks || ticks > maxValue.Ticks)
+ {
throw new ArgumentException(
- String.Format(CultureInfo.InvariantCulture, Environment.GetResourceString("Argument_ResultCalendarRange"),
- minValue, maxValue));
+ String.Format(CultureInfo.InvariantCulture, SR.Format(SR.Argument_ResultCalendarRange,
+ minValue, maxValue)));
}
Contract.EndContractBlock();
}
- internal DateTime Add(DateTime time, double value, int scale) {
+ internal DateTime Add(DateTime time, double value, int scale)
+ {
// From ECMA CLI spec, Partition III, section 3.27:
//
// If overflow occurs converting a floating-point type to an integer, or if the floating-point value
@@ -256,7 +238,7 @@ namespace System.Globalization {
double tempMillis = (value * scale + (value >= 0 ? 0.5 : -0.5));
if (!((tempMillis > -(double)MaxMillis) && (tempMillis < (double)MaxMillis)))
{
- throw new ArgumentOutOfRangeException(nameof(value), Environment.GetResourceString("ArgumentOutOfRange_AddValue"));
+ throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_AddValue);
}
long millis = (long)tempMillis;
@@ -272,7 +254,8 @@ namespace System.Globalization {
// argument is permitted to be negative.
//
- public virtual DateTime AddMilliseconds(DateTime time, double milliseconds) {
+ public virtual DateTime AddMilliseconds(DateTime time, double milliseconds)
+ {
return (Add(time, milliseconds, 1));
}
@@ -284,7 +267,8 @@ namespace System.Globalization {
// value argument is permitted to be negative.
//
- public virtual DateTime AddDays(DateTime time, int days) {
+ public virtual DateTime AddDays(DateTime time, int days)
+ {
return (Add(time, days, MillisPerDay));
}
@@ -295,7 +279,8 @@ namespace System.Globalization {
// value argument is permitted to be negative.
//
- public virtual DateTime AddHours(DateTime time, int hours) {
+ public virtual DateTime AddHours(DateTime time, int hours)
+ {
return (Add(time, hours, MillisPerHour));
}
@@ -307,7 +292,8 @@ namespace System.Globalization {
// value argument is permitted to be negative.
//
- public virtual DateTime AddMinutes(DateTime time, int minutes) {
+ public virtual DateTime AddMinutes(DateTime time, int minutes)
+ {
return (Add(time, minutes, MillisPerMinute));
}
@@ -339,7 +325,8 @@ namespace System.Globalization {
// value argument is permitted to be negative.
//
- public virtual DateTime AddSeconds(DateTime time, int seconds) {
+ public virtual DateTime AddSeconds(DateTime time, int seconds)
+ {
return Add(time, seconds, MillisPerSecond);
}
@@ -348,7 +335,8 @@ namespace System.Globalization {
// value argument is permitted to be negative.
//
- public virtual DateTime AddWeeks(DateTime time, int weeks) {
+ public virtual DateTime AddWeeks(DateTime time, int weeks)
+ {
return (AddDays(time, weeks * 7));
}
@@ -425,7 +413,8 @@ namespace System.Globalization {
============================================================================*/
- public abstract int[] Eras {
+ public abstract int[] Eras
+ {
get;
}
@@ -434,7 +423,8 @@ namespace System.Globalization {
// integer between 0 and 23.
//
- public virtual int GetHour(DateTime time) {
+ public virtual int GetHour(DateTime time)
+ {
return ((int)((time.Ticks / TicksPerHour) % 24));
}
@@ -442,7 +432,8 @@ namespace System.Globalization {
// is an integer between 0 and 999.
//
- public virtual double GetMilliseconds(DateTime time) {
+ public virtual double GetMilliseconds(DateTime time)
+ {
return (double)((time.Ticks / TicksPerMillisecond) % 1000);
}
@@ -450,7 +441,8 @@ namespace System.Globalization {
// an integer between 0 and 59.
//
- public virtual int GetMinute(DateTime time) {
+ public virtual int GetMinute(DateTime time)
+ {
return ((int)((time.Ticks / TicksPerMinute) % 60));
}
@@ -475,7 +467,8 @@ namespace System.Globalization {
// an integer between 0 and 59.
//
- public virtual int GetSecond(DateTime time) {
+ public virtual int GetSecond(DateTime time)
+ {
return ((int)((time.Ticks / TicksPerSecond) % 60));
}
@@ -516,7 +509,8 @@ namespace System.Globalization {
** So Week of year = (GetDayOfYear(time) + offset - 1) / 7 + 1
============================================================================*/
- internal int GetFirstDayWeekOfYear(DateTime time, int firstDayOfWeek) {
+ internal int GetFirstDayWeekOfYear(DateTime time, int firstDayOfWeek)
+ {
int dayOfYear = GetDayOfYear(time) - 1; // Make the day of year to be 0-based, so that 1/1 is day 0.
// Calculate the day of week for the first day of the year.
// dayOfWeek - (dayOfYear % 7) is the day of week for the first day of this year. Note that
@@ -527,7 +521,8 @@ namespace System.Globalization {
return ((dayOfYear + offset) / 7 + 1);
}
- private int GetWeekOfYearFullDays(DateTime time, int firstDayOfWeek, int fullDays) {
+ private int GetWeekOfYearFullDays(DateTime time, int firstDayOfWeek, int fullDays)
+ {
int dayForJan1;
int offset;
int day;
@@ -576,11 +571,12 @@ namespace System.Globalization {
// Calculate the day of year for specified time by taking offset into account.
//
day = dayOfYear - offset;
- if (day >= 0) {
+ if (day >= 0)
+ {
//
// If the day of year value is greater than zero, get the week of year.
//
- return (day/7 + 1);
+ return (day / 7 + 1);
}
//
// Otherwise, the specified time falls on the week of previous year.
@@ -643,13 +639,15 @@ namespace System.Globalization {
public virtual int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
{
- if ((int)firstDayOfWeek < 0 || (int)firstDayOfWeek > 6) {
+ if ((int)firstDayOfWeek < 0 || (int)firstDayOfWeek > 6)
+ {
throw new ArgumentOutOfRangeException(
- nameof(firstDayOfWeek), Environment.GetResourceString("ArgumentOutOfRange_Range",
+ nameof(firstDayOfWeek), SR.Format(SR.ArgumentOutOfRange_Range,
DayOfWeek.Sunday, DayOfWeek.Saturday));
}
Contract.EndContractBlock();
- switch (rule) {
+ switch (rule)
+ {
case CalendarWeekRule.FirstDay:
return (GetFirstDayWeekOfYear(time, (int)firstDayOfWeek));
case CalendarWeekRule.FirstFullWeek:
@@ -658,9 +656,8 @@ namespace System.Globalization {
return (GetWeekOfYearFullDays(time, (int)firstDayOfWeek, 4));
}
throw new ArgumentOutOfRangeException(
- nameof(rule), Environment.GetResourceString("ArgumentOutOfRange_Range",
+ nameof(rule), SR.Format(SR.ArgumentOutOfRange_Range,
CalendarWeekRule.FirstDay, CalendarWeekRule.FirstFourDayWeek));
-
}
// Returns the year part of the specified DateTime. The returned value is an
@@ -688,7 +685,8 @@ namespace System.Globalization {
// month is a leap month, or false if not.
//
- public virtual bool IsLeapMonth(int year, int month) {
+ public virtual bool IsLeapMonth(int year, int month)
+ {
return (IsLeapMonth(year, month, CurrentEra));
}
@@ -717,7 +715,7 @@ namespace System.Globalization {
return 0;
int monthsCount = GetMonthsInYear(year, era);
- for (int month=1; month<=monthsCount; month++)
+ for (int month = 1; month <= monthsCount; month++)
{
if (IsLeapMonth(year, month, era))
return month;
@@ -744,7 +742,7 @@ namespace System.Globalization {
// Returns the date and time converted to a DateTime value. Throws an exception if the n-tuple is invalid.
//
- public virtual DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
+ public virtual DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
{
return (ToDateTime(year, month, day, hour, minute, second, millisecond, CurrentEra));
}
@@ -754,30 +752,35 @@ namespace System.Globalization {
public abstract DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era);
- internal virtual Boolean TryToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era, out DateTime result) {
+ internal virtual Boolean TryToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era, out DateTime result)
+ {
result = DateTime.MinValue;
- try {
+ try
+ {
result = ToDateTime(year, month, day, hour, minute, second, millisecond, era);
return true;
}
- catch (ArgumentException) {
+ catch (ArgumentException)
+ {
return false;
}
}
-
- internal virtual bool IsValidYear(int year, int era) {
+
+ internal virtual bool IsValidYear(int year, int era)
+ {
return (year >= GetYear(MinSupportedDateTime) && year <= GetYear(MaxSupportedDateTime));
}
-
- internal virtual bool IsValidMonth(int year, int month, int era) {
+
+ internal virtual bool IsValidMonth(int year, int month, int era)
+ {
return (IsValidYear(year, era) && month >= 1 && month <= GetMonthsInYear(year, era));
}
-
+
internal virtual bool IsValidDay(int year, int month, int day, int era)
{
return (IsValidMonth(year, month, era) && day >= 1 && day <= GetDaysInMonth(year, month, era));
}
-
+
// Returns and assigns the maximum value to represent a two digit year. This
// value is the upper boundary of a 100 year range that allows a two digit year
@@ -805,14 +808,17 @@ namespace System.Globalization {
// then a two digit value of 30 will get converted to 1930 while a two digit
// value of 29 will get converted to 2029.
- public virtual int ToFourDigitYear(int year) {
- if (year < 0) {
+ public virtual int ToFourDigitYear(int year)
+ {
+ if (year < 0)
+ {
throw new ArgumentOutOfRangeException(nameof(year),
- Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
+ SR.ArgumentOutOfRange_NeedNonNegNum);
}
Contract.EndContractBlock();
- if (year < 100) {
- return ((TwoDigitYearMax/100 - ( year > TwoDigitYearMax % 100 ? 1 : 0))*100 + year);
+ if (year < 100)
+ {
+ return ((TwoDigitYearMax / 100 - (year > TwoDigitYearMax % 100 ? 1 : 0)) * 100 + year);
}
// If the year value is above 100, just return the year value. Don't have to do
// the TwoDigitYearMax comparison.
@@ -823,30 +829,29 @@ namespace System.Globalization {
// Will check the if the parameters are valid.
internal static long TimeToTicks(int hour, int minute, int second, int millisecond)
{
- if (hour >= 0 && hour < 24 && minute >= 0 && minute < 60 && second >=0 && second < 60)
+ if (hour >= 0 && hour < 24 && minute >= 0 && minute < 60 && second >= 0 && second < 60)
{
- if (millisecond < 0 || millisecond >= MillisPerSecond) {
+ if (millisecond < 0 || millisecond >= MillisPerSecond)
+ {
throw new ArgumentOutOfRangeException(
nameof(millisecond),
String.Format(
CultureInfo.InvariantCulture,
- Environment.GetResourceString("ArgumentOutOfRange_Range"), 0, MillisPerSecond - 1));
+ SR.Format(SR.ArgumentOutOfRange_Range, 0, MillisPerSecond - 1)));
}
- return TimeSpan.TimeToTicks(hour, minute, second) + millisecond * TicksPerMillisecond;
+ return InternalGlobalizationHelper.TimeToTicks(hour, minute, second) + millisecond * TicksPerMillisecond;
}
- throw new ArgumentOutOfRangeException(null, Environment.GetResourceString("ArgumentOutOfRange_BadHourMinuteSecond"));
+ throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_BadHourMinuteSecond);
}
- internal static int GetSystemTwoDigitYearSetting(int CalID, int defaultYearValue)
+ internal static int GetSystemTwoDigitYearSetting(CalendarId CalID, int defaultYearValue)
{
- // Call nativeGetTwoDigitYearMax
- int twoDigitYearMax = CalendarData.nativeGetTwoDigitYearMax(CalID);
+ int twoDigitYearMax = CalendarData.GetTwoDigitYearMax(CalID);
if (twoDigitYearMax < 0)
{
twoDigitYearMax = defaultYearValue;
}
return (twoDigitYearMax);
}
-
}
}