| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609 |
- // System.Globalization.DateTimeFormatInfo
- //
- // Some useful functions are missing in the ECMA specs.
- // They have been added following MS SDK Beta2
- //
- // Martin Weindel ([email protected])
- //
- // (C) Martin Weindel ([email protected])
- using System;
- using System.Threading;
- namespace System.Globalization
- {
- [Serializable]
-
- public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider {
- private static readonly string MSG_READONLY = "This instance is read only";
- private static readonly string MSG_ARRAYSIZE_MONTH = "An array with exactly 13 elements is needed";
- private static readonly string MSG_ARRAYSIZE_DAY = "An array with exactly 7 elements is needed";
- private static readonly string[] INVARIANT_ABBREVIATED_DAY_NAMES
- = new string[7] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
- private static readonly string[] INVARIANT_DAY_NAMES
- = new string[7] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
- private static readonly string[] INVARIANT_ABBREVIATED_MONTH_NAMES
- = new string[13] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""};
- private static readonly string[] INVARIANT_MONTH_NAMES
- = new string[13] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ""};
- public static DateTimeFormatInfo theInvariantDateTimeFormatInfo;
- private bool readOnly;
-
- private string _AMDesignator;
-
- private string _PMDesignator;
-
- private string _DateSeparator;
-
- private string _TimeSeparator;
-
- private string _ShortDatePattern;
-
- private string _LongDatePattern;
-
- private string _ShortTimePattern;
-
- private string _LongTimePattern;
-
- private string _MonthDayPattern;
-
- private string _YearMonthPattern;
-
- private string _FullDateTimePattern;
-
- private string _RFC1123Pattern;
-
- private string _SortableDateTimePattern;
-
- private string _UniversalSortableDateTimePattern;
-
- private DayOfWeek _FirstDayOfWeek;
-
- private Calendar _Calendar;
-
- private CalendarWeekRule _CalendarWeekRule;
-
- private string[] _AbbreviatedDayNames;
-
- private string[] _DayNames;
-
- private string[] _MonthNames;
-
- private string[] _AbbreviatedMonthNames;
-
- public DateTimeFormatInfo()
-
- {
-
- readOnly = false;
-
- _AMDesignator = "AM";
-
- _PMDesignator = "PM";
-
- _DateSeparator = "/";
-
- _TimeSeparator = ":";
-
- _ShortDatePattern = "MM/dd/yyyy";
-
- _LongDatePattern = "dddd, dd MMMM yyyy";
-
- _ShortTimePattern = "HH:mm";
-
- _LongTimePattern = "HH:mm:ss";
-
- _MonthDayPattern = "MMMM dd";
-
- _YearMonthPattern = "yyyy MMMM";
-
- _FullDateTimePattern = "dddd, dd MMMM yyyy HH:mm:ss";
- // FIXME for the following three pattern: "The default value of this property is
- //derived from the calendar that is set for CultureInfo.CurrentCulture or the default
- //calendar of CultureInfo.CurrentCulture."
-
- _RFC1123Pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
-
- _SortableDateTimePattern = "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
-
- _UniversalSortableDateTimePattern = "yyyy'-'MM'-'dd HH':'mm':'ss'Z'";
-
- _FirstDayOfWeek = DayOfWeek.Sunday;
-
- _Calendar = new GregorianCalendar();
-
- _CalendarWeekRule = CalendarWeekRule.FirstDay;
-
- _AbbreviatedDayNames = INVARIANT_ABBREVIATED_DAY_NAMES;
-
- _DayNames = INVARIANT_DAY_NAMES;
-
- _AbbreviatedMonthNames = INVARIANT_ABBREVIATED_MONTH_NAMES;
-
- _MonthNames = INVARIANT_MONTH_NAMES;
-
- }
-
-
-
- // LAMESPEC: this is not in ECMA specs
-
- public static DateTimeFormatInfo GetInstance(IFormatProvider provider)
-
- {
-
- if (provider != null) {
-
- DateTimeFormatInfo dtfi;
-
- dtfi = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo));
-
- if (dtfi != null)
-
- return dtfi;
-
- }
-
-
- return CurrentInfo;
-
- }
-
-
- public bool IsReadOnly {
-
- get {
-
- return readOnly;
-
- }
-
- }
-
-
- public static DateTimeFormatInfo ReadOnly(DateTimeFormatInfo dtfi)
-
- {
-
- DateTimeFormatInfo copy = (DateTimeFormatInfo)dtfi.Clone();
-
- copy.readOnly = true;
-
- return copy;
-
- }
-
-
- public object Clone ()
-
- {
-
- DateTimeFormatInfo clone = (DateTimeFormatInfo) MemberwiseClone();
-
- // clone is not read only
-
- clone.readOnly = false;
-
- return clone;
-
- }
-
-
- public object GetFormat(Type formatType)
- {
- return (formatType == GetType()) ? this : null;
- }
- public string GetAbbreviatedEraName(int era)
- {
- if (era < _Calendar.Eras.Length || era >= _Calendar.Eras.Length)
- throw new ArgumentOutOfRangeException();
- notImplemented();
- //FIXME: implement me
- return null;
- }
- public string GetAbbreviatedMonthName(int month)
- {
- if (month < 1 || month > 13) throw new ArgumentOutOfRangeException();
- return _AbbreviatedMonthNames[month-1];
- }
- public int GetEra(string eraName)
- {
- if (eraName == null) throw new ArgumentNullException();
- eraName = eraName.ToUpper();
- notImplemented();
- //FIXME: implement me
- return -1;
- }
- public string GetEraName(int era)
- {
- if (era < _Calendar.Eras.Length || era >= _Calendar.Eras.Length)
- throw new ArgumentOutOfRangeException();
- notImplemented();
- //FIXME: implement me
- return null;
- }
- public string GetMonthName(int month)
- {
- if (month < 1 || month > 13) throw new ArgumentOutOfRangeException();
- return _MonthNames[month-1];
- }
- public string[] AbbreviatedDayNames
- {
- get
- {
- return (string[]) _AbbreviatedDayNames.Clone();
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- if (value.GetLength(0) != 7) throw new ArgumentException(MSG_ARRAYSIZE_DAY);
- _AbbreviatedDayNames = (string[]) value.Clone();
- }
- }
- public string[] AbbreviatedMonthNames
- {
- get
- {
- return (string[]) _AbbreviatedMonthNames.Clone();
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- if (value.GetLength(0) != 13) throw new ArgumentException(MSG_ARRAYSIZE_MONTH);
- _AbbreviatedMonthNames = (string[]) value.Clone();
- }
- }
- public string[] DayNames
- {
- get
- {
- return (string[]) _DayNames.Clone();
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- if (value.GetLength(0) != 7) throw new ArgumentException(MSG_ARRAYSIZE_DAY);
- _DayNames = (string[]) value.Clone();
- }
- }
- public string[] MonthNames
- {
- get
- {
- return (string[]) _MonthNames.Clone();
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- if (value.GetLength(0) != 13) throw new ArgumentException(MSG_ARRAYSIZE_MONTH);
- _MonthNames = (string[]) value.Clone();
- }
- }
- public string AMDesignator
- {
- get
- {
- return _AMDesignator;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _AMDesignator = value;
- }
- }
- public string PMDesignator
- {
- get
- {
- return _PMDesignator;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _PMDesignator = value;
- }
- }
- public string DateSeparator
- {
- get
- {
- return _DateSeparator;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _DateSeparator = value;
- }
- }
- public string TimeSeparator
- {
- get
- {
- return _TimeSeparator;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _TimeSeparator = value;
- }
- }
- public string LongDatePattern
- {
- get
- {
- return _LongDatePattern;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _LongDatePattern = value;
- }
- }
- public string ShortDatePattern
- {
- get
- {
- return _ShortDatePattern;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _ShortDatePattern = value;
- }
- }
- public string ShortTimePattern
- {
- get
- {
- return _ShortTimePattern;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _ShortTimePattern = value;
- }
- }
- public string LongTimePattern
- {
- get
- {
- return _LongTimePattern;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _LongTimePattern = value;
- }
- }
- public string MonthDayPattern
- {
- get
- {
- return _MonthDayPattern;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _MonthDayPattern = value;
- }
- }
- public string YearMonthPattern
- {
- get
- {
- return _YearMonthPattern;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _YearMonthPattern = value;
- }
- }
- public string FullDateTimePattern
- {
- get
- {
- return _FullDateTimePattern;
- }
-
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _FullDateTimePattern = value;
- }
- }
- public static DateTimeFormatInfo CurrentInfo
- {
- get
- {
- return Thread.CurrentThread.CurrentCulture.DateTimeFormat;
- }
- }
- public static DateTimeFormatInfo InvariantInfo
- {
- get
- {
- if (theInvariantDateTimeFormatInfo == null)
- {
- theInvariantDateTimeFormatInfo =
- DateTimeFormatInfo.ReadOnly(new DateTimeFormatInfo());
- }
- return theInvariantDateTimeFormatInfo;
- }
- }
- // LAMESPEC: this is not in ECMA specs
- public DayOfWeek FirstDayOfWeek
- {
- get
- {
- return _FirstDayOfWeek;
- }
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if ((int) value < 0 || (int) value > 6) throw new ArgumentOutOfRangeException();
- _FirstDayOfWeek = value;
- }
- }
- // LAMESPEC: this is not in ECMA specs
- public Calendar Calendar
- {
- get
- {
- return _Calendar;
- }
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- if (value == null) throw new ArgumentNullException();
- _Calendar = value;
- }
- }
- public CalendarWeekRule CalendarWeekRule
- {
- get
- {
- return _CalendarWeekRule;
- }
- set
- {
- if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
- _CalendarWeekRule = value;
- }
- }
- // LAMESPEC: this is not in ECMA specs
- public string RFC1123Pattern
- {
- get
- {
- return _RFC1123Pattern;
- }
- }
- // LAMESPEC: this is not in ECMA specs
- public string SortableDateTimePattern
- {
- get
- {
- return _SortableDateTimePattern;
- }
- }
- // LAMESPEC: this is not in ECMA specs
- public string UniversalSortableDateTimePattern
- {
- get
- {
- return _UniversalSortableDateTimePattern;
- }
- }
- // LAMESPEC: this is not in ECMA specs
- public string[] GetAllDateTimePatterns()
- {
- notImplemented();
- //FIXME: implement me
- return null;
- }
- // LAMESPEC: this is not in ECMA specs
- public string[] GetAllDateTimePatterns(char format)
- {
- notImplemented();
- //FIXME: implement me
- return null;
- }
- // LAMESPEC: this is not in ECMA specs
- public string GetDayName(DayOfWeek dayofweek)
- {
- int index = (int) dayofweek;
- if (index < 0 || index > 6) throw new ArgumentOutOfRangeException();
- return _DayNames[index];
- }
- // LAMESPEC: this is not in ECMA specs
- public string GetAbbreviatedDayName(DayOfWeek dayofweek)
- {
- int index = (int) dayofweek;
- if (index < 0 || index > 6) throw new ArgumentOutOfRangeException();
- return _AbbreviatedDayNames[index];
- }
- private static void notImplemented()
- {
- throw new Exception("Not implemented");
- }
- }
- }
|