DateTime.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. //
  2. // System.DateTime.cs
  3. //
  4. // author:
  5. // Marcel Narings ([email protected])
  6. //
  7. // (C) 2001 Marcel Narings
  8. using System;
  9. using System.Globalization ;
  10. namespace System
  11. {
  12. /// <summary>
  13. /// The DateTime structure represents dates and time ranging from
  14. /// 1-1-0001 12:00:00 AM to 31-12-9999 23:59:00 Common Era.
  15. /// </summary>
  16. ///
  17. public struct DateTime : IComparable , IFormattable , IConvertible
  18. {
  19. long ticks;
  20. private const long MaxTicks = 3155378975999999999L;
  21. private const long MinTicks = 0L;
  22. private const int dp400 = 146097;
  23. private const int dp100 = 36524;
  24. private const int dp4 = 1461;
  25. public static readonly DateTime MaxValue = new DateTime (MaxTicks);
  26. public static readonly DateTime MinValue = new DateTime (MinTicks);
  27. private enum Which
  28. {
  29. Day,
  30. DayYear,
  31. Month,
  32. Year
  33. };
  34. private static int[] daysmonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  35. private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  36. private static long AbsoluteDays (int year, int month, int day)
  37. {
  38. int[] days;
  39. int temp = 0, m=1 ;
  40. days = (IsLeapYear(year) ? daysmonthleap : daysmonth);
  41. while (m < month)
  42. temp += days[m++];
  43. return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
  44. }
  45. private int FromTicks(Which what)
  46. {
  47. int num400, num100, num4, numyears;
  48. int M =1;
  49. int[] days = daysmonth;
  50. int totaldays = (int) (ticks / TimeSpan.TicksPerDay);
  51. num400 = (totaldays / dp400);
  52. totaldays -= num400 * dp400;
  53. num100 = (totaldays / dp100);
  54. if (num100 == 4) // leap
  55. num100 = 3;
  56. totaldays -= (num100 * dp100);
  57. num4 = totaldays / dp4;
  58. totaldays -= (num4 * dp4);
  59. numyears = totaldays / 365 ;
  60. if (numyears == 4) //leap
  61. numyears =3 ;
  62. if (what == Which.Year )
  63. return num400*400 + num100*100 + num4*4 + numyears + 1;
  64. totaldays -= (numyears * 365) ;
  65. if (what == Which.DayYear )
  66. return totaldays + 1;
  67. if ((numyears==3) && ((num100 == 3) || !(num4 == 24)) ) //31 dec leapyear
  68. days = daysmonthleap;
  69. while (totaldays >= days[M])
  70. totaldays -= days[M++];
  71. if (what == Which.Month )
  72. return M;
  73. return totaldays +1;
  74. }
  75. // Constructors
  76. /// <summary>
  77. /// Constructs a DateTime for specified ticks
  78. /// </summary>
  79. ///
  80. public DateTime (long newticks)
  81. {
  82. ticks = newticks;
  83. if ( newticks < MinValue.ticks || newticks > MaxValue.ticks)
  84. throw new ArgumentOutOfRangeException ();
  85. }
  86. public DateTime (int year, int month, int day)
  87. : this (year, month, day,0,0,0,0) {}
  88. public DateTime (int year, int month, int day, int hour, int minute, int second)
  89. : this (year, month, day, hour, minute, second, 0) {}
  90. public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond)
  91. {
  92. if ( year < 1 || year > 9999 ||
  93. month < 1 || month >12 ||
  94. day < 1 || day > DaysInMonth(year, month) ||
  95. hour < 0 || hour > 23 ||
  96. minute < 0 || minute > 59 ||
  97. second < 0 || second > 59 )
  98. throw new ArgumentOutOfRangeException() ;
  99. ticks = AbsoluteDays(year,month,day) * TimeSpan.TicksPerDay +
  100. hour * TimeSpan.TicksPerHour +
  101. minute * TimeSpan.TicksPerMinute +
  102. second * TimeSpan.TicksPerSecond +
  103. millisecond * TimeSpan.TicksPerMillisecond ;
  104. if (ticks < MinValue.ticks || ticks > MaxValue.ticks )
  105. throw new ArgumentException() ;
  106. }
  107. public DateTime (int year, int month, int day, Calendar calendar)
  108. : this (year, month, day, 0, 0, 0, 0, calendar) {}
  109. public DateTime (int year, int month, int day, int hour, int minute, int second, Calendar calendar)
  110. : this (year, month, day, hour, minute, second, 0, calendar) {}
  111. public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar)
  112. : this(year, month, day, hour, minute, second, millisecond)
  113. {
  114. if ( calendar == null)
  115. throw new ArgumentNullException();
  116. }
  117. /* Properties */
  118. public DateTime Date
  119. {
  120. get
  121. {
  122. return new DateTime(ticks - (ticks % TimeSpan.TicksPerDay )) ;
  123. }
  124. }
  125. public int Day
  126. {
  127. get
  128. {
  129. return FromTicks(Which.Day);
  130. }
  131. }
  132. public DayOfWeek DayOfWeek
  133. {
  134. get
  135. {
  136. return ( (DayOfWeek) (((ticks / TimeSpan.TicksPerDay)+1) % 7) );
  137. }
  138. }
  139. public int DayOfYear
  140. {
  141. get
  142. {
  143. return FromTicks(Which.DayYear);
  144. }
  145. }
  146. public int Hour
  147. {
  148. get
  149. {
  150. return ( (int) ((ticks % TimeSpan.TicksPerDay) / TimeSpan.TicksPerHour) );
  151. }
  152. }
  153. public int Millisecond
  154. {
  155. get
  156. {
  157. return ( (int) (ticks % TimeSpan.TicksPerSecond / TimeSpan.TicksPerMillisecond) );
  158. }
  159. }
  160. public int Minute
  161. {
  162. get
  163. {
  164. return ( (int) (ticks % TimeSpan.TicksPerHour / TimeSpan.TicksPerMinute) );
  165. }
  166. }
  167. public int Month
  168. {
  169. get
  170. {
  171. return FromTicks(Which.Month);
  172. }
  173. }
  174. // TODO implement me
  175. public static DateTime Now
  176. {
  177. get
  178. {
  179. return new DateTime (0);
  180. }
  181. }
  182. public int Second
  183. {
  184. get
  185. {
  186. return (int) (ticks % TimeSpan.TicksPerMinute / TimeSpan.TicksPerSecond);
  187. }
  188. }
  189. public long Ticks
  190. {
  191. get
  192. {
  193. return ticks ;
  194. }
  195. }
  196. public TimeSpan TimeOfDay
  197. {
  198. get
  199. {
  200. return new TimeSpan(ticks % TimeSpan.TicksPerDay );
  201. }
  202. }
  203. //TODO implement
  204. public static DateTime Today
  205. {
  206. get
  207. {
  208. return new DateTime (0);
  209. }
  210. }
  211. //TODO implement
  212. public static DateTime UtcNow
  213. {
  214. get {
  215. return new DateTime (0);
  216. }
  217. }
  218. public int Year
  219. {
  220. get
  221. {
  222. return FromTicks(Which.Year);
  223. }
  224. }
  225. /* methods */
  226. public DateTime Add (TimeSpan ts)
  227. {
  228. long newticks ;
  229. newticks = ticks + ts.Ticks ;
  230. if (ts.Ticks < MinTicks || ts.Ticks > MaxTicks ||
  231. newticks < MinTicks || newticks > MaxTicks)
  232. throw new ArgumentException ();
  233. return new DateTime (newticks);
  234. }
  235. public DateTime AddDays (double days)
  236. {
  237. return AddMilliseconds (days * 86400000);
  238. }
  239. public DateTime AddTicks (long t)
  240. {
  241. long newticks = ticks + t;
  242. if (t<MinTicks || t>MaxTicks || newticks<MinTicks || newticks>MaxTicks)
  243. throw new ArgumentException ();
  244. return new DateTime(newticks);
  245. }
  246. public DateTime AddHours (double hours)
  247. {
  248. return AddMilliseconds (hours * 3600000);
  249. }
  250. public DateTime AddMilliseconds (double ms)
  251. {
  252. long msticks, newticks;
  253. msticks = (long) (ms += ms > 0 ? 0.5 : -0.5) * TimeSpan.TicksPerMillisecond ;
  254. newticks = ticks + msticks ;
  255. if (msticks < MinTicks || msticks > MaxTicks ||
  256. newticks < MinTicks || newticks > MaxTicks)
  257. throw new ArgumentException ();
  258. return new DateTime (newticks);
  259. }
  260. public DateTime AddMinutes (double minutes)
  261. {
  262. return AddMilliseconds (minutes * 60000);
  263. }
  264. public DateTime AddMonths (int months)
  265. {
  266. int day, month, year, maxday ;
  267. DateTime temp ;
  268. day = this.Day;
  269. month = this.Month + (months % 12);
  270. year = this.Year + months/12 ;
  271. if (month < 1)
  272. {
  273. month = 12 + month ;
  274. year -- ;
  275. }
  276. else if (month>12)
  277. {
  278. month = month -12;
  279. year ++;
  280. }
  281. maxday = DaysInMonth(year, month);
  282. if (day > maxday)
  283. day = maxday;
  284. temp = new DateTime (year, month, day);
  285. return temp.Add (this.TimeOfDay);
  286. }
  287. public DateTime AddSeconds (double seconds)
  288. {
  289. return AddMilliseconds (seconds*1000);
  290. }
  291. public DateTime AddYears (int years )
  292. {
  293. return AddMonths(years * 12);
  294. }
  295. public static int Compare (DateTime t1, DateTime t2)
  296. {
  297. if (t1.ticks < t2.ticks)
  298. return -1;
  299. else if (t1.ticks > t2.ticks)
  300. return 1;
  301. else
  302. return 0;
  303. }
  304. public int CompareTo (object v)
  305. {
  306. if ( v == null)
  307. return 1 ;
  308. if (!(v is System.DateTime))
  309. throw new ArgumentException ("Value is not a System.DateTime");
  310. return Compare (this , (DateTime) v);
  311. }
  312. public static int DaysInMonth (int year, int month)
  313. {
  314. int[] days ;
  315. if (month < 1 || month >12)
  316. throw new ArgumentOutOfRangeException ();
  317. days = (IsLeapYear(year) ? daysmonthleap : daysmonth);
  318. return days[month];
  319. }
  320. public override bool Equals (object o)
  321. {
  322. if (!(o is System.DateTime))
  323. return false;
  324. return ((DateTime) o).ticks == ticks;
  325. }
  326. public static bool Equals (DateTime t1, DateTime t2 )
  327. {
  328. return (t1.ticks == t2.ticks );
  329. }
  330. // TODO: Implement me.
  331. public static DateTime FromFileTime (long fileTime)
  332. {
  333. return new DateTime (0);
  334. }
  335. // TODO: Implement me.
  336. public static DateTime FromOADate (double d)
  337. {
  338. return new DateTime(0);
  339. }
  340. // TODO: Implement me.
  341. public string[] GetDateTimeFormats()
  342. {
  343. return null;
  344. }
  345. //TODO: implement me
  346. public string[] GetDateTimeFormats( char format )
  347. {
  348. return null;
  349. }
  350. // TODO: implement me
  351. public string[] GetDateTimeFormats( IFormatProvider provider)
  352. {
  353. return null;
  354. }
  355. //TODO: implement me
  356. public string[] GetDateTimeFormats(char format,IFormatProvider provider )
  357. {
  358. return null;
  359. }
  360. public override int GetHashCode ()
  361. {
  362. return (int) ticks;
  363. }
  364. public TypeCode GetTypeCode ()
  365. {
  366. return TypeCode.DateTime;
  367. }
  368. public static bool IsLeapYear (int year)
  369. {
  370. return ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ;
  371. }
  372. public static DateTime Parse (string s)
  373. {
  374. // TODO: Implement me
  375. return new DateTime (0);
  376. }
  377. public static DateTime Parse (string s, IFormatProvider fp)
  378. {
  379. // TODO: Implement me
  380. return new DateTime (0);
  381. }
  382. public static DateTime Parse (string s, NumberStyles style, IFormatProvider fp)
  383. {
  384. // TODO: Implement me
  385. return new DateTime (0);
  386. }
  387. public static DateTime ParseExact(string s, string format, IFormatProvider provider )
  388. {
  389. // TODO: Implement me
  390. return new DateTime (0);
  391. }
  392. public static DateTime ParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style )
  393. {
  394. // TODO: Implement me
  395. return new DateTime (0);
  396. }
  397. public static DateTime ParseExact( string s, string[] formats, IFormatProvider provider, DateTimeStyles style )
  398. {
  399. // TODO: Implement me
  400. return new DateTime (0);
  401. }
  402. public TimeSpan Subtract(DateTime dt )
  403. {
  404. return new TimeSpan(ticks - dt.ticks );
  405. }
  406. public DateTime Subtract(TimeSpan ts)
  407. {
  408. return new DateTime(ticks - ts.Ticks );
  409. }
  410. public long ToFileTime()
  411. {
  412. // TODO: Implement me
  413. return 0 ;
  414. }
  415. public DateTime ToLocalTime()
  416. {
  417. // TODO Implement me
  418. return new DateTime (0);
  419. }
  420. public string ToLongDateString()
  421. {
  422. // TODO implement me
  423. return "ToLongDateString";
  424. }
  425. public string ToLongTimeString()
  426. {
  427. // TODO implement me
  428. return "ToLongTimeString";
  429. }
  430. public double ToOADate()
  431. {
  432. // TODO implement me
  433. return 0;
  434. }
  435. public string ToShortDateString()
  436. {
  437. // TODO implement me
  438. return "ToShortDateString";
  439. }
  440. public string ToShortTimeString()
  441. {
  442. // TODO implement me
  443. return "ToShortTimeString";
  444. }
  445. public override string ToString ()
  446. {
  447. // TODO: Implement me
  448. return "" ;
  449. }
  450. public string ToString (IFormatProvider fp)
  451. {
  452. // TODO: Implement me.
  453. return "ToString1";
  454. }
  455. public string ToString (string format)
  456. {
  457. // TODO: Implement me.
  458. return "ToString2";
  459. }
  460. public string ToString (string format, IFormatProvider fp)
  461. {
  462. // TODO: Implement me.
  463. return "" ;
  464. }
  465. public DateTime ToUniversalTime()
  466. {
  467. // TODO: implement me
  468. return new DateTime(0);
  469. }
  470. /* OPERATORS */
  471. public static DateTime operator +(DateTime d, TimeSpan t)
  472. {
  473. return new DateTime (d.ticks + t.Ticks);
  474. }
  475. public static bool operator ==(DateTime d1, DateTime d2)
  476. {
  477. return (d1.ticks == d2.ticks);
  478. }
  479. public static bool operator >(DateTime t1,DateTime t2)
  480. {
  481. return (t1.ticks > t2.ticks);
  482. }
  483. public static bool operator >=(DateTime t1,DateTime t2)
  484. {
  485. return (t1.ticks >= t2.ticks);
  486. }
  487. public static bool operator !=(DateTime d1, DateTime d2)
  488. {
  489. return (d1.ticks != d2.ticks);
  490. }
  491. public static bool operator <(DateTime t1, DateTime t2)
  492. {
  493. return (t1.ticks < t2.ticks );
  494. }
  495. public static bool operator <=(DateTime t1,DateTime t2)
  496. {
  497. return (t1.ticks <= t2.ticks);
  498. }
  499. public static TimeSpan operator -(DateTime d1,DateTime d2)
  500. {
  501. return new TimeSpan(d1.ticks - d2.ticks);
  502. }
  503. public static DateTime operator -(DateTime d,TimeSpan t )
  504. {
  505. return new DateTime (d.ticks - t.Ticks);
  506. }
  507. public bool ToBoolean(IFormatProvider provider)
  508. {
  509. throw new InvalidCastException();
  510. }
  511. public byte ToByte(IFormatProvider provider)
  512. {
  513. throw new InvalidCastException();
  514. }
  515. public char ToChar(IFormatProvider provider)
  516. {
  517. throw new InvalidCastException();
  518. }
  519. // TODO Implement me
  520. public System.DateTime ToDateTime(IFormatProvider provider)
  521. {
  522. return new System.DateTime(this.ticks);
  523. }
  524. public decimal ToDecimal(IFormatProvider provider)
  525. {
  526. throw new InvalidCastException();
  527. }
  528. public double ToDouble(IFormatProvider provider)
  529. {
  530. throw new InvalidCastException();
  531. }
  532. public Int16 ToInt16(IFormatProvider provider)
  533. {
  534. throw new InvalidCastException();
  535. }
  536. public Int32 ToInt32(IFormatProvider provider)
  537. {
  538. throw new InvalidCastException();
  539. }
  540. public Int64 ToInt64(IFormatProvider provider)
  541. {
  542. throw new InvalidCastException();
  543. }
  544. public SByte ToSByte(IFormatProvider provider)
  545. {
  546. throw new InvalidCastException();
  547. }
  548. public Single ToSingle(IFormatProvider provider)
  549. {
  550. throw new InvalidCastException();
  551. }
  552. public object ToType(Type conversionType,IFormatProvider provider)
  553. {
  554. throw new InvalidCastException();
  555. }
  556. UInt16 System.IConvertible.ToUInt16(IFormatProvider provider)
  557. {
  558. throw new InvalidCastException();
  559. }
  560. public UInt32 ToUInt32(IFormatProvider provider)
  561. {
  562. throw new InvalidCastException();
  563. }
  564. public UInt64 ToUInt64(IFormatProvider provider)
  565. {
  566. throw new InvalidCastException();
  567. }
  568. }
  569. }
  570. namespace System
  571. {
  572. public enum DayOfWeek
  573. {
  574. Sunday,
  575. Monday,
  576. Tuesday,
  577. Wednesday,
  578. Thursday,
  579. Friday,
  580. Saturday
  581. }
  582. }