2008-01-21 Sebastien Pouliot <sebastien@ximian.com>
[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 //
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
8 //
9
10 #if NET_2_0
11 using System.Globalization;
12 using NUnit.Framework;
13 using System;
14
15 namespace MonoTests.System {
16
17         [TestFixture]
18         public class DateTimeOffsetTest
19         {
20                 //ctor exception checking...
21                 [Test]
22                 [ExpectedException (typeof (ArgumentException))]
23                 public void UtcWithWrongOffset ()
24                 {
25                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Utc);
26                         TimeSpan offset = new TimeSpan (2, 0, 0);
27                         new DateTimeOffset (dt, offset);
28                 }
29
30                 [Test]
31                 [ExpectedException (typeof (ArgumentException))]
32                 public void LocalWithWrongOffset ()
33                 {
34                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Local);
35                         TimeSpan offset = TimeZone.CurrentTimeZone.GetUtcOffset (dt) + new TimeSpan (1,0,0);
36                         new DateTimeOffset (dt, offset);
37                 }
38
39                 [Test]
40                 [ExpectedException (typeof (ArgumentException))]
41                 public void OffsetNotInWholeminutes ()
42                 {
43                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Unspecified);
44                         TimeSpan offset = new TimeSpan (1,0,59);
45                         new DateTimeOffset (dt, offset);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
50                 public void OffsetOutOfRange1 ()
51                 {
52                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Unspecified);
53                         TimeSpan offset = new TimeSpan (14, 1, 0);
54                         new DateTimeOffset (dt, offset);
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
59                 public void OffsetOutOfRange2 ()
60                 {
61                         DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Unspecified);
62                         TimeSpan offset = new TimeSpan (-14, -1, 0);
63                         new DateTimeOffset (dt, offset);
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
68                 public void UtcDateTimeOutOfRange1 ()
69                 {
70                         DateTime dt = DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Unspecified);
71                         TimeSpan offset = new TimeSpan (1, 0, 0);
72                         new DateTimeOffset (dt, offset);
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
77                 public void UtcDateTimeOutOfRange2 ()
78                 {
79                         DateTime dt = DateTime.SpecifyKind (DateTime.MaxValue, DateTimeKind.Unspecified);
80                         TimeSpan offset = new TimeSpan (-1, 0, 0);
81                         new DateTimeOffset (dt, offset);
82                 }
83
84                 [Test]
85                 public void CompareTwoDateInDiffTZ ()
86                 {
87                         DateTimeOffset dt1 = new DateTimeOffset (2007, 12, 16, 15, 06, 00, new TimeSpan (1, 0, 0));
88                         DateTimeOffset dt2 = new DateTimeOffset (2007, 12, 16, 9, 06, 00, new TimeSpan (-5, 0, 0));
89                         DateTimeOffset dt3 = new DateTimeOffset (2007, 12, 16, 14, 06, 00, new TimeSpan (1, 0, 0));
90                         object o = dt1;
91                         Assert.IsTrue (dt1.CompareTo (dt2) == 0);
92                         Assert.IsTrue (DateTimeOffset.Compare (dt1, dt2) == 0);
93                         Assert.IsTrue (dt1 == dt2);
94                         Assert.IsTrue (dt1.Equals (dt2));
95                         Assert.IsFalse (dt1 == dt3);
96                         Assert.IsTrue (dt1 != dt3);
97                         Assert.IsFalse (dt1.EqualsExact (dt2));
98                         Assert.IsTrue (dt1.CompareTo (dt3) > 0);
99                         Assert.IsTrue (dt1.CompareTo (o) == 0);
100                 }
101
102                 [Test]
103                 public void UtcDateTime ()
104                 {
105                         DateTimeOffset dt = new DateTimeOffset (2007, 12, 16, 15, 49, 00, new TimeSpan (1, 0, 0));
106                         object o = dt.UtcDateTime;
107                         Assert.IsTrue (o is DateTime);
108                         Assert.IsTrue (dt.UtcDateTime == new DateTime (2007,12,16,14,49,00,DateTimeKind.Utc));
109                 }
110
111                 [Test]
112                 public void ParameterlessToString ()
113                 {
114                         DateTimeOffset dt = new DateTimeOffset (2007, 12, 18, 12, 16, 30, new TimeSpan (1, 0, 0));
115                         Assert.AreEqual ("12/18/2007 12:16:30 PM +01:00", dt.ToString (new CultureInfo ("en-us")));
116                         dt = new DateTimeOffset (2007, 12, 18, 12, 16, 30, new TimeSpan (-5, 0, 0));
117                         Assert.AreEqual ("12/18/2007 12:16:30 PM -05:00", dt.ToString (new CultureInfo ("en-us")));
118                         dt = new DateTimeOffset (2007, 12, 18, 12, 16, 30, TimeSpan.Zero);
119                         Assert.AreEqual ("12/18/2007 12:16:30 PM +00:00", dt.ToString (new CultureInfo ("en-us")));
120                 }
121
122                 [Test]
123                 public void ToStringWithCultureInfo ()
124                 {
125                         DateTimeOffset dto = new DateTimeOffset(2007, 5, 1, 9, 0, 0, TimeSpan.Zero);
126                         Assert.AreEqual ("05/01/2007 09:00:00 +00:00", dto.ToString (CultureInfo.InvariantCulture));
127                         Assert.AreEqual ("5/1/2007 9:00:00 AM +00:00", dto.ToString (new CultureInfo ("en-us")));
128                         Assert.AreEqual ("01/05/2007 09:00:00 +00:00", dto.ToString (new CultureInfo ("fr-fr")));
129                         Assert.AreEqual ("01.05.2007 09:00:00 +00:00", dto.ToString (new CultureInfo ("de-DE")));
130                         Assert.AreEqual ("01/05/2007 9:00:00 +00:00", dto.ToString (new CultureInfo ("es-ES")));
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (FormatException))]
135                 public void ToStringExceptionalCase1 ()
136                 {
137                         DateTimeOffset.Now.ToString (";");
138                 }
139
140                 [Test]
141                 [ExpectedException (typeof (FormatException))]
142                 public void ToStringExceptionalCase2 ()
143                 {
144                         DateTimeOffset.Now.ToString ("U");
145                 }
146
147                 [Test]
148                 public void ToStringWithFormat ()
149                 {
150                         DateTimeOffset dto = new DateTimeOffset (2007, 10, 31, 21, 0, 0, new TimeSpan(-8, 0, 0));
151                         Assert.AreEqual ("10/31/2007", dto.ToString ("d", new CultureInfo ("en-us")));
152                         Assert.AreEqual ("Wednesday, October 31, 2007", dto.ToString ("D", new CultureInfo ("en-us")));
153                         Assert.AreEqual ("9:00 PM", dto.ToString ("t", new CultureInfo ("en-us")));
154                         Assert.AreEqual ("9:00:00 PM", dto.ToString ("T", new CultureInfo ("en-us")));
155                         Assert.AreEqual ("Wednesday, October 31, 2007 9:00 PM", dto.ToString ("f", new CultureInfo ("en-us")));
156                         Assert.AreEqual ("Wednesday, October 31, 2007 9:00:00 PM", dto.ToString ("F", new CultureInfo ("en-us")));
157                         Assert.AreEqual ("10/31/2007 9:00 PM", dto.ToString ("g", new CultureInfo ("en-us")));
158                         Assert.AreEqual ("10/31/2007 9:00:00 PM", dto.ToString ("G", new CultureInfo ("en-us")));
159                         Assert.AreEqual ("October 31", dto.ToString ("M", new CultureInfo ("en-us")));
160                         Assert.AreEqual (dto.ToString ("m", new CultureInfo ("en-us")), dto.ToString ("M", new CultureInfo ("en-us")));
161                         Assert.AreEqual ("Thu, 01 Nov 2007 05:00:00 GMT", dto.ToString ("R", new CultureInfo ("en-us")));
162                         Assert.AreEqual (dto.ToString ("r", new CultureInfo ("en-us")), dto.ToString ("R", new CultureInfo ("en-us")));
163                         Assert.AreEqual ("2007-10-31T21:00:00", dto.ToString ("s", new CultureInfo ("en-us")));
164                         Assert.AreEqual ("2007-11-01 05:00:00Z", dto.ToString ("u", new CultureInfo ("en-us")));
165                         Assert.AreEqual ("October, 2007", dto.ToString ("Y", new CultureInfo ("en-us")));
166                         Assert.AreEqual (dto.ToString ("y", new CultureInfo ("en-us")), dto.ToString ("Y", new CultureInfo ("en-us")));
167                 }
168
169                 [Test]
170                 public void ToStringWithFormatAndCulture ()
171                 {
172                         DateTimeOffset dto = new DateTimeOffset (2007, 11, 1, 9, 0, 0, new TimeSpan(-7, 0, 0));
173                         string format = "dddd, MMM dd yyyy HH:mm:ss zzz";
174                         Assert.AreEqual ("Thursday, Nov 01 2007 09:00:00 -07:00", dto.ToString (format, null as DateTimeFormatInfo));
175                         Assert.AreEqual ("Thursday, Nov 01 2007 09:00:00 -07:00", dto.ToString (format, CultureInfo.InvariantCulture));
176                         Assert.AreEqual ("jeudi, nov. 01 2007 09:00:00 -07:00", dto.ToString (format, new CultureInfo ("fr-FR")));
177                         Assert.AreEqual ("jueves, nov 01 2007 09:00:00 -07:00", dto.ToString (format, new CultureInfo ("es-ES")));
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",
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                         };
236
237                         foreach (DateTimeOffset dto in dtos)
238                                 foreach (string format in formats)
239                                 {
240                                         string serialized = dto.ToString (format, fp);
241                                         //Console.WriteLine ("{0} {1} {2}", format, dto, serialized);
242                                         Assert.AreEqual (dto, DateTimeOffset.ParseExact (serialized, format, fp), format);
243                                 }
244                 }
245                 [Test]
246                 public void ParseExactYearFormat ()
247                 {
248                         CultureInfo fp = CultureInfo.InvariantCulture;
249                         DateTimeOffset[] dtos = {
250                                 new DateTimeOffset (2008, 01, 14, 13, 21, 0, new TimeSpan (1,0,0)),
251                                 new DateTimeOffset (2008, 01, 14, 13, 21, 0, new TimeSpan (-5,0,0)),
252                                 new DateTimeOffset (1978, 02, 16, 13, 21, 0, new TimeSpan (2,0,0)),
253                                 new DateTimeOffset (999, 9, 9, 9, 9, 0, new TimeSpan (3,0,0)),
254                         };
255
256                         string[] formats = {
257                                 "M/d/yyy H:m zzz",
258                                 "M/d/yyyy H:m zzz",
259                                 "M/d/yyyyy H:m zzz",
260                                 "M/d/yyyyyy H:m zzz",
261                                 "M/d/yyyyyyy H:m zzz",
262                         };
263
264                         foreach (DateTimeOffset dto in dtos)
265                                 foreach (string format in formats)
266                                 {
267                                         string serialized = dto.ToString (format, fp);
268                                         //Console.WriteLine ("{0} {1} {2}", format, dto, serialized);
269                                         Assert.AreEqual (dto, DateTimeOffset.ParseExact (serialized, format, fp), format);
270                                 }
271                         
272                 }
273
274                 [Test]
275                 [ExpectedException (typeof (FormatException))]
276                 public void ParseExactYearException ()
277                 {
278                         CultureInfo fp = CultureInfo.InvariantCulture;
279                         string format = "M/d/yyyy H:m zzz";
280                         string date = "1/15/999 10:58 +01:00";
281                         DateTimeOffset.ParseExact(date, format, fp);    
282                 }
283
284                 [Test]
285                 [ExpectedException (typeof (FormatException))]
286                 public void ParseExactFormatException1 ()
287                 {
288                         CultureInfo fp = CultureInfo.InvariantCulture;
289                         string format = "d";
290                         string date = "1/14/2008";
291                         DateTimeOffset.ParseExact(date, format, fp);
292                 }
293
294                 [Test]
295                 [ExpectedException (typeof (FormatException))]
296                 public void ParseExactFormatException2 ()
297                 {
298                         CultureInfo fp = CultureInfo.InvariantCulture;
299                         string format = "ddd dd MMM yyyy h:mm tt zzz";
300                         string date = "Mon 14 Jan 2008 2:56 PM +01";
301                         DateTimeOffset.ParseExact(date, format, fp);
302                 }
303
304                 [Test]
305                 [ExpectedException (typeof (FormatException))]
306                 public void ParseExactFormatException3 ()
307                 {
308                         CultureInfo fp = CultureInfo.InvariantCulture;
309                         string format = "ddd dd MMM yyyy h:mm tt zzz";
310                         string date = "Mon 14 Jan 2008 2:56 PM +01:1";
311                         DateTimeOffset.ParseExact(date, format, fp);
312                 }
313
314                 [Test]
315                 [ExpectedException (typeof (FormatException))]
316                 public void ParseExactFormatException4 ()
317                 {
318                         CultureInfo fp = CultureInfo.InvariantCulture;
319                         string format = "ddd dd MMM yyyy h:mm tt zzz";
320                         string date = "Mon 14 Jan 2008 2:56 PM +01: 00";
321                         DateTimeOffset.ParseExact(date, format, fp);
322                 }
323
324                 [Test]
325                 public void ParseExactWithSeconds ()
326                 {
327                         CultureInfo fp = CultureInfo.InvariantCulture;
328                         DateTimeOffset[] dtos = {
329                                 new DateTimeOffset (2008, 01, 14, 13, 21, 25, new TimeSpan (1,0,0)),
330                                 new DateTimeOffset (2008, 01, 14, 13, 21, 5, new TimeSpan (-5,0,0)),
331                         };
332
333                         string[] formats = {
334                                 "dddd, dd MMMM yyyy HH:mm:ss zzz",
335                         };
336
337                         foreach (DateTimeOffset dto in dtos)
338                                 foreach (string format in formats)
339                                 {
340                                         string serialized = dto.ToString (format, fp);
341                                         //Console.WriteLine ("{0} {1} {2}", format, dto, serialized);
342                                         Assert.AreEqual (dto, DateTimeOffset.ParseExact (serialized, format, fp), format);
343                                 }
344                 }
345
346                 [Test]
347                 public void ParseExactWithFractions ()
348                 {
349                         CultureInfo fp = CultureInfo.InvariantCulture;
350                         DateTimeOffset[] dtos = {
351                                 new DateTimeOffset (2008, 1, 15, 14, 36, 44, 900, TimeSpan.Zero),
352                         };
353
354                         string[] formats = {
355                                 "M/d/yyyy H:m:ss,f zzz",
356                                 "M/d/yyyy H:m:ss,ff zzz",
357                                 "M/d/yyyy H:m:ss,fff zzz",
358                                 "M/d/yyyy H:m:ss,ffff zzz",
359                                 "M/d/yyyy H:m:ss,fffff zzz",
360                                 "M/d/yyyy H:m:ss,ffffff zzz",
361                                 "M/d/yyyy H:m:ss,fffffff zzz",
362                                 "M/d/yyyy H:m:ss,F zzz",
363                                 "M/d/yyyy H:m:ss,FF zzz",
364                                 "M/d/yyyy H:m:ss,FFF zzz",
365                                 "M/d/yyyy H:m:ss,FFFF zzz",
366                                 "M/d/yyyy H:m:ss,FFFFF zzz",
367                                 "M/d/yyyy H:m:ss,FFFFFF zzz",
368                                 "M/d/yyyy H:m:ss,FFFFFFF zzz",
369                         };
370
371                         foreach (DateTimeOffset dto in dtos)
372                                 foreach (string format in formats)
373                                 {
374                                         string serialized = dto.ToString (format, fp);
375                                         //Console.WriteLine ("{0} {1} {2}", format, dto, serialized);
376                                         Assert.AreEqual (dto, DateTimeOffset.ParseExact (serialized, format, fp), format);
377                                 }
378                 }
379
380                 [Test]
381                 public void EqualsObject ()
382                 {
383                         DateTimeOffset offset1 = new DateTimeOffset ();
384                         Assert.IsFalse (offset1.Equals (null), "null"); // found using Gendarme :)
385                         Assert.IsTrue (offset1.Equals (offset1), "self");
386                         DateTimeOffset offset2 = new DateTimeOffset (DateTime.Today);
387                         Assert.IsFalse (offset1.Equals (offset2), "1!=2");
388                         Assert.IsFalse (offset2.Equals (offset1), "2!=1");
389                 }
390         }
391 }
392 #endif
393