DateTimeFormatInfo.cs 12 KB

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