1 // System.Globalization.DateTimeFormatInfo
3 // Some useful functions are missing in the ECMA specs.
4 // They have been added following MS SDK Beta2
6 // Martin Weindel (martin.weindel@t-online.de)
8 // (C) Martin Weindel (martin.weindel@t-online.de)
11 using System.Threading;
13 namespace System.Globalization
16 public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider {
17 private static readonly string MSG_READONLY = "This instance is read only";
18 private static readonly string MSG_ARRAYSIZE_MONTH = "An array with exactly 13 elements is needed";
19 private static readonly string MSG_ARRAYSIZE_DAY = "An array with exactly 7 elements is needed";
20 private static readonly string[] INVARIANT_ABBREVIATED_DAY_NAMES
21 = new string[7] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
22 private static readonly string[] INVARIANT_DAY_NAMES
23 = new string[7] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
24 private static readonly string[] INVARIANT_ABBREVIATED_MONTH_NAMES
25 = new string[13] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""};
26 private static readonly string[] INVARIANT_MONTH_NAMES
27 = new string[13] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ""};
28 private static readonly string[] INVARIANT_ERA_NAMES = {"A.D."};
30 internal static DateTimeFormatInfo theInvariantDateTimeFormatInfo;
32 private bool readOnly;
33 private string _AMDesignator;
34 private string _PMDesignator;
35 private string _DateSeparator;
36 private string _TimeSeparator;
37 private string _ShortDatePattern;
38 private string _LongDatePattern;
39 private string _ShortTimePattern;
40 private string _LongTimePattern;
41 private string _MonthDayPattern;
42 private string _YearMonthPattern;
43 private string _FullDateTimePattern;
44 private string _RFC1123Pattern;
45 private string _SortableDateTimePattern;
46 private string _UniversalSortableDateTimePattern;
47 private DayOfWeek _FirstDayOfWeek;
48 private Calendar _Calendar;
49 private CalendarWeekRule _CalendarWeekRule;
50 private string[] _AbbreviatedDayNames;
51 private string[] _DayNames;
52 private string[] _MonthNames;
53 private string[] _AbbreviatedMonthNames;
55 public DateTimeFormatInfo()
62 _ShortDatePattern = "MM/dd/yyyy";
63 _LongDatePattern = "dddd, dd MMMM yyyy";
64 _ShortTimePattern = "HH:mm";
65 _LongTimePattern = "HH:mm:ss";
66 _MonthDayPattern = "MMMM dd";
67 _YearMonthPattern = "yyyy MMMM";
68 _FullDateTimePattern = "dddd, dd MMMM yyyy HH:mm:ss";
70 // FIXME for the following three pattern: "The
71 // default value of this property is derived
72 // from the calendar that is set for
73 // CultureInfo.CurrentCulture or the default
74 // calendar of CultureInfo.CurrentCulture."
76 _RFC1123Pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
77 _SortableDateTimePattern = "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
78 _UniversalSortableDateTimePattern = "yyyy'-'MM'-'dd HH':'mm':'ss'Z'";
80 _FirstDayOfWeek = DayOfWeek.Sunday;
81 _Calendar = new GregorianCalendar();
82 _CalendarWeekRule = CalendarWeekRule.FirstDay;
84 _AbbreviatedDayNames = INVARIANT_ABBREVIATED_DAY_NAMES;
85 _DayNames = INVARIANT_DAY_NAMES;
86 _AbbreviatedMonthNames = INVARIANT_ABBREVIATED_MONTH_NAMES;
87 _MonthNames = INVARIANT_MONTH_NAMES;
90 // LAMESPEC: this is not in ECMA specs
91 public static DateTimeFormatInfo GetInstance(IFormatProvider provider)
93 if (provider != null) {
94 DateTimeFormatInfo dtfi;
95 dtfi = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo));
103 public bool IsReadOnly {
109 public static DateTimeFormatInfo ReadOnly(DateTimeFormatInfo dtfi)
111 DateTimeFormatInfo copy = (DateTimeFormatInfo)dtfi.Clone();
112 copy.readOnly = true;
116 public object Clone ()
118 DateTimeFormatInfo clone = (DateTimeFormatInfo) MemberwiseClone();
119 // clone is not read only
120 clone.readOnly = false;
124 public object GetFormat(Type formatType)
126 return (formatType == GetType()) ? this : null;
130 public string GetAbbreviatedEraName(int era)
132 if (era < _Calendar.Eras.Length || era >= _Calendar.Eras.Length)
133 throw new ArgumentOutOfRangeException();
135 //FIXME: implement me
139 public string GetAbbreviatedMonthName(int month)
141 if (month < 1 || month > 13) throw new ArgumentOutOfRangeException();
142 return _AbbreviatedMonthNames[month-1];
146 public int GetEra(string eraName)
148 if (eraName == null) throw new ArgumentNullException();
149 eraName = eraName.ToUpper();
151 //FIXME: implement me
156 public string GetEraName(int era)
158 // if (era < _Calendar.Eras.Length || era >= _Calendar.Eras.Length)
159 // throw new ArgumentOutOfRangeException();
161 return INVARIANT_ERA_NAMES[era - 1];
164 //FIXME: implement me
170 public string GetMonthName(int month)
172 if (month < 1 || month > 13) throw new ArgumentOutOfRangeException();
173 return _MonthNames[month-1];
176 public string[] AbbreviatedDayNames
180 return (string[]) _AbbreviatedDayNames.Clone();
184 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
185 if (value == null) throw new ArgumentNullException();
186 if (value.GetLength(0) != 7) throw new ArgumentException(MSG_ARRAYSIZE_DAY);
187 _AbbreviatedDayNames = (string[]) value.Clone();
191 public string[] AbbreviatedMonthNames
195 return (string[]) _AbbreviatedMonthNames.Clone();
199 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
200 if (value == null) throw new ArgumentNullException();
201 if (value.GetLength(0) != 13) throw new ArgumentException(MSG_ARRAYSIZE_MONTH);
202 _AbbreviatedMonthNames = (string[]) value.Clone();
206 public string[] DayNames
210 return (string[]) _DayNames.Clone();
214 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
215 if (value == null) throw new ArgumentNullException();
216 if (value.GetLength(0) != 7) throw new ArgumentException(MSG_ARRAYSIZE_DAY);
217 _DayNames = (string[]) value.Clone();
221 public string[] MonthNames
225 return (string[]) _MonthNames.Clone();
229 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
230 if (value == null) throw new ArgumentNullException();
231 if (value.GetLength(0) != 13) throw new ArgumentException(MSG_ARRAYSIZE_MONTH);
232 _MonthNames = (string[]) value.Clone();
236 public string AMDesignator
240 return _AMDesignator;
244 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
245 if (value == null) throw new ArgumentNullException();
246 _AMDesignator = value;
250 public string PMDesignator
254 return _PMDesignator;
258 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
259 if (value == null) throw new ArgumentNullException();
260 _PMDesignator = value;
264 public string DateSeparator
268 return _DateSeparator;
272 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
273 if (value == null) throw new ArgumentNullException();
274 _DateSeparator = value;
278 public string TimeSeparator
282 return _TimeSeparator;
286 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
287 if (value == null) throw new ArgumentNullException();
288 _TimeSeparator = value;
292 public string LongDatePattern
296 return _LongDatePattern;
300 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
301 if (value == null) throw new ArgumentNullException();
302 _LongDatePattern = value;
306 public string ShortDatePattern
310 return _ShortDatePattern;
314 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
315 if (value == null) throw new ArgumentNullException();
316 _ShortDatePattern = value;
320 public string ShortTimePattern
324 return _ShortTimePattern;
328 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
329 if (value == null) throw new ArgumentNullException();
330 _ShortTimePattern = value;
334 public string LongTimePattern
338 return _LongTimePattern;
342 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
343 if (value == null) throw new ArgumentNullException();
344 _LongTimePattern = value;
348 public string MonthDayPattern
352 return _MonthDayPattern;
356 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
357 if (value == null) throw new ArgumentNullException();
358 _MonthDayPattern = value;
362 public string YearMonthPattern
366 return _YearMonthPattern;
370 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
371 if (value == null) throw new ArgumentNullException();
372 _YearMonthPattern = value;
376 public string FullDateTimePattern
380 if(_FullDateTimePattern!=null) {
381 return _FullDateTimePattern;
383 return(_LongDatePattern + " " + _LongTimePattern);
388 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
389 if (value == null) throw new ArgumentNullException();
390 _FullDateTimePattern = value;
394 public static DateTimeFormatInfo CurrentInfo
398 return Thread.CurrentThread.CurrentCulture.DateTimeFormat;
402 public static DateTimeFormatInfo InvariantInfo
406 if (theInvariantDateTimeFormatInfo == null)
408 theInvariantDateTimeFormatInfo =
409 DateTimeFormatInfo.ReadOnly(new DateTimeFormatInfo());
411 return theInvariantDateTimeFormatInfo;
415 // LAMESPEC: this is not in ECMA specs
416 public DayOfWeek FirstDayOfWeek
420 return _FirstDayOfWeek;
424 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
425 if ((int) value < 0 || (int) value > 6) throw new ArgumentOutOfRangeException();
426 _FirstDayOfWeek = value;
430 // LAMESPEC: this is not in ECMA specs
431 public Calendar Calendar
439 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
440 if (value == null) throw new ArgumentNullException();
445 public CalendarWeekRule CalendarWeekRule
449 return _CalendarWeekRule;
453 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);
454 _CalendarWeekRule = value;
458 // LAMESPEC: this is not in ECMA specs
459 public string RFC1123Pattern
463 return _RFC1123Pattern;
467 // LAMESPEC: this is not in ECMA specs
468 public string SortableDateTimePattern
472 return _SortableDateTimePattern;
476 // LAMESPEC: this is not in ECMA specs
477 public string UniversalSortableDateTimePattern
481 return _UniversalSortableDateTimePattern;
485 // LAMESPEC: this is not in ECMA specs
487 public string[] GetAllDateTimePatterns()
490 //FIXME: implement me
494 // LAMESPEC: this is not in ECMA specs
496 public string[] GetAllDateTimePatterns(char format)
499 //FIXME: implement me
503 // LAMESPEC: this is not in ECMA specs
504 public string GetDayName(DayOfWeek dayofweek)
506 int index = (int) dayofweek;
507 if (index < 0 || index > 6) throw new ArgumentOutOfRangeException();
508 return _DayNames[index];
511 // LAMESPEC: this is not in ECMA specs
512 public string GetAbbreviatedDayName(DayOfWeek dayofweek)
514 int index = (int) dayofweek;
515 if (index < 0 || index > 6) throw new ArgumentOutOfRangeException();
516 return _AbbreviatedDayNames[index];
519 private static void notImplemented()
521 throw new Exception("Not implemented");