5 // Marcel Narings (marcel@narings.nl)
7 // (C) 2001 Marcel Narings
9 using System.Globalization ;
13 public struct DateTime : IComparable //, IFormattable, IConvertible
17 public static readonly DateTime MaxValue = new DateTime(3155378975999999999L);
18 public static readonly DateTime MinValue = new DateTime(0L);
29 public DateTime (long newticks)
33 if ( newticks < MinValue.ticks || newticks > MaxValue.ticks)
34 throw new ArgumentOutOfRangeException ();
37 public DateTime (int year, int month, int day)
38 : this (year, month, day,0,0,0,0) {}
40 public DateTime (int year, int month, int day, int hour, int minute, int second)
41 : this (year, month, day, hour, minute, second, 0) {}
43 public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond)
45 if ( year < 1 || year > 9999 ||
46 month < 1 || month >12 ||
47 day < 1 || day > DaysInMonth(year, month) ||
48 hour < 0 || hour > 23 ||
49 minute < 0 || minute > 59 ||
50 second < 0 || second > 59 )
51 throw new ArgumentOutOfRangeException() ;
53 ticks = AbsoluteDays(year,month,day) * TimeSpan.TicksPerDay +
54 hour * TimeSpan.TicksPerHour +
55 minute * TimeSpan.TicksPerMinute +
56 second * TimeSpan.TicksPerSecond +
57 millisecond * TimeSpan.TicksPerMillisecond ;
59 if (ticks < MinValue.ticks || ticks > MaxValue.ticks )
60 throw new ArgumentException() ;
63 public DateTime (int year, int month, int day, Calendar calendar)
64 : this (year, month, day, 0, 0, 0, 0, calendar) {}
67 public DateTime (int year, int month, int day, int hour, int minute, int second, Calendar calendar)
68 : this (year, month, day, hour, minute, second, 0, calendar) {}
71 public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar)
72 : this(year, month, day, hour, minute, second, millisecond)
74 // TODO implement calendar
84 return new DateTime(ticks - (ticks % TimeSpan.TicksPerDay )) ;
92 return FromTicks(Which.Day);
96 public DayOfWeek DayOfWeek
100 return (DayOfWeek) (((ticks / TimeSpan.TicksPerDay)+1) % 7);
108 return FromTicks(Which.DayYear);
116 return (int) ((ticks % TimeSpan.TicksPerDay) / TimeSpan.TicksPerHour);
120 public int Millisecond
124 return (int) (ticks % TimeSpan.TicksPerSecond / TimeSpan.TicksPerMillisecond);
132 return (int) (ticks % TimeSpan.TicksPerHour / TimeSpan.TicksPerMinute);
140 return FromTicks(Which.Month);
145 //public static DateTime Now {get;}
151 return (int) (ticks % TimeSpan.TicksPerMinute / TimeSpan.TicksPerSecond);
163 public TimeSpan TimeOfDay
167 return new TimeSpan(ticks % TimeSpan.TicksPerDay );
173 //public static DateTime Today {get;}
176 //public static DateTime UtcNow {get;}
182 return FromTicks(Which.Year);
189 public DateTime AddTicks( long t )
191 return new DateTime(ticks + t);
194 // FIXME: Implement me.
195 public DateTime AddDays( double days )
197 return new DateTime (0);
200 // TODO: Implement me.
201 public DateTime AddHours( double hours )
203 return new DateTime (0);
206 // TODO: Implement me.
207 public DateTime AddMilliseconds( double ms )
209 return new DateTime (0);
212 // TODO: Implement me.
213 public DateTime AddMinutes( double minutes )
215 return new DateTime (0);
218 // TODO: Implement me.
219 public DateTime AddMonths( int months )
221 return new DateTime (0);
224 // TODO: Implement me.
225 public DateTime AddSeconds(double seconds )
227 return new DateTime (0);
230 // TODO: Implement me.
231 public DateTime AddYears(int years )
233 return new DateTime (0);
236 public static int Compare( DateTime t1, DateTime t2 )
238 if (t1.ticks < t2.ticks)
240 else if (t1.ticks > t2.ticks)
247 public int CompareTo (object v)
252 if (!(v is System.DateTime))
253 throw new ArgumentException ("Value is not a System.DateTime");
255 return Compare (this , (DateTime) v);
258 public static int DaysInMonth(int year, int month)
260 int[] dayspermonth = new int[13] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
262 if (month < 1 || month >12)
263 throw new ArgumentOutOfRangeException ();
265 if (month == 2 && IsLeapYear(year))
268 return dayspermonth[month];
272 public override bool Equals (object o)
274 if (!(o is System.DateTime))
277 return ((DateTime) o).ticks == ticks;
280 public static new bool Equals(DateTime t1, DateTime t2 )
282 return (t1.ticks == t2.ticks );
285 // TODO: Implement me.
286 public static DateTime FromFileTime(long fileTime)
288 return new DateTime(0);
291 // TODO: Implement me.
292 public static DateTime FromOADate(double d)
294 return new DateTime(0);
297 // TODO: Implement me.
298 //public string[] GetDateTimeFormats();
301 //public string[] GetDateTimeFormats( char format )
303 // TODO: implement me
304 //public string[] GetDateTimeFormats( IFormatProvider provider)
307 //public string[] GetDateTimeFormats(char format,IFormatProvider provider )
310 public override int GetHashCode ()
315 public TypeCode GetTypeCode ()
317 return TypeCode.DateTime;
320 public static bool IsLeapYear(int year)
322 return ( !(year %4 > 0 ) && (year %100 > 0) || !(year %400 > 0) ) ;
325 public static DateTime Parse (string s)
327 // TODO: Implement me
328 return new DateTime (0);
331 public static DateTime Parse (string s, IFormatProvider fp)
333 // TODO: Implement me
334 return new DateTime (0);
337 public static DateTime Parse (string s, NumberStyles style, IFormatProvider fp)
339 // TODO: Implement me
340 return new DateTime (0);
343 public static DateTime ParseExact(string s, string format, IFormatProvider provider )
345 // TODO: Implement me
346 return new DateTime (0);
349 public static DateTime ParseExact( string s, string format, IFormatProvider provider, DateTimeStyles style )
351 // TODO: Implement me
352 return new DateTime (0);
356 public static DateTime ParseExact( string s, string[] formats, IFormatProvider provider, DateTimeStyles style )
358 // TODO: Implement me
359 return new DateTime (0);
363 public TimeSpan Subtract(DateTime dt )
364 { //TODO : implement me
365 return new TimeSpan(ticks - dt.ticks );
368 public DateTime Subtract(TimeSpan ts)
369 { // TODO : implement me
370 return new DateTime(ticks - ts.Ticks );
373 public long ToFileTime()
375 // TODO: Implement me
379 public DateTime ToLocalTime()
382 return new DateTime (0);
385 public string ToLongDateString()
391 public string ToLongTimeString()
397 public double ToOADate()
403 public string ToShortDateString()
409 public string ToShortTimeString()
416 public override string ToString ()
418 // TODO: Implement me
423 public string ToString (IFormatProvider fp)
425 // TODO: Implement me.
429 public string ToString (string format)
431 // TODO: Implement me.
435 public string ToString (string format, IFormatProvider fp)
437 // TODO: Implement me.
441 public DateTime ToUniversalTime()
443 // TODO: implement me
444 return new DateTime(0);
449 public static DateTime operator +( DateTime d, TimeSpan t )
451 return new DateTime (d.ticks + t.Ticks );
454 public static bool operator ==( DateTime d1, DateTime d2 )
456 return (d1.ticks == d2.ticks );
459 public static bool operator >(DateTime t1,DateTime t2)
461 return (t1.ticks > t2.ticks );
464 public static bool operator >=( DateTime t1,DateTime t2 )
466 return (t1.ticks >= t2.ticks);
469 public static bool operator !=( DateTime d1, DateTime d2)
471 return (d1.ticks != d2.ticks );
474 public static bool operator <( DateTime t1, DateTime t2 )
476 return (t1.ticks < t2.ticks );
479 public static bool operator <=( DateTime t1,DateTime t2 )
481 return (t1.ticks <= t2.ticks );
484 public static TimeSpan operator -(DateTime d1,DateTime d2)
486 return new TimeSpan(d1.ticks - d2.ticks );
489 public static DateTime operator -(DateTime d,TimeSpan t )
491 return new DateTime (d.ticks - t.Ticks );
496 private static long AbsoluteDays (int year, int month, int day)
498 int[] days = new int[13] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
501 if (IsLeapYear(year))
505 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
509 private int FromTicks(Which what)
511 const int dp400 = 146097;
512 const int dp100 = 36524;
513 const int dp4 = 1461;
515 int[] days = new int[13] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
516 int totaldays = (int) (ticks / TimeSpan.TicksPerDay );
518 int num400 = (totaldays / dp400);
519 totaldays -= num400 * dp400;
521 int num100 = (totaldays / dp100);
522 if (num100 == 4) // leap
524 totaldays -= (num100 * dp100);
526 int num4 = totaldays / dp4;
527 totaldays -= (num4 * dp4);
529 int numyears = totaldays / 365 ;
531 if (numyears == 4) //leap
533 if (what == Which.Year )
534 return num400*400 + num100*100 + num4*4 + numyears + 1;
536 totaldays -= (numyears * 365) ;
537 if (what ==Which.DayYear )
538 return totaldays + 1;
540 if ((numyears==3) && ((num100 == 3) || !(num4 == 24)) ) //31 dec leapyear
545 while (totaldays >= days[M])
546 totaldays -= days[M++];
548 if (what == Which.Month )
558 public enum DayOfWeek