DateTimeFormatInfo.cs 13 KB

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