2008-01-15 Stephane Delcroix <sdelcroix@novell.com>
[mono.git] / mcs / class / corlib / System / DateTimeOffset.cs
1 /*
2  * System.DateTimeOffset
3  *
4  * Author(s)
5  *      Stephane Delcroix <stephane@delcroix.org>
6  *      Marek Safar (marek.safar@gmail.com)
7  *
8  *  Copyright (C) 2007 Novell, Inc (http://www.novell.com) 
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  * 
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  * 
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29
30
31 #if NET_2_0 // Introduced by .NET 3.5 for 2.0 mscorlib
32
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Text;
37
38 namespace System
39 {
40         [Serializable]
41         [StructLayout (LayoutKind.Auto)]
42         public struct DateTimeOffset : IComparable, IFormattable, ISerializable, IDeserializationCallback, IComparable<DateTimeOffset>, IEquatable<DateTimeOffset>
43         {
44                 public static readonly DateTimeOffset MaxValue = new DateTimeOffset (DateTime.MaxValue, TimeSpan.Zero);
45                 public static readonly DateTimeOffset MinValue = new DateTimeOffset (DateTime.MinValue, TimeSpan.Zero);
46                 
47                 DateTime dt;
48                 TimeSpan utc_offset;
49         
50                 public DateTimeOffset (DateTime dateTime)
51                 {
52                         dt = dateTime;
53
54                         if (dateTime.Kind == DateTimeKind.Utc)
55                                 utc_offset = TimeSpan.Zero;
56                         else 
57                                 utc_offset = TimeZone.CurrentTimeZone.GetUtcOffset (dateTime);
58                                 
59                         if (UtcDateTime < DateTime.MinValue || UtcDateTime > DateTime.MaxValue)
60                                 throw new ArgumentOutOfRangeException ("The UTC date and time that results from applying the offset is earlier than MinValue or later than MaxValue.");
61
62                 }
63                 
64                 public DateTimeOffset (DateTime dateTime, TimeSpan offset)
65                 {
66                         if (dateTime.Kind == DateTimeKind.Utc && offset != TimeSpan.Zero)
67                                 throw new ArgumentException ("dateTime.Kind equals Utc and offset does not equal zero.");
68
69                         if (dateTime.Kind == DateTimeKind.Local && offset != TimeZone.CurrentTimeZone.GetUtcOffset (dateTime))
70                                 throw new ArgumentException ("dateTime.Kind equals Local and offset does not equal the offset of the system's local time zone.");
71
72                         if (offset.Ticks % TimeSpan.TicksPerMinute != 0)
73                                 throw new ArgumentException ("offset is not specified in whole minutes.");
74
75                         if (offset < new TimeSpan (-14, 0 ,0) || offset > new TimeSpan (14, 0, 0))
76                                 throw new ArgumentOutOfRangeException ("offset is less than -14 hours or greater than 14 hours.");
77
78                         dt = dateTime;
79                         utc_offset = offset;
80
81                         if (UtcDateTime < DateTime.MinValue || UtcDateTime > DateTime.MaxValue)
82                                 throw new ArgumentOutOfRangeException ("The UtcDateTime property is earlier than MinValue or later than MaxValue.");
83                 }
84
85                 public DateTimeOffset (long ticks, TimeSpan offset) : this (new DateTime (ticks), offset)
86                 {
87                 }
88
89                 public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, TimeSpan offset) : 
90                         this (new DateTime (year, month, day, hour, minute, second), offset)
91                 {
92                 }
93
94                 public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset) :
95                         this (new DateTime (year, month, day, hour, minute, second, millisecond), offset)
96                 {
97                 }
98
99                 public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, TimeSpan offset) :
100                         this (new DateTime (year, month, day, hour, minute, second, millisecond, calendar), offset)
101                 {
102                 }
103
104                 public DateTimeOffset Add (TimeSpan timeSpan)
105                 {
106                         return new DateTimeOffset (dt.Add (timeSpan), utc_offset);
107                 }
108         
109                 public DateTimeOffset AddDays (double days)
110                 {
111                         return new DateTimeOffset (dt.AddDays (days), utc_offset);      
112                 }
113                 
114                 public DateTimeOffset AddHours (double hours)
115                 {
116                         return new DateTimeOffset (dt.AddHours (hours), utc_offset);
117                 }
118
119                 public static DateTimeOffset operator + (DateTimeOffset dateTimeTz, TimeSpan timeSpan)
120                 {
121                         return dateTimeTz.Add (timeSpan);
122                 }
123
124                 public DateTimeOffset AddMilliseconds (double milliseconds)
125                 {
126                         return new DateTimeOffset (dt.AddMilliseconds (milliseconds), utc_offset);
127                 }
128
129                 public DateTimeOffset AddMinutes (double minutes)
130                 {
131                         return new DateTimeOffset (dt.AddMinutes (minutes), utc_offset);        
132                 }
133
134                 public DateTimeOffset AddMonths (int months)
135                 {
136                         return new DateTimeOffset (dt.AddMonths (months), utc_offset);
137                 }
138
139                 public DateTimeOffset AddSeconds (double seconds)
140                 {
141                         return new DateTimeOffset (dt.AddSeconds (seconds), utc_offset);
142                 }
143
144                 public DateTimeOffset AddTicks (long ticks)
145                 {
146                         return new DateTimeOffset (dt.AddTicks (ticks), utc_offset);    
147                 }
148
149                 public DateTimeOffset AddYears (int years)
150                 {
151                         return new DateTimeOffset (dt.AddYears (years), utc_offset);
152                 }
153
154                 public static int Compare (DateTimeOffset first, DateTimeOffset second)
155                 {
156                         return first.CompareTo (second);        
157                 }
158
159                 public int CompareTo (DateTimeOffset other)
160                 {
161                         return UtcDateTime.CompareTo (other.UtcDateTime);
162                 }
163
164                 public int CompareTo (object other)
165                 {
166                         return CompareTo ((DateTimeOffset) other);
167                 }
168
169                 public static bool operator == (DateTimeOffset left, DateTimeOffset right)
170                 {
171                         return left.Equals (right);     
172                 }
173
174                 public bool Equals (DateTimeOffset other)
175                 {
176                         return UtcDateTime == other.UtcDateTime;
177                 }
178
179                 public override bool Equals (object other)
180                 {
181                         return UtcDateTime == ((DateTimeOffset) other).UtcDateTime;
182                 }
183
184                 public static bool Equals (DateTimeOffset first, DateTimeOffset second)
185                 {
186                         return first.Equals (second);   
187                 }
188
189                 public bool EqualsExact (DateTimeOffset other)
190                 {
191                         return dt == other.dt && utc_offset == other.utc_offset;        
192                 }
193
194                 public static DateTimeOffset FromFileTime (long fileTime)
195                 {
196                         if (fileTime < 0 || fileTime > MaxValue.Ticks)
197                                 throw new ArgumentOutOfRangeException ("fileTime is less than zero or greater than DateTimeOffset.MaxValue.Ticks.");
198                         
199                         return new DateTimeOffset (DateTime.FromFileTime (fileTime), TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.FromFileTime (fileTime)));
200
201                 }
202
203                 public override int GetHashCode ()
204                 {
205                         return dt.GetHashCode () ^ utc_offset.GetHashCode ();
206                 }
207
208                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
209                 {
210                         if (info == null)
211                                 throw new ArgumentNullException ("info");
212
213                         info.AddValue ("datetime", dt);
214                         info.AddValue ("offset", utc_offset);
215                 }
216
217                 public static bool operator > (DateTimeOffset left, DateTimeOffset right)
218                 {
219                         return left.UtcDateTime > right.UtcDateTime;
220                 }                       
221
222                 public static bool operator >= (DateTimeOffset left, DateTimeOffset right)
223                 {
224                         return left.UtcDateTime >= right.UtcDateTime;
225                 }                       
226
227                 public static implicit operator DateTimeOffset (DateTime dateTime)
228                 {
229                         return new DateTimeOffset (dateTime);
230                 }
231
232                 public static bool operator != (DateTimeOffset left, DateTimeOffset right)
233                 {
234                         return left.UtcDateTime != right.UtcDateTime;
235                 }
236
237                 public static bool operator < (DateTimeOffset left, DateTimeOffset right)
238                 {
239                         return left.UtcDateTime < right.UtcDateTime;
240                 }
241                 
242                 public static bool operator <= (DateTimeOffset left, DateTimeOffset right)
243                 {
244                         return left.UtcDateTime <= right.UtcDateTime;
245                 }
246         
247                 [MonoTODO]
248                 void IDeserializationCallback.OnDeserialization (object sender)
249                 {
250                 }
251
252                 public static DateTimeOffset Parse (string input)
253                 {
254                         return Parse (input, null);
255                 }
256
257                 public static DateTimeOffset Parse (string input, IFormatProvider formatProvider)
258                 {
259                         return Parse (input, formatProvider, DateTimeStyles.AllowWhiteSpaces);
260                 }
261
262                 [MonoTODO]
263                 public static DateTimeOffset Parse (string input, IFormatProvider formatProvider, DateTimeStyles styles)
264                 {
265                         if (input == null)
266                                 throw new ArgumentNullException ("input");
267
268                         throw new NotImplementedException ();
269                 }
270
271                 public static DateTimeOffset ParseExact (string input, string format, IFormatProvider formatProvider)
272                 {
273                         return ParseExact (input, format, formatProvider, DateTimeStyles.AssumeLocal);
274                 }
275
276                 public static DateTimeOffset ParseExact (string input, string format, IFormatProvider formatProvider, DateTimeStyles styles)
277                 {
278                         if (format == null)
279                                 throw new ArgumentNullException ("format");
280
281                         if (format == String.Empty)
282                                 throw new FormatException ("format is an empty string");
283
284                         return ParseExact (input, new string [] {format}, formatProvider, styles);
285                 }
286
287                 public static DateTimeOffset ParseExact (string input, string[] formats, IFormatProvider formatProvider, DateTimeStyles styles)
288                 {
289                         if (input == null)
290                                 throw new ArgumentNullException ("input");
291
292                         if (input == String.Empty)
293                                 throw new FormatException ("input is an empty string");
294
295                         if (formats == null)
296                                 throw new ArgumentNullException ("formats");
297
298                         if (formats.Length == 0)
299                                 throw new FormatException ("Invalid format specifier");
300
301                         if ((styles & DateTimeStyles.AssumeLocal) != 0 && (styles & DateTimeStyles.AssumeUniversal) != 0)
302                                 throw new ArgumentException ("styles parameter contains incompatible flags");
303
304                         DateTimeOffset result;
305                         if (!ParseExact (input, formats, DateTimeFormatInfo.GetInstance (formatProvider), styles, out result))
306                                 throw new FormatException ();
307
308                         return result;
309                 }
310
311                 private static bool ParseExact (string input, string [] formats,
312                                 DateTimeFormatInfo dfi, DateTimeStyles styles, out DateTimeOffset ret)
313                 {
314                         foreach (string format in formats)
315                         {
316                                 if (format == null || format == String.Empty)
317                                         throw new FormatException ("Invlid Format Sting");
318
319                                 DateTimeOffset result;
320                                 if (DoParse (input, format, false, out result, dfi, styles)) {
321                                         ret = result;
322                                         return true;
323                                 }
324                         }
325                         ret = DateTimeOffset.MinValue;
326                         return false;
327                 }
328
329                 private static bool DoParse (string input, 
330                                 string format,
331                                 bool exact,
332                                 out DateTimeOffset result,
333                                 DateTimeFormatInfo dfi,
334                                 DateTimeStyles styles)
335                 {
336                         if ((styles & DateTimeStyles.AllowLeadingWhite) != 0) {
337                                 format = format.TrimStart (null);
338                                 input = input.TrimStart (null);
339                         }
340
341                         if ((styles & DateTimeStyles.AllowTrailingWhite) != 0) {
342                                 format = format.TrimEnd (null);
343                                 input = input.TrimEnd (null);
344                         }
345
346                         bool allow_white_spaces = false;
347                         if ((styles & DateTimeStyles.AllowInnerWhite) != 0)
348                                 allow_white_spaces = true;
349
350                         bool useutc = false, use_invariants = false;
351                         if (format.Length == 1)
352                                 format = DateTimeUtils.GetStandardPattern (format[0], dfi, out useutc, out use_invariants, true);
353                 
354                         int year = -1;
355                         int month = -1;
356                         int day = -1;
357                         int partial_hour = -1; // for 'hh tt' formats
358                         int hour = -1;
359                         int minute = -1;
360                         int temp_int = -1;
361                         TimeSpan offset = TimeSpan.MinValue;
362
363                         result = DateTimeOffset.MinValue;
364
365                         int fi = 0; //format iterator
366                         int ii = 0; //input iterator
367                         while (fi < format.Length) {
368                                 int tokLen;
369                                 char ch = format [fi];
370
371                                 switch (ch) {
372                                 case 'd':
373                                         tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
374                                         if (day != -1 || tokLen > 4)
375                                                 return false;
376
377                                         if (tokLen <= 2)
378                                                 ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out day);
379                                         else
380                                                 ii += ParseEnum (input, ii, tokLen == 3 ? dfi.AbbreviatedDayNames : dfi.DayNames, allow_white_spaces, out temp_int); 
381                                         break;
382                                 case 'h':
383                                         tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
384                                         if (hour != -1 || tokLen > 2)
385                                                 return false;
386
387                                         ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out temp_int);
388                                         if (temp_int == -1)
389                                                 return false;
390
391                                         if (partial_hour == -1)
392                                                 partial_hour = temp_int;
393                                         else 
394                                                 hour = partial_hour + temp_int;
395                                         break;
396                                 case 'H':
397                                         tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
398                                         if (hour != -1 || tokLen > 2)
399                                                 return false;
400
401                                         ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out hour);
402                                         break;
403                                 case 'm':
404                                         tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
405                                         if (minute != -1 || tokLen > 2)
406                                                 return false;
407
408                                         ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out minute);
409                                         break;
410                                 case 'M':
411                                         tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
412                                         if (month != -1 || tokLen > 4)
413                                                 return false;
414
415                                         if (tokLen <= 2)
416                                                 ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out month);
417                                         else {
418                                                 ii += ParseEnum (input, ii, tokLen == 3 ? dfi.AbbreviatedMonthNames : dfi.MonthNames, allow_white_spaces, out month);
419                                                 month += 1;
420                                         }
421
422                                         break;
423                                 case 't':
424                                         tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
425                                         if (hour != -1 || tokLen > 2)
426                                                 return false;
427
428                                         ii += ParseEnum (input, ii,
429                                                          tokLen == 1 ? new string[] {new string (dfi.AMDesignator[0], 1), new string (dfi.PMDesignator[0], 0)} 
430                                                                      : new string[] {dfi.AMDesignator, dfi.PMDesignator},
431                                                          allow_white_spaces, out temp_int);
432                                         if (temp_int == -1)
433                                                 return false;
434
435                                         if (partial_hour == -1)
436                                                 partial_hour = temp_int * 12;
437                                         else
438                                                 hour = partial_hour + temp_int * 12;
439                                         break;
440                                 case 'y':
441                                         if (year != -1)
442                                                 return false;
443
444                                         tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
445                                         if (tokLen <= 2) {
446                                                 ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out year);
447                                                 if (year != -1)
448                                                         year += DateTime.Now.Year - DateTime.Now.Year % 100;
449                                         } else if (tokLen <= 4) { // yyy and yyyy accept up to 5 digits with leading 0
450                                                 int digit_parsed;
451                                                 ii += ParseNumber (input, ii, 5, false, allow_white_spaces, out year, out digit_parsed);
452                                                 if (digit_parsed < tokLen || (digit_parsed > tokLen && (year / (10 ^ (digit_parsed - 1)) < 1)))
453                                                         return false;
454                                         } else
455                                                 ii += ParseNumber (input, ii, tokLen, true, allow_white_spaces, out year);
456                                         break;
457                                 case 'z':
458                                         tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
459                                         if (offset != TimeSpan.MinValue || tokLen > 3)
460                                                 return false;
461
462                                         int off_h, off_m = 0, sign;
463                                         temp_int = 0;
464                                         ii += ParseEnum (input, ii, new string [] {"-", "+"}, allow_white_spaces, out sign);
465                                         ii += ParseNumber (input, ii, 2, tokLen != 1, false, out off_h);
466                                         if (tokLen == 3) {
467                                                 ii += ParseEnum (input, ii, new string [] {dfi.TimeSeparator}, false, out temp_int);
468                                                 ii += ParseNumber (input, ii, 2, true, false, out off_m);
469                                         }
470                                         if (off_h == -1 || off_m == -1 || sign == -1 || temp_int == -1)
471                                                 return false;
472
473                                         if (sign == 0)
474                                                 sign = -1;
475                                         offset = new TimeSpan (sign * off_h, sign * off_m, 0);
476                                         break;
477                                 case ':':
478                                         tokLen = 1;
479                                         ii += ParseEnum (input, ii, new string [] {dfi.TimeSeparator}, false, out temp_int);
480                                         if (temp_int == -1)
481                                                 return false;
482                                         break;
483                                 case '/':
484                                         tokLen = 1;
485                                         ii += ParseEnum (input, ii, new string [] {dfi.DateSeparator}, false, out temp_int);
486                                         if (temp_int == -1)
487                                                 return false;
488                                         break;
489                                 case '%':
490                                         tokLen = 1;
491                                         if (fi != 0) 
492                                                 return false;
493                                         break;
494                                 case ' ':
495                                         tokLen = 1;
496                                         ii += ParseEnum (input, ii, new string [] {" "}, false, out temp_int);
497                                         if (temp_int == -1)
498                                                 return false;
499                                         break;
500                                 case '\\':
501                                         tokLen = 2;
502                                         ii += ParseEnum (input, ii, new string [] {format.Substring (fi + 1, 1)}, allow_white_spaces, out temp_int);
503                                         if (temp_int == -1)
504                                                 return false;
505                                         break;
506                                 default:
507                                         Console.WriteLine ("un-parsed character: {0}", ch);
508                                         return false;
509                                 }
510                                 fi += tokLen;
511                         }
512
513                         //Console.WriteLine ("{0}-{1}-{2} {3}:{4} {5}", year, month, day, hour, minute, offset);
514                         if (offset == TimeSpan.MinValue && (styles & DateTimeStyles.AssumeLocal) != 0)
515                                 offset = TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.Now);
516
517                         if (offset == TimeSpan.MinValue && (styles & DateTimeStyles.AssumeUniversal) != 0)
518                                 offset = TimeSpan.Zero;
519
520                         if (hour < 0)           hour = 0;
521                         if (minute < 0)         minute = 0;
522
523                         if (year > 0 && month > 0 && day > 0) {
524                                 result = new DateTimeOffset (year, month, day, hour, minute, 0, offset);
525                                 if ((styles & DateTimeStyles.AdjustToUniversal) != 0)
526                                         result = result.ToUniversalTime ();
527                                 return true;
528                         }
529
530                         return false;
531                 }
532
533                 private static int ParseNumber (string input, int pos, int digits, bool leading_zero, bool allow_leading_white, out int result)
534                 {
535                         int digit_parsed;
536                         return ParseNumber (input, pos, digits, leading_zero, allow_leading_white, out result, out digit_parsed);
537                 }
538
539                 private static int ParseNumber (string input, int pos, int digits, bool leading_zero, bool allow_leading_white, out int result, out int digit_parsed)
540                 {
541                         int char_parsed = 0;
542                         digit_parsed = 0;
543                         result = 0;
544                         for (; allow_leading_white && pos < input.Length && input[pos] == ' '; pos++, char_parsed++)
545                                 ;
546
547                         for (; pos < input.Length && Char.IsDigit (input[pos]) && digits > 0; pos ++, char_parsed++, digit_parsed++, digits --)
548                                 result = 10 * result + (byte) (input[pos] - '0');
549
550                         if (leading_zero && digits > 0)
551                                 result = -1;
552
553                         if (digit_parsed == 0)
554                                 result = -1;
555
556                         return char_parsed;
557                 }
558
559                 private static int ParseEnum (string input, int pos, string [] enums, bool allow_leading_white, out int result)
560                 {
561                         int char_parsed = 0;
562                         result = -1;
563                         for (; allow_leading_white && pos < input.Length && input[pos] == ' '; pos++, char_parsed++)
564                                 ;
565                         
566                         for (int i = 0; i < enums.Length; i++)
567                                 if (input.Substring(pos).StartsWith (enums [i])) {
568                                         result = i;
569                                         break;
570                                 }
571
572                         if (result >= 0)
573                                 char_parsed += enums[result].Length;
574
575                         return char_parsed;     
576                 }
577
578                 public TimeSpan Subtract (DateTimeOffset other)
579                 {
580                         return UtcDateTime - other.UtcDateTime;
581                 }
582
583                 public DateTimeOffset Subtract (TimeSpan timeSpan)
584                 {
585                         return Add (-timeSpan);
586                 }
587
588                 public static TimeSpan operator - (DateTimeOffset left, DateTimeOffset right)
589                 {
590                         return left.Subtract (right);
591                 }
592
593                 public static DateTimeOffset operator - (DateTimeOffset dateTimeTz, TimeSpan timeSpan)
594                 {
595                         return dateTimeTz.Subtract (timeSpan);  
596                 }
597
598                 public long ToFileTime ()
599                 {
600                         return UtcDateTime.ToFileTime ();
601                 }
602
603                 public DateTimeOffset ToLocalTime ()
604                 {
605                         return new DateTimeOffset (UtcDateTime.ToLocalTime (), TimeZone.CurrentTimeZone.GetUtcOffset (UtcDateTime.ToLocalTime ()));
606                 }
607
608                 public DateTimeOffset ToOffset (TimeSpan offset)
609                 {
610                         return new DateTimeOffset (dt - utc_offset + offset, offset);
611                 }
612
613                 public override string ToString ()
614                 {
615                         return ToString (null, null);
616                 }
617
618                 public string ToString (IFormatProvider formatProvider)
619                 {
620                         return ToString (null, formatProvider);
621                 }
622
623                 public string ToString (string format)
624                 {
625                         return ToString (format, null);
626                 }
627
628                 public string ToString (string format, IFormatProvider formatProvider)
629                 {
630                         DateTimeFormatInfo dfi = DateTimeFormatInfo.GetInstance(formatProvider);
631
632                         if (format == null || format == String.Empty)
633                                 format = dfi.ShortDatePattern + " " + dfi.LongTimePattern + " zzz";
634
635                         bool to_utc = false, use_invariant = false;
636                         if (format.Length == 1) {
637                                 char fchar = format [0];
638                                 try {
639                                         format = DateTimeUtils.GetStandardPattern (fchar, dfi, out to_utc, out use_invariant, true);
640                                 } catch {
641                                         format = null;
642                                 }
643                 
644                                 if (format == null)
645                                         throw new FormatException ("format is not one of the format specifier characters defined for DateTimeFormatInfo");
646                         }
647                         return to_utc ? DateTimeUtils.ToString (UtcDateTime, TimeSpan.Zero, format, dfi) : DateTimeUtils.ToString (DateTime, Offset, format, dfi);
648                 }
649
650                 public DateTimeOffset ToUniversalTime ()
651                 {
652                         return new DateTimeOffset (UtcDateTime, TimeSpan.Zero); 
653                 }
654
655                 public static bool TryParse (string input, out DateTimeOffset result)
656                 {
657                         try {
658                                 result = Parse (input);
659                                 return true;
660                         } catch {
661                                 result = MinValue;
662                                 return false;
663                         }
664                 }
665
666                 public static bool TryParse (string input, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
667                 {
668                         try {
669                                 result = Parse (input, formatProvider, styles);
670                                 return true;
671                         } catch {
672                                 result = MinValue;
673                                 return false;
674                         }       
675                 }
676
677                 public static bool TryParseExact (string input, string format, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
678                 {
679                         try {
680                                 result = ParseExact (input, format, formatProvider, styles);
681                                 return true;
682                         } catch {
683                                 result = MinValue;
684                                 return false;
685                         }
686                 }
687
688                 public static bool TryParseExact (string input, string[] formats, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
689                 {
690                         try {
691                                 result = ParseExact (input, formats, formatProvider, styles);
692                                 return true;
693                         } catch {
694                                 result = MinValue;
695                                 return false;
696                         }
697                 }
698
699                 public DateTime Date {
700                         get { return DateTime.SpecifyKind (dt.Date, DateTimeKind.Unspecified); }
701                 }
702
703                 public DateTime DateTime {
704                         get { return DateTime.SpecifyKind (dt, DateTimeKind.Unspecified); }
705                 }
706
707                 public int Day {
708                         get { return dt.Day; }
709                 }
710
711                 public DayOfWeek DayOfWeek {
712                         get { return dt.DayOfWeek; }
713                 }
714
715                 public int DayOfYear {
716                         get { return dt.DayOfYear; }
717                 }
718
719                 public int Hour {
720                         get { return dt.Hour; }
721                 }
722
723                 public DateTime LocalDateTime {
724                         get { return UtcDateTime.ToLocalTime (); }
725                 }
726
727                 public int Millisecond {
728                         get { return dt.Millisecond; }
729                 }
730
731                 public int Minute {
732                         get { return dt.Minute; }
733                 }
734
735                 public int Month {
736                         get { return dt.Month; }
737                 }
738
739                 public static DateTimeOffset Now {
740                         get { return new DateTimeOffset (DateTime.Now);}
741                 }
742
743                 public TimeSpan Offset {
744                         get { return utc_offset; }      
745                 }
746
747                 public int Second {
748                         get { return dt.Second; }
749                 }
750
751                 public long Ticks {
752                         get { return dt.Ticks; }        
753                 }
754
755                 public TimeSpan TimeOfDay {
756                         get { return dt.TimeOfDay; }
757                 }
758
759                 public DateTime UtcDateTime {
760                         get { return DateTime.SpecifyKind (dt - utc_offset, DateTimeKind.Utc); }        
761                 }
762                 
763                 public static DateTimeOffset UtcNow {
764                         get { return new DateTimeOffset (DateTime.UtcNow); }
765                 }
766
767                 public long UtcTicks {
768                         get { return UtcDateTime.Ticks; }
769                 }
770
771                 public int Year {
772                         get { return dt.Year; }
773                 }
774         }
775 }
776 #endif