2002-04-13 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Globalization / DateTimeFormatInfo.cs
1 //
2 // System.Globalization.DateTimeFormatInfo
3 //
4 // Some useful functions are missing in the ECMA specs.
5 // They have been added following MS SDK Beta2
6 //
7 // Martin Weindel (martin.weindel@t-online.de)
8 //
9 // (C) Martin Weindel (martin.weindel@t-online.de)
10
11 using System;
12 using System.Threading;
13
14 namespace System.Globalization \r
15 {
16         [Serializable]
17         public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider {
18                 private static readonly string MSG_READONLY = "This instance is read only";\r
19                 private static readonly string MSG_ARRAYSIZE_MONTH = "An array with exactly 13 elements is needed";\r
20                 private static readonly string MSG_ARRAYSIZE_DAY = "An array with exactly 7 elements is needed";\r
21                 private static readonly string[] INVARIANT_ABBREVIATED_DAY_NAMES\r
22                         = new string[7] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};\r
23                 private static readonly string[] INVARIANT_DAY_NAMES\r
24                         = new string[7] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};\r
25                 private static readonly string[] INVARIANT_ABBREVIATED_MONTH_NAMES\r
26                         = new string[13] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""};\r
27                 private static readonly string[] INVARIANT_MONTH_NAMES\r
28                         = new string[13] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ""};\r
29 \r
30                 public static DateTimeFormatInfo theInvariantDateTimeFormatInfo;
31 \r
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;
54
55                 public DateTimeFormatInfo()
56                 {
57                         readOnly = false;
58                         _AMDesignator = "AM";
59                         _PMDesignator = "PM";
60                         _DateSeparator = "/";
61                         _TimeSeparator = ":";
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";
69
70                         // 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."
71                         _RFC1123Pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"; 
72                         _SortableDateTimePattern = "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
73                         _UniversalSortableDateTimePattern = "yyyy'-'MM'-'dd HH':'mm':'ss'Z'";
74
75                         _FirstDayOfWeek = DayOfWeek.Sunday;
76                         _Calendar = new GregorianCalendar();
77                         _CalendarWeekRule = CalendarWeekRule.FirstDay;
78
79                         _AbbreviatedDayNames = INVARIANT_ABBREVIATED_DAY_NAMES;
80                         _DayNames = INVARIANT_DAY_NAMES;
81                         _AbbreviatedMonthNames = INVARIANT_ABBREVIATED_MONTH_NAMES;
82                         _MonthNames = INVARIANT_MONTH_NAMES;
83                 }
84                                 
85                 // LAMESPEC: this is not in ECMA specs
86                 public static DateTimeFormatInfo GetInstance(IFormatProvider provider)
87                 {
88                         if (provider != null) {
89                                 DateTimeFormatInfo dtfi;
90                                 dtfi = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo));
91                                 if (dtfi != null)
92                                         return dtfi;
93                         }
94                         
95                         return CurrentInfo;
96                 }
97
98                 public bool IsReadOnly {
99                         get {
100                                 return readOnly;
101                         }
102                 }
103
104                 public static DateTimeFormatInfo ReadOnly(DateTimeFormatInfo dtfi)
105                 {
106                         DateTimeFormatInfo copy = (DateTimeFormatInfo)dtfi.Clone();
107                         copy.readOnly = true;
108                         return copy;
109                 }                       
110
111                 public object Clone () 
112                 {
113                         DateTimeFormatInfo clone = (DateTimeFormatInfo) MemberwiseClone();
114                         // clone is not read only
115                         clone.readOnly = false;
116                         return clone;
117                 }
118
119                 public object GetFormat(Type formatType)\r
120                 {\r
121                         return (formatType == GetType()) ? this : null;\r
122                 }\r
123 \r
124                 public string GetAbbreviatedEraName(int era) \r
125                 {\r
126                         if (era < _Calendar.Eras.Length || era >= _Calendar.Eras.Length)\r
127                                 throw new ArgumentOutOfRangeException();\r
128                         notImplemented();\r
129                         //FIXME: implement me\r
130                         return null;\r
131                 }\r
132 \r
133                 public string GetAbbreviatedMonthName(int month) \r
134                 {\r
135                         if (month < 1 || month > 13) throw new ArgumentOutOfRangeException();\r
136                         return _AbbreviatedMonthNames[month-1];\r
137                 }\r
138 \r
139                 public int GetEra(string eraName)\r
140                 {\r
141                         if (eraName == null) throw new ArgumentNullException();\r
142                         eraName = eraName.ToUpper();\r
143                         notImplemented();\r
144                         //FIXME: implement me\r
145                         return -1;\r
146                 }\r
147 \r
148                 public string GetEraName(int era)\r
149                 {\r
150                         if (era < _Calendar.Eras.Length || era >= _Calendar.Eras.Length)\r
151                                 throw new ArgumentOutOfRangeException();\r
152                         notImplemented();\r
153                         //FIXME: implement me\r
154                         return null;\r
155                 }\r
156 \r
157                 public string GetMonthName(int month)\r
158                 {\r
159                         if (month < 1 || month > 13) throw new ArgumentOutOfRangeException();\r
160                         return _MonthNames[month-1];\r
161                 }\r
162 \r
163                 public string[] AbbreviatedDayNames\r
164                 {\r
165                         get \r
166                         {\r
167                                 return (string[]) _AbbreviatedDayNames.Clone();\r
168                         }\r
169                         \r
170                         set\r
171                         {\r
172                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
173                                 if (value == null) throw new ArgumentNullException();\r
174                                 if (value.GetLength(0) != 7) throw new ArgumentException(MSG_ARRAYSIZE_DAY);\r
175                                 _AbbreviatedDayNames = (string[]) value.Clone();\r
176                         }\r
177                 } \r
178 \r
179                 public string[] AbbreviatedMonthNames\r
180                 {\r
181                         get \r
182                         {\r
183                                 return (string[]) _AbbreviatedMonthNames.Clone();\r
184                         }\r
185                         \r
186                         set\r
187                         {\r
188                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
189                                 if (value == null) throw new ArgumentNullException();\r
190                                 if (value.GetLength(0) != 13) throw new ArgumentException(MSG_ARRAYSIZE_MONTH);\r
191                                 _AbbreviatedMonthNames = (string[]) value.Clone();\r
192                         }\r
193                 } \r
194 \r
195                 public string[] DayNames\r
196                 {\r
197                         get \r
198                         {\r
199                                 return (string[]) _DayNames.Clone();\r
200                         }\r
201                         \r
202                         set\r
203                         {\r
204                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
205                                 if (value == null) throw new ArgumentNullException();\r
206                                 if (value.GetLength(0) != 7) throw new ArgumentException(MSG_ARRAYSIZE_DAY);\r
207                                 _DayNames = (string[]) value.Clone();\r
208                         }\r
209                 } \r
210 \r
211                 public string[] MonthNames\r
212                 {\r
213                         get \r
214                         {\r
215                                 return (string[]) _MonthNames.Clone();\r
216                         }\r
217                         \r
218                         set\r
219                         {\r
220                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
221                                 if (value == null) throw new ArgumentNullException();\r
222                                 if (value.GetLength(0) != 13) throw new ArgumentException(MSG_ARRAYSIZE_MONTH);\r
223                                 _MonthNames = (string[]) value.Clone();\r
224                         }\r
225                 } \r
226 \r
227                 public string AMDesignator\r
228                 {\r
229                         get \r
230                         {\r
231                                 return _AMDesignator;\r
232                         }\r
233                         \r
234                         set\r
235                         {\r
236                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
237                                 if (value == null) throw new ArgumentNullException();\r
238                                 _AMDesignator = value;\r
239                         }\r
240                 } \r
241 \r
242                 public string PMDesignator\r
243                 {\r
244                         get \r
245                         {\r
246                                 return _PMDesignator;\r
247                         }\r
248                         \r
249                         set\r
250                         {\r
251                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
252                                 if (value == null) throw new ArgumentNullException();\r
253                                 _PMDesignator = value;\r
254                         }\r
255                 } \r
256 \r
257                 public string DateSeparator\r
258                 {\r
259                         get \r
260                         {\r
261                                 return _DateSeparator;\r
262                         }\r
263                         \r
264                         set\r
265                         {\r
266                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
267                                 if (value == null) throw new ArgumentNullException();\r
268                                 _DateSeparator = value;\r
269                         }\r
270                 } \r
271 \r
272                 public string TimeSeparator\r
273                 {\r
274                         get \r
275                         {\r
276                                 return _TimeSeparator;\r
277                         }\r
278                         \r
279                         set\r
280                         {\r
281                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
282                                 if (value == null) throw new ArgumentNullException();\r
283                                 _TimeSeparator = value;\r
284                         }\r
285                 } \r
286 \r
287                 public string LongDatePattern\r
288                 {\r
289                         get \r
290                         {\r
291                                 return _LongDatePattern;\r
292                         }\r
293                         \r
294                         set\r
295                         {\r
296                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
297                                 if (value == null) throw new ArgumentNullException();\r
298                                 _LongDatePattern = value;\r
299                         }\r
300                 } \r
301 \r
302                 public string ShortDatePattern\r
303                 {\r
304                         get \r
305                         {\r
306                                 return _ShortDatePattern;\r
307                         }\r
308                         \r
309                         set\r
310                         {\r
311                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
312                                 if (value == null) throw new ArgumentNullException();\r
313                                 _ShortDatePattern = value;\r
314                         }\r
315                 } \r
316 \r
317                 public string ShortTimePattern\r
318                 {\r
319                         get \r
320                         {\r
321                                 return _ShortTimePattern;\r
322                         }\r
323                         \r
324                         set\r
325                         {\r
326                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
327                                 if (value == null) throw new ArgumentNullException();\r
328                                 _ShortTimePattern = value;\r
329                         }\r
330                 } \r
331 \r
332                 public string LongTimePattern\r
333                 {\r
334                         get \r
335                         {\r
336                                 return _LongTimePattern;\r
337                         }\r
338                         \r
339                         set\r
340                         {\r
341                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
342                                 if (value == null) throw new ArgumentNullException();\r
343                                 _LongTimePattern = value;\r
344                         }\r
345                 } \r
346 \r
347                 public string MonthDayPattern \r
348                 {\r
349                         get \r
350                         {\r
351                                 return _MonthDayPattern;\r
352                         }\r
353                         \r
354                         set\r
355                         {\r
356                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
357                                 if (value == null) throw new ArgumentNullException();\r
358                                 _MonthDayPattern = value;\r
359                         }\r
360                 } \r
361 \r
362                 public string YearMonthPattern \r
363                 {\r
364                         get \r
365                         {\r
366                                 return _YearMonthPattern;\r
367                         }\r
368                         \r
369                         set\r
370                         {\r
371                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
372                                 if (value == null) throw new ArgumentNullException();\r
373                                 _YearMonthPattern = value;\r
374                         }\r
375                 } \r
376 \r
377                 public string FullDateTimePattern\r
378                 {\r
379                         get \r
380                         {\r
381                                 return _FullDateTimePattern;\r
382                         }\r
383                         \r
384                         set\r
385                         {\r
386                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
387                                 if (value == null) throw new ArgumentNullException();\r
388                                 _FullDateTimePattern = value;\r
389                         }\r
390                 } \r
391 \r
392                 public static DateTimeFormatInfo CurrentInfo\r
393                 {\r
394                         get\r
395                         {\r
396                                 return Thread.CurrentThread.CurrentCulture.DateTimeFormat;\r
397                         }\r
398                 } \r
399 \r
400                 public static DateTimeFormatInfo InvariantInfo\r
401                 {\r
402                         get\r
403                         {\r
404                                 if (theInvariantDateTimeFormatInfo == null)\r
405                                 {\r
406                                         theInvariantDateTimeFormatInfo = \r
407                                                 DateTimeFormatInfo.ReadOnly(new DateTimeFormatInfo());\r
408                                 }\r
409                                 return theInvariantDateTimeFormatInfo;\r
410                         }\r
411                 } \r
412 \r
413                 // LAMESPEC: this is not in ECMA specs\r
414                 public DayOfWeek FirstDayOfWeek\r
415                 {\r
416                         get\r
417                         {\r
418                                 return _FirstDayOfWeek;\r
419                         }\r
420                         set\r
421                         {\r
422                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
423                                 if ((int) value < 0 || (int) value > 6) throw new ArgumentOutOfRangeException();\r
424                                 _FirstDayOfWeek = value;\r
425                         }\r
426                 }\r
427 \r
428                 // LAMESPEC: this is not in ECMA specs\r
429                 public Calendar Calendar \r
430                 {\r
431                         get\r
432                         {\r
433                                 return _Calendar;\r
434                         }\r
435                         set\r
436                         {\r
437                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
438                                 if (value == null) throw new ArgumentNullException();\r
439                                 _Calendar = value;\r
440                         }\r
441                 }\r
442 \r
443                 public CalendarWeekRule CalendarWeekRule \r
444                 {\r
445                         get\r
446                         {\r
447                                 return _CalendarWeekRule;\r
448                         }\r
449                         set\r
450                         {\r
451                                 if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY);\r
452                                 _CalendarWeekRule = value;\r
453                         }\r
454                 }\r
455 \r
456                 // LAMESPEC: this is not in ECMA specs\r
457                 public string RFC1123Pattern\r
458                 {\r
459                         get\r
460                         {\r
461                                 return _RFC1123Pattern;\r
462                         }\r
463                 }\r
464 \r
465                 // LAMESPEC: this is not in ECMA specs\r
466                 public string SortableDateTimePattern\r
467                 {\r
468                         get\r
469                         {\r
470                                 return _SortableDateTimePattern;\r
471                         }\r
472                 }\r
473 \r
474                 // LAMESPEC: this is not in ECMA specs\r
475                 public string UniversalSortableDateTimePattern\r
476                 {\r
477                         get\r
478                         {\r
479                                 return _UniversalSortableDateTimePattern;\r
480                         }\r
481                 }\r
482 \r
483                 // LAMESPEC: this is not in ECMA specs\r
484                 public string[] GetAllDateTimePatterns()\r
485                 {\r
486                         notImplemented();\r
487                         //FIXME: implement me\r
488                         return null;\r
489                 }\r
490 \r
491                 // LAMESPEC: this is not in ECMA specs\r
492                 public string[] GetAllDateTimePatterns(char format)\r
493                 {\r
494                         notImplemented();\r
495                         //FIXME: implement me\r
496                         return null;\r
497                 }\r
498 \r
499                 // LAMESPEC: this is not in ECMA specs\r
500                 public string GetDayName(DayOfWeek dayofweek)\r
501                 {\r
502                         int index = (int) dayofweek;\r
503                         if (index < 0 || index > 6) throw new ArgumentOutOfRangeException();\r
504                         return _DayNames[index];\r
505                 }\r
506 \r
507                 // LAMESPEC: this is not in ECMA specs\r
508                 public string GetAbbreviatedDayName(DayOfWeek dayofweek)\r
509                 {\r
510                         int index = (int) dayofweek;\r
511                         if (index < 0 || index > 6) throw new ArgumentOutOfRangeException();\r
512                         return _AbbreviatedDayNames[index];\r
513                 }\r
514 \r
515                 private static void notImplemented()\r
516                 {\r
517                         throw new Exception("Not implemented");\r
518                 }\r
519         }
520 }