[corlib] Update list of known lcids and update CLRD data. Fixes #46456
[mono.git] / mcs / class / corlib / Test / System / DateTimeOffsetTest.cs
1 //
2 // DateTimeOffsetTest.cs - NUnit Test Cases for the System.DateTimeOffset struct
3 //
4 // Authors:
5 //      Stephane Delcroix  (sdelcroix@novell.com)
6 //      Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
9 // Copyright 2012 Xamarin, Inc (http://www.xamarin.com)
10 //
11
12 using System.Globalization;
13 using NUnit.Framework;
14 using System;
15
16 namespace MonoTests.System {
17
18         [TestFixture]
19         public class DateTimeOffsetTest
20         {
21                 //ctor exception checking...
22                 [Test]
23                 [ExpectedException (typeof (ArgumentException))]
24                 public void UtcWithWrongOffset ()
25                 {
26                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Utc);
27                         TimeSpan offset = new TimeSpan (2, 0, 0);
28                         new DateTimeOffset (dt, offset);
29                 }
30
31                 [Test]
32                 [ExpectedException (typeof (ArgumentException))]
33                 public void LocalWithWrongOffset ()
34                 {
35                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Local);
36                         TimeSpan offset = TimeZone.CurrentTimeZone.GetUtcOffset (dt) + new TimeSpan (1,0,0);
37                         new DateTimeOffset (dt, offset);
38                 }
39
40                 [Test]
41                 [ExpectedException (typeof (ArgumentException))]
42                 public void OffsetNotInWholeminutes ()
43                 {
44                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Unspecified);
45                         TimeSpan offset = new TimeSpan (1,0,59);
46                         new DateTimeOffset (dt, offset);
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
51                 public void OffsetOutOfRange1 ()
52                 {
53                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Unspecified);
54                         TimeSpan offset = new TimeSpan (14, 1, 0);
55                         new DateTimeOffset (dt, offset);
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
60                 public void OffsetOutOfRange2 ()
61                 {
62                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Unspecified);
63                         TimeSpan offset = new TimeSpan (-14, -1, 0);
64                         new DateTimeOffset (dt, offset);
65                 }
66
67                 [Test]
68                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
69                 public void UtcDateTimeOutOfRange1 ()
70                 {
71                         DateTime dt = DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Unspecified);
72                         TimeSpan offset = new TimeSpan (1, 0, 0);
73                         new DateTimeOffset (dt, offset);
74                 }
75
76                 [Test]
77                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
78                 public void UtcDateTimeOutOfRange2 ()
79                 {
80                         DateTime dt = DateTime.SpecifyKind (DateTime.MaxValue, DateTimeKind.Unspecified);
81                         TimeSpan offset = new TimeSpan (-1, 0, 0);
82                         new DateTimeOffset (dt, offset);
83                 }
84
85                 [Test]
86                 public void CompareTwoDateInDiffTZ ()
87                 {
88                         DateTimeOffset dt1 = new DateTimeOffset (2007, 12, 16, 15, 06, 00, new TimeSpan (1, 0, 0));
89                         DateTimeOffset dt2 = new DateTimeOffset (2007, 12, 16, 9, 06, 00, new TimeSpan (-5, 0, 0));
90                         DateTimeOffset dt3 = new DateTimeOffset (2007, 12, 16, 14, 06, 00, new TimeSpan (1, 0, 0));
91                         object o = dt1;
92                         Assert.IsTrue (dt1.CompareTo (dt2) == 0);
93                         Assert.IsTrue (DateTimeOffset.Compare (dt1, dt2) == 0);
94                         Assert.IsTrue (dt1 == dt2);
95                         Assert.IsTrue (dt1.Equals (dt2));
96                         Assert.IsFalse (dt1 == dt3);
97                         Assert.IsTrue (dt1 != dt3);
98                         Assert.IsFalse (dt1.EqualsExact (dt2));
99                         Assert.IsTrue (dt1.CompareTo (dt3) > 0);
100                         Assert.IsTrue (((IComparable)dt1).CompareTo (o) == 0);
101                 }
102
103                 [Test]
104                 public void UtcDateTime ()
105                 {
106                         DateTimeOffset dt = new DateTimeOffset (2007, 12, 16, 15, 49, 00, new TimeSpan (1, 0, 0));
107                         object o = dt.UtcDateTime;
108                         Assert.IsTrue (o is DateTime);
109                         Assert.IsTrue (dt.UtcDateTime == new DateTime (2007,12,16,14,49,00,DateTimeKind.Utc));
110                 }
111
112                 [Test]
113                 public void ParameterlessToString ()
114                 {
115                         DateTimeOffset dt = new DateTimeOffset (2007, 12, 18, 12, 16, 30, new TimeSpan (1, 0, 0));
116                         Assert.AreEqual ("12/18/2007 12:16:30 PM +01:00", dt.ToString (new CultureInfo ("en-us")));
117                         dt = new DateTimeOffset (2007, 12, 18, 12, 16, 30, new TimeSpan (-5, 0, 0));
118                         Assert.AreEqual ("12/18/2007 12:16:30 PM -05:00", dt.ToString (new CultureInfo ("en-us")));
119                         dt = new DateTimeOffset (2007, 12, 18, 12, 16, 30, TimeSpan.Zero);
120                         Assert.AreEqual ("12/18/2007 12:16:30 PM +00:00", dt.ToString (new CultureInfo ("en-us")));
121                 }
122
123                 [Test]
124                 public void ToStringWithCultureInfo ()
125                 {
126                         DateTimeOffset dto = new DateTimeOffset(2007, 5, 1, 9, 0, 0, TimeSpan.Zero);
127                         Assert.AreEqual ("05/01/2007 09:00:00 +00:00", dto.ToString (CultureInfo.InvariantCulture));
128                         Assert.AreEqual ("5/1/2007 9:00:00 AM +00:00", dto.ToString (new CultureInfo ("en-us")));
129                         Assert.AreEqual ("01/05/2007 09:00:00 +00:00", dto.ToString (new CultureInfo ("fr-fr")));
130                         Assert.AreEqual ("01.05.2007 09:00:00 +00:00", dto.ToString (new CultureInfo ("de-DE")));
131                         Assert.AreEqual ("01/05/2007 9:00:00 +00:00", dto.ToString (new CultureInfo ("es-ES")));
132                 }
133
134                 [Test]
135                 [ExpectedException (typeof (FormatException))]
136                 public void ToStringExceptionalCase1 ()
137                 {
138                         DateTimeOffset.Now.ToString (";");
139                 }
140
141                 [Test]
142                 [ExpectedException (typeof (FormatException))]
143                 public void ToStringExceptionalCase2 ()
144                 {
145                         DateTimeOffset.Now.ToString ("U");
146                 }
147
148                 [Test]
149                 public void ToStringWithFormat ()
150                 {
151                         DateTimeOffset dto = new DateTimeOffset (2007, 10, 31, 21, 0, 0, new TimeSpan(-8, 0, 0));
152                         Assert.AreEqual ("10/31/2007", dto.ToString ("d", new CultureInfo ("en-us")));
153                         Assert.AreEqual ("Wednesday, October 31, 2007", dto.ToString ("D", new CultureInfo ("en-us")));
154                         Assert.AreEqual ("9:00 PM", dto.ToString ("t", new CultureInfo ("en-us")));
155                         Assert.AreEqual ("9:00:00 PM", dto.ToString ("T", new CultureInfo ("en-us")));
156                         Assert.AreEqual ("Wednesday, October 31, 2007 9:00 PM", dto.ToString ("f", new CultureInfo ("en-us")));
157                         Assert.AreEqual ("Wednesday, October 31, 2007 9:00:00 PM", dto.ToString ("F", new CultureInfo ("en-us")));
158                         Assert.AreEqual ("10/31/2007 9:00 PM", dto.ToString ("g", new CultureInfo ("en-us")));
159                         Assert.AreEqual ("10/31/2007 9:00:00 PM", dto.ToString ("G", new CultureInfo ("en-us")));
160                         Assert.AreEqual ("October 31", dto.ToString ("M", new CultureInfo ("en-us")));
161                         Assert.AreEqual (dto.ToString ("m", new CultureInfo ("en-us")), dto.ToString ("M", new CultureInfo ("en-us")));
162                         Assert.AreEqual ("Thu, 01 Nov 2007 05:00:00 GMT", dto.ToString ("R", new CultureInfo ("en-us")));
163                         Assert.AreEqual (dto.ToString ("r", new CultureInfo ("en-us")), dto.ToString ("R", new CultureInfo ("en-us")));
164                         Assert.AreEqual ("2007-10-31T21:00:00", dto.ToString ("s", new CultureInfo ("en-us")));
165                         Assert.AreEqual ("2007-11-01 05:00:00Z", dto.ToString ("u", new CultureInfo ("en-us")));
166                         Assert.AreEqual ("October 2007", dto.ToString ("Y", new CultureInfo ("en-us")));
167                         Assert.AreEqual (dto.ToString ("y", new CultureInfo ("en-us")), dto.ToString ("Y", new CultureInfo ("en-us")));
168                 }
169
170                 [Test]
171                 public void ToStringWithFormatAndCulture ()
172                 {
173                         DateTimeOffset dto = new DateTimeOffset (2007, 11, 1, 9, 0, 0, new TimeSpan(-7, 0, 0));
174                         const string format = "dddd, MMM dd yyyy HH:mm:ss zzz";
175                         Assert.AreEqual ("Thursday, Nov 01 2007 09:00:00 -07:00", dto.ToString (format, CultureInfo.InvariantCulture), "ts2");
176                         Assert.AreEqual ("jeudi, nov. 01 2007 09:00:00 -07:00", dto.ToString (format, new CultureInfo ("fr-FR")), "ts3");
177                         Assert.AreEqual ("jueves, nov. 01 2007 09:00:00 -07:00", dto.ToString (format, new CultureInfo ("es-ES")), "ts4");
178                 }
179
180                 [Test]
181                 public void ParseExactDatesOnly ()
182                 {
183                         DateTimeOffset dto = DateTimeOffset.Now;
184                         CultureInfo fp = CultureInfo.InvariantCulture;
185
186                         string[] formats = {
187                                 "d",
188                                 "M/dd/yyyy",
189                                 "M/d/y",
190                                 "M/d/yy",
191 //                              "M/d/yyy", // this fails on .NET 2.0 (3.5) and 4.0
192                                 "M/d/yyyy",
193                                 "M/d/yyyyy",
194                                 "M/d/yyyyyy",
195                                 "M/dd/yyyy",
196                                 "MM/dd/yyyy",
197                         };
198
199                         foreach (string format in formats)
200                         {
201                                 string serialized = dto.ToString (format, fp);
202                                 //Console.WriteLine ("{0} {1} {2}", format, dto, serialized);
203                                 Assert.AreEqual (dto.Date, DateTimeOffset.ParseExact (serialized, format, fp).Date, format);
204
205                         }
206                 }
207
208                 [Test]
209                 public void ParseExactTest ()
210                 {
211                         CultureInfo fp = CultureInfo.InvariantCulture;
212                         DateTimeOffset[] dtos = {
213                                 new DateTimeOffset (2008, 01, 14, 13, 21, 0, new TimeSpan (1,0,0)),
214                                 new DateTimeOffset (2008, 01, 14, 13, 21, 0, new TimeSpan (-5,0,0)),
215                         };
216
217                         string[] formats = {
218                                 "M/dd/yyyy HH:m zzz",  "MM/dd/yyyy HH:m zzz",
219                                 "M/d/yyyy HH:m zzz",   "MM/d/yyyy HH:m zzz",
220                                 "M/dd/yy HH:m zzz",    "MM/dd/yy HH:m zzz",
221                                 "M/d/yy HH:m zzz",     "M/d/yy HH:m zzz",
222                                 "M/dd/yyyy H:m zzz",   "MM/dd/yyyy H:m zzz",
223                                 "M/d/yyyy H:m zzz",    "MM/d/yyyy H:m zzz",
224                                 "M/dd/yy H:m zzz",     "MM/dd/yy H:m zzz",
225                                 "M/d/yy H:m zzz",      "MM/d/yy H:m zzz",
226                                 "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz",
227                                 "M/d/yyyy HH:mm zzz",  "MM/d/yyyy HH:mm zzz",
228                                 "M/dd/yy HH:mm zzz",   "MM/dd/yy HH:mm zzz",
229                                 "M/d/yy HH:mm zzz",    "M/d/yy HH:mm zzz",
230                                 "M/dd/yyyy H:m zzz",   "MM/dd/yyyy H:m zzz",
231                                 "M/d/yyyy H:m zzz",    "MM/d/yyyy H:m zzz",
232                                 "M/dd/yy H:m zzz",     "MM/dd/yy H:m zzz",
233                                 "M/d/yy H:m zzz",      "M/d/yy H:m zzz",
234                                 "ddd dd MMM yyyy h:mm tt zzz",
235                                 "o", "O",
236                                 "r", "R",
237                                 "u",
238                         };
239
240                         foreach (DateTimeOffset dto in dtos)
241                                 foreach (string format in formats)
242                                 {
243                                         string serialized = dto.ToString (format, fp);
244                                         //Console.WriteLine ("{0} {1} {2}", format, dto, serialized);
245                                         Assert.AreEqual (dto, DateTimeOffset.ParseExact (serialized, format, fp), format);
246                                 }
247                 }
248
249                 //
250                 // Tests that we can parse the K format specifier which is a 4 digit offset
251                 // see bug 589227
252                 //
253                 [Test]
254                 public void ParseExactWithKFormat ()
255                 {
256                         DateTimeOffset o = DateTimeOffset.ParseExact ("Wed Mar 17 22:25:08 +0000 2010", "ddd MMM d H:m:ss K yyyy", CultureInfo.InvariantCulture);
257                 }
258
259                 [Test]
260                 public void ParseExactCustomFormat ()
261                 {
262                         var dt = DateTimeOffset.ParseExact ("Sunday, 06-Nov-94 08:49:37 GMT", "dddd, dd'-'MMM'-'yy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
263                         Assert.AreEqual (new DateTimeOffset (1994, 11, 6, 8, 49, 37, TimeSpan.Zero), dt);
264                 }
265                 
266                 [Test]
267                 public void ParseExactYearFormat ()
268                 {
269                         CultureInfo fp = CultureInfo.InvariantCulture;
270                         DateTimeOffset[] dtos = {
271                                 new DateTimeOffset (2008, 01, 14, 13, 21, 0, new TimeSpan (1,0,0)),
272                                 new DateTimeOffset (2008, 01, 14, 13, 21, 0, new TimeSpan (-5,0,0)),
273                                 new DateTimeOffset (1978, 02, 16, 13, 21, 0, new TimeSpan (2,0,0)),
274                                 new DateTimeOffset (999, 9, 9, 9, 9, 0, new TimeSpan (3,0,0)),
275                         };
276
277                         string[] formats = {
278 //                              "M/d/yyy H:m zzz", // fails under .NET 2.0 and 4.0.
279                                 "M/d/yyyy H:m zzz",
280                                 "M/d/yyyyy H:m zzz",
281                                 "M/d/yyyyyy H:m zzz",
282                                 "M/d/yyyyyyy H:m zzz",
283                         };
284
285                         foreach (DateTimeOffset dto in dtos)
286                                 foreach (string format in formats)
287                                 {
288                                         string serialized = dto.ToString (format, fp);
289                                         //Console.WriteLine ("{0} {1} {2}", format, dto, serialized);
290                                         Assert.AreEqual (dto, DateTimeOffset.ParseExact (serialized, format, fp), format);
291                                 }
292                         
293                 }
294
295                 [Test]
296                 public void ParseExactOffsetFormat ()
297                 {
298                         CultureInfo fp = CultureInfo.InvariantCulture;
299                         string[] dates = {
300                                 "13/Oct/2009:22:35:35 +09:00",
301                                 "13/Oct/2009:22:35:35 +0900",
302                                 "13/Oct/2009:22:35:35 +09:30",
303                                 "13/Oct/2009:22:35:35 +0930",
304                         };
305                         TimeSpan[] offsets = {
306                                 new TimeSpan (9,0,0),
307                                 new TimeSpan (9,0,0),
308                                 new TimeSpan (9,30,0),
309                                 new TimeSpan (9,30,0),
310                         };
311
312                         for (int i = 0; i < dates.Length; i++)
313                                 Assert.AreEqual(offsets[i], DateTimeOffset.ParseExact (dates[i], "d/MMM/yyyy:HH:mm:ss zzz", fp).Offset, dates[i]);
314                 }
315
316                 [Test]
317                 [ExpectedException (typeof (FormatException))]
318                 public void ParseExactYearException ()
319                 {
320                         CultureInfo fp = CultureInfo.InvariantCulture;
321                         string format = "M/d/yyyy H:m zzz";
322                         string date = "1/15/999 10:58 +01:00";
323                         DateTimeOffset.ParseExact(date, format, fp);    
324                 }
325
326                 [Test]
327                 [ExpectedException (typeof (FormatException))]
328                 public void ParseExactFormatException1 ()
329                 {
330                         CultureInfo fp = CultureInfo.InvariantCulture;
331                         string format = "d";
332                         string date = "1/14/2008";
333                         DateTimeOffset.ParseExact(date, format, fp);
334                 }
335
336                 [Test]
337                 [ExpectedException (typeof (FormatException))]
338                 public void ParseExactFormatException2 ()
339                 {
340                         CultureInfo fp = CultureInfo.InvariantCulture;
341                         string format = "ddd dd MMM yyyy h:mm tt zzz";
342                         string date = "Mon 14 Jan 2008 2:56 PM +01";
343                         DateTimeOffset.ParseExact(date, format, fp);
344                 }
345
346                 [Test]
347                 [ExpectedException (typeof (FormatException))]
348                 public void ParseExactFormatException3 ()
349                 {
350                         CultureInfo fp = CultureInfo.InvariantCulture;
351                         string format = "ddd dd MMM yyyy h:mm tt zzz";
352                         string date = "Mon 14 Jan 2008 2:56 PM +01:1";
353                         DateTimeOffset.ParseExact(date, format, fp);
354                 }
355
356                 [Test]
357                 [ExpectedException (typeof (FormatException))]
358                 public void ParseExactFormatException4 ()
359                 {
360                         CultureInfo fp = CultureInfo.InvariantCulture;
361                         string format = "ddd dd MMM yyyy h:mm tt zzz";
362                         string date = "Mon 14 Jan 2008 2:56 PM +01: 00";
363                         DateTimeOffset.ParseExact(date, format, fp);
364                 }
365
366                 [Test]
367                 [ExpectedException (typeof (FormatException))]
368                 public void ParseExactFormatException5 ()
369                 {
370                         CultureInfo fp = CultureInfo.InvariantCulture;
371                         string format = "U";
372                         string date = "Mon 14 Jan 2008 2:56 PM +01: 00";
373                         DateTimeOffset.ParseExact (date, format, fp);
374                 }
375
376                 [Test]
377                 public void ParseExactWithSeconds ()
378                 {
379                         CultureInfo fp = CultureInfo.InvariantCulture;
380                         DateTimeOffset[] dtos = {
381                                 new DateTimeOffset (2008, 01, 14, 13, 21, 25, new TimeSpan (1,0,0)),
382                                 new DateTimeOffset (2008, 01, 14, 13, 21, 5, new TimeSpan (-5,0,0)),
383                         };
384
385                         string[] formats = {
386                                 "dddd, dd MMMM yyyy HH:mm:ss zzz",
387                         };
388
389                         foreach (DateTimeOffset dto in dtos)
390                                 foreach (string format in formats)
391                                 {
392                                         string serialized = dto.ToString (format, fp);
393                                         //Console.WriteLine ("{0} {1} {2}", format, dto, serialized);
394                                         Assert.AreEqual (dto, DateTimeOffset.ParseExact (serialized, format, fp), format);
395                                 }
396                 }
397
398                 [Test]
399                 public void ParseDateOnly ()
400                 {
401                         DateTimeOffset dto = DateTimeOffset.Parse ("2009/06/14");
402                         Assert.AreEqual (14, dto.Day, "Day");
403                         Assert.AreEqual (6, dto.Month, "Month");
404                         Assert.AreEqual (2009, dto.Year, "Year");
405                 }
406
407                 [Test]
408                 public void ParseTimeOnly ()
409                 {
410                         DateTimeOffset dto = DateTimeOffset.Parse ("2:3:4");
411                         Assert.AreEqual (2, dto.Hour, "Hour");
412                         Assert.AreEqual (3, dto.Minute, "Minute");
413                         Assert.AreEqual (4, dto.Second, "Second");
414                 }
415
416                 [Test]
417                 public void ParseTimeTZ ()
418                 {
419                         DateTimeOffset dto = DateTimeOffset.Parse ("2:30:40 +3:4");
420                         Assert.AreEqual (2, dto.Hour, "Hour");
421                         Assert.AreEqual (30, dto.Minute, "Minute");
422                         Assert.AreEqual (40, dto.Second, "Second");
423                         Assert.AreEqual (3, dto.Offset.Hours, "Offset Hours");
424                         Assert.AreEqual (4, dto.Offset.Minutes, "Offset Minutes");
425                 }
426
427                 [Test]
428                 public void ParseTimePMTZ ()
429                 {
430                         DateTimeOffset dto = DateTimeOffset.Parse ("3:30:40 PM -3:4");
431                         Assert.AreEqual (15, dto.Hour, "Hour");
432                         Assert.AreEqual (30, dto.Minute, "Minute");
433                         Assert.AreEqual (40, dto.Second, "Second");
434                         Assert.AreEqual (-3, dto.Offset.Hours, "Offset Hours");
435                         Assert.AreEqual (-4, dto.Offset.Minutes, "Offset Minutes");
436                 }
437
438                 [Test]
439                 public void ParseFull1 ()
440                 {
441                         DateTimeOffset dto = DateTimeOffset.Parse ("2009/06/14 2:30:40 AM -03:04");
442                         Assert.AreEqual (14, dto.Day, "Day");
443                         Assert.AreEqual (6, dto.Month, "Month");
444                         Assert.AreEqual (2009, dto.Year, "Year");
445                         Assert.AreEqual (2, dto.Hour, "Hour");
446                         Assert.AreEqual (30, dto.Minute, "Minute");
447                         Assert.AreEqual (40, dto.Second, "Second");
448                         Assert.AreEqual (-3, dto.Offset.Hours, "Offset Hours");
449                         Assert.AreEqual (-4, dto.Offset.Minutes, "Offset Minutes");
450                 }
451
452                 [Test]
453                 [ExpectedException (typeof (FormatException))]
454                 public void ParseUnderflow ()
455                 {
456                         DateTimeOffset.Parse ("01/01/0001 0:0 +09:00", new CultureInfo ("en-US"));
457                 }
458
459                 [Test]
460                 [ExpectedException (typeof (FormatException))]
461                 public void ParseOverflow ()
462                 {
463                         DateTimeOffset.Parse ("12/31/9999 23:59 -09:00", new CultureInfo ("en-US"));
464                 }
465
466                 [Test]
467                 public void ParseExactWithFractions ()
468                 {
469                         CultureInfo fp = CultureInfo.InvariantCulture;
470                         DateTimeOffset[] dtos = {
471                                 new DateTimeOffset (2008, 1, 15, 14, 36, 44, 900, TimeSpan.Zero),
472                         };
473
474                         string[] formats = {
475                                 "M/d/yyyy H:m:ss,f zzz",
476                                 "M/d/yyyy H:m:ss,ff zzz",
477                                 "M/d/yyyy H:m:ss,fff zzz",
478                                 "M/d/yyyy H:m:ss,ffff zzz",
479                                 "M/d/yyyy H:m:ss,fffff zzz",
480                                 "M/d/yyyy H:m:ss,ffffff zzz",
481                                 "M/d/yyyy H:m:ss,fffffff zzz",
482                                 "M/d/yyyy H:m:ss,F zzz",
483                                 "M/d/yyyy H:m:ss,FF zzz",
484                                 "M/d/yyyy H:m:ss,FFF zzz",
485                                 "M/d/yyyy H:m:ss,FFFF zzz",
486                                 "M/d/yyyy H:m:ss,FFFFF zzz",
487                                 "M/d/yyyy H:m:ss,FFFFFF zzz",
488                                 "M/d/yyyy H:m:ss,FFFFFFF zzz",
489                         };
490
491                         foreach (DateTimeOffset dto in dtos)
492                                 foreach (string format in formats)
493                                 {
494                                         string serialized = dto.ToString (format, fp);
495                                         //Console.WriteLine ("{0} {1} {2}", format, dto, serialized);
496                                         Assert.AreEqual (dto, DateTimeOffset.ParseExact (serialized, format, fp), format);
497                                 }
498                 }
499
500                 [Test]
501                 public void ParseExactPreserveFractions ()
502                 {
503                         var s = "1999-06-10T21:27:03.1147764+02:00";
504                         var d = DateTimeOffset.ParseExact (s, "yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
505                         Assert.AreEqual (630646468231147764, d.Ticks, "#1");
506                 }
507
508                 [Test]
509                 public void EqualsObject ()
510                 {
511                         DateTimeOffset offset1 = new DateTimeOffset ();
512                         Assert.IsFalse (offset1.Equals (null), "null"); // found using Gendarme :)
513                         Assert.IsTrue (offset1.Equals (offset1), "self");
514                         DateTimeOffset offset2 = new DateTimeOffset (DateTime.Today);
515                         Assert.IsFalse (offset1.Equals (offset2), "1!=2");
516                         Assert.IsFalse (offset2.Equals (offset1), "2!=1");
517                 }
518
519                 [Test]
520                 public void Serialization()
521                 {
522                         global::System.IO.MemoryStream dst = new global::System.IO.MemoryStream ();
523                         global::System.Runtime.Serialization.IFormatter fmtr
524                                 = new global::System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ();
525                         for (int i = 0; i < SerializationCases.Length; ++i) {
526                                 dst.SetLength (0);
527                                 DateTimeOffset cur = SerializationCases[i].Input;
528                                 fmtr.Serialize (dst, cur);
529                                 String result = BitConverter.ToString (dst.GetBuffer (), 0, (int)dst.Length);
530                                 Assert.AreEqual (SerializationCases[i].ResultBinaryString, result, "resultToString #" + i);
531                         }//for
532                 }
533
534                 [Test]
535                 public void Deserialization()
536                 {
537                         global::System.Runtime.Serialization.IFormatter fmtr
538                                 = new global::System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ();
539                         global::System.IO.MemoryStream src;
540                         for (int i = 0; i < SerializationCases.Length; ++i) {
541                                 src = new global::System.IO.MemoryStream (
542                                         BitConverter_ByteArray_FromString (SerializationCases[i].ResultBinaryString));
543                                 DateTimeOffset result = (DateTimeOffset)fmtr.Deserialize (src);
544 #if false // DUMP_TO_CONSOLE
545                                 Console.WriteLine ("#{0} input.o/s : {1}", i, SerializationCases[i].Input.Offset);
546                                 Console.WriteLine ("#{0} result.o/s: {1}", i, result.Offset);
547                                 Console.WriteLine ("#{0} input.dt : {1}={1:R}={1:u}", i, SerializationCases[i].Input.DateTime);
548                                 Console.WriteLine ("#{0} result.dt: {1}={1:R}={1:u}", i, result.DateTime);
549                                 Console.WriteLine ("#{0} input.dtK: {1}", i, SerializationCases[i].Input.DateTime.Kind);
550                                 Console.WriteLine ("#{0} input : {1}={1:R}={1:u}", i, SerializationCases[i].Input);
551                                 Console.WriteLine ("#{0} result: {1}={1:R}={1:u}", i, result);
552 #endif
553                                 Assert.AreEqual (SerializationCases[i].Input.Offset, result.Offset, ".Offset #" + i);
554                                 Assert.AreEqual (SerializationCases[i].Input.DateTime, result.DateTime, ".DateTime #" + i);
555                                 Assert.AreEqual (SerializationCases[i].Input, result, "equals #" + i);
556                                 // DateTimeOffset always stores as Kind==Unspecified
557                                 Assert.AreEqual (DateTimeKind.Unspecified, SerializationCases[i].Input.DateTime.Kind, ".DateTime.Kind==unspec #" + i);
558                                 Assert.AreEqual (SerializationCases[i].Input.DateTime.Kind, result.DateTime.Kind, ".DateTime.Kind #" + i);
559                         }//for
560                 }
561
562                 public class TestRow
563                 {
564                         public DateTimeOffset Input;
565                         public String ResultBinaryString;
566
567                         public TestRow(DateTimeOffset input, String resultBinaryString)
568                         {
569                                 this.Input = input;
570                                 this.ResultBinaryString = resultBinaryString;
571                         }
572                 }
573                 readonly TestRow[] SerializationCases = {
574                         // Multiple tests of Unspecified and different timezone offsets; one 
575                         // for UTC; and one for Local which is disabled as it suits only a machine
576                         // in GMT or similar.
577 #if ___MACHINE_TIMEZONE_HAS_ZERO_OFFSET
578                         // The "new DateTimeOffset(new DateTime(...)))" expression results in a 
579                         // DTO that has an offset set to the machine timezone offset.  So, as 
580                         // with the test case at the bottom we can't test this around the world.
581                         new TestRow (new DateTimeOffset (new DateTime (2007, 01, 02, 12, 30, 50)),
582                                 "00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-"
583                                 + "00-04-01-00-00-00-15-53-79-73-74-65-6D-2E-44-61-"
584                                 + "74-65-54-69-6D-65-4F-66-66-73-65-74-02-00-00-00-"
585                                 + "08-44-61-74-65-54-69-6D-65-0D-4F-66-66-73-65-74-"
586                                 + "4D-69-6E-75-74-65-73-00-00-0D-07-00-39-75-F8-80-"
587                                 + "FC-C8-08-00-00-0B"),
588 #endif
589                         new TestRow (new DateTimeOffset (new DateTime (2007, 01, 02, 12, 30, 50), new TimeSpan (0, 0, 0)),
590                                 "00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-"
591                                 + "00-04-01-00-00-00-15-53-79-73-74-65-6D-2E-44-61-"
592                                 + "74-65-54-69-6D-65-4F-66-66-73-65-74-02-00-00-00-"
593                                 + "08-44-61-74-65-54-69-6D-65-0D-4F-66-66-73-65-74-"
594                                 + "4D-69-6E-75-74-65-73-00-00-0D-07-00-39-75-F8-80-"
595                                 + "FC-C8-08-00-00-0B"),
596                         new TestRow (new DateTimeOffset (new DateTime (2007, 01, 02, 12, 30, 50), new TimeSpan (1, 0, 0)),
597                                 "00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-"
598                                 + "00-04-01-00-00-00-15-53-79-73-74-65-6D-2E-44-61-"
599                                 + "74-65-54-69-6D-65-4F-66-66-73-65-74-02-00-00-00-"
600                                 + "08-44-61-74-65-54-69-6D-65-0D-4F-66-66-73-65-74-"
601                                 + "4D-69-6E-75-74-65-73-00-00-0D-07-00-D1-B0-96-78-"
602                                 + "FC-C8-08-3C-00-0B"),
603                         new TestRow (new DateTimeOffset (new DateTime (2007, 01, 02, 12, 30, 50), new TimeSpan (0, 30, 0)),
604                                 "00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-"
605                                 + "00-04-01-00-00-00-15-53-79-73-74-65-6D-2E-44-61-"
606                                 + "74-65-54-69-6D-65-4F-66-66-73-65-74-02-00-00-00-"
607                                 + "08-44-61-74-65-54-69-6D-65-0D-4F-66-66-73-65-74-"
608                                 + "4D-69-6E-75-74-65-73-00-00-0D-07-00-05-93-C7-7C-"
609                                 + "FC-C8-08-1E-00-0B"),
610                         new TestRow (new DateTimeOffset (new DateTime (2007, 01, 02, 12, 30, 50), new TimeSpan (-5, 0, 0)),
611                                 "00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-"
612                                 + "00-04-01-00-00-00-15-53-79-73-74-65-6D-2E-44-61-"
613                                 + "74-65-54-69-6D-65-4F-66-66-73-65-74-02-00-00-00-"
614                                 + "08-44-61-74-65-54-69-6D-65-0D-4F-66-66-73-65-74-"
615                                 + "4D-69-6E-75-74-65-73-00-00-0D-07-00-41-4B-E1-AA-"
616                                 + "FC-C8-08-D4-FE-0B"),
617                         new TestRow (new DateTimeOffset (new DateTime (2007, 01, 02, 12, 30, 50), new TimeSpan (-10, 30, 0)),
618                                 "00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-"
619                                 + "00-04-01-00-00-00-15-53-79-73-74-65-6D-2E-44-61-"
620                                 + "74-65-54-69-6D-65-4F-66-66-73-65-74-02-00-00-00-"
621                                 + "08-44-61-74-65-54-69-6D-65-0D-4F-66-66-73-65-74-"
622                                 + "4D-69-6E-75-74-65-73-00-00-0D-07-00-15-3F-99-D0-"
623                                 + "FC-C8-08-C6-FD-0B"),
624                         // invalid case: .ctor ArgEx "non whole minutes"
625                         //      new DateTimeOffset(new DateTime(2007, 01, 02, 12, 30, 50), new TimeSpan(1, 2, 3));
626                         //----
627                         new TestRow (new DateTimeOffset (new DateTime (2007, 01, 02, 12, 30, 50, DateTimeKind.Utc)),
628                                 "00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-"
629                                 + "00-04-01-00-00-00-15-53-79-73-74-65-6D-2E-44-61-"
630                                 + "74-65-54-69-6D-65-4F-66-66-73-65-74-02-00-00-00-"
631                                 + "08-44-61-74-65-54-69-6D-65-0D-4F-66-66-73-65-74-"
632                                 + "4D-69-6E-75-74-65-73-00-00-0D-07-00-39-75-F8-80-"
633                                 + "FC-C8-08-00-00-0B"),
634                         //----
635 #if ___MACHINE_TIMEZONE_HAS_ZERO_OFFSET
636                         // Local needs offset to be the same as the machine timezone,
637                         // not easy to cope with the tests being run around the world!
638                         // This one works in UK, etc.
639                         new TestRow (new DateTimeOffset (new DateTime (2007, 01, 02, 12, 30, 50, DateTimeKind.Local), new TimeSpan (0,0,0)),
640                                 "00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-"
641                                 + "00-04-01-00-00-00-15-53-79-73-74-65-6D-2E-44-61-"
642                                 + "74-65-54-69-6D-65-4F-66-66-73-65-74-02-00-00-00-"
643                                 + "08-44-61-74-65-54-69-6D-65-0D-4F-66-66-73-65-74-"
644                                 + "4D-69-6E-75-74-65-73-00-00-0D-07-00-39-75-F8-80-"
645                                 + "FC-C8-08-00-00-0B"),
646 #endif
647                 };
648
649                 static byte[] BitConverter_ByteArray_FromString(String hexString)
650                 {
651                         String[] numbers = hexString.Split('-');
652                         byte[] result = new byte[numbers.Length];
653                         for (int i = 0; i < numbers.Length; ++i) {
654                                 byte x = Byte.Parse (numbers[i], NumberStyles.HexNumber, global::System.Globalization.CultureInfo.InvariantCulture);
655                                 result[i] = x;
656                         }
657                         return result;
658                 }
659                 
660                 [Test]
661                 public void ArithmeticAccrossDSTBoudaries ()
662                 {
663                         DateTime dt = new DateTime (633954393584177800, DateTimeKind.Local); //Dec 3, 2009, 12:16
664                         DateTimeOffset dto = new DateTimeOffset (dt);
665                         DateTimeOffset dto2 = dto.AddDays (-60); //Should cross the late Oct boundary in most part of the world
666                         Assert.AreEqual (dto.Offset, dto2.Offset);
667                         Assert.AreEqual (dt.AddDays (-60), dto2.DateTime);
668                 }
669
670                 [Test]
671                 public void TestPartialDateTimeParsing ()
672                 {
673                         var now = DateTime.UtcNow;
674                         const DateTimeStyles style = DateTimeStyles.AssumeUniversal;
675
676                         //year
677                         var date = DateTimeOffset.ParseExact ("2003", "yyyy", CultureInfo.InvariantCulture, style);
678                         var expected = "01/01/2003 00:00:00 +00:00";
679                         Assert.AreEqual (expected, date.ToString (CultureInfo.InvariantCulture));
680
681                         //month
682                         date = DateTimeOffset.ParseExact ("12", "MM", CultureInfo.InvariantCulture, style);
683                         expected = string.Format ("12/01/{0} 00:00:00 +00:00", now.Year);
684                         Assert.AreEqual (expected, date.ToString (CultureInfo.InvariantCulture));
685
686                         //day
687                         date = DateTimeOffset.ParseExact ("29", "dd", CultureInfo.InvariantCulture, style);
688                         expected = string.Format ("01/29/{0} 00:00:00 +00:00", now.Year);
689                         Assert.AreEqual (expected, date.ToString (CultureInfo.InvariantCulture));
690
691                         //hours
692                         date = DateTimeOffset.ParseExact ("06", "HH", CultureInfo.InvariantCulture, style);
693                         expected = string.Format ("{0:D2}/{1:D2}/{2} 06:00:00 +00:00", now.Month, now.Day, now.Year);
694                         Assert.AreEqual (expected, date.ToString (CultureInfo.InvariantCulture));
695
696                         //minutes
697                         date = DateTimeOffset.ParseExact ("45", "mm", CultureInfo.InvariantCulture, style);
698                         expected = string.Format ("{0:D2}/{1:D2}/{2} 00:45:00 +00:00", now.Month, now.Day, now.Year);
699                         Assert.AreEqual (expected, date.ToString (CultureInfo.InvariantCulture));
700
701                         //seconds
702                         date = DateTimeOffset.ParseExact ("45", "ss", CultureInfo.InvariantCulture, style);
703                         expected = string.Format ("{0:D2}/{1:D2}/{2} 00:00:45 +00:00", now.Month, now.Day, now.Year);
704                         Assert.AreEqual (expected, date.ToString (CultureInfo.InvariantCulture));
705                 }
706
707                 [Test]
708                 public void TestDateOnlyWithTimeOffset ()
709                 {
710                         var fp = CultureInfo.InvariantCulture;
711                         var date = DateTimeOffset.Parse("2013-11-07+11:00", fp, DateTimeStyles.AssumeUniversal);
712                         var expected = string.Format ("{0:D2}/{1:D2}/{2} 00:00:00 +11:00", 11, 7, 2013);
713                         Assert.AreEqual (expected, date.ToString (CultureInfo.InvariantCulture));
714                 }
715
716                 [Test]
717                 public void GMTDateTime ()
718                 {
719                         var date = DateTimeOffset.Parse ("Wed, 10 Sep 2014 22:01:40 GMT", CultureInfo.InvariantCulture);
720                         var expected = "09/10/2014 22:01:40 +00:00";
721                         Assert.AreEqual (expected, date.ToString (CultureInfo.InvariantCulture));
722                 }
723         }
724 }
725