2 // DateTimeOffsetTest.cs - NUnit Test Cases for the System.DateTimeOffset struct
5 // Stephane Delcroix (sdelcroix@novell.com)
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
11 using System.Globalization;
12 using NUnit.Framework;
15 namespace MonoTests.System {
18 public class DateTimeOffsetTest
20 //ctor exception checking...
22 [ExpectedException (typeof (ArgumentException))]
23 public void UtcWithWrongOffset ()
25 DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Utc);
26 TimeSpan offset = new TimeSpan (2, 0, 0);
27 new DateTimeOffset (dt, offset);
31 [ExpectedException (typeof (ArgumentException))]
32 public void LocalWithWrongOffset ()
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);
40 [ExpectedException (typeof (ArgumentException))]
41 public void OffsetNotInWholeminutes ()
43 DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Unspecified);
44 TimeSpan offset = new TimeSpan (1,0,59);
45 new DateTimeOffset (dt, offset);
49 [ExpectedException (typeof (ArgumentOutOfRangeException))]
50 public void OffsetOutOfRange1 ()
52 DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Unspecified);
53 TimeSpan offset = new TimeSpan (14, 1, 0);
54 new DateTimeOffset (dt, offset);
58 [ExpectedException (typeof (ArgumentOutOfRangeException))]
59 public void OffsetOutOfRange2 ()
61 DateTime dt = DateTime.SpecifyKind (DateTime.Now, DateTimeKind.Unspecified);
62 TimeSpan offset = new TimeSpan (-14, -1, 0);
63 new DateTimeOffset (dt, offset);
67 [ExpectedException (typeof (ArgumentOutOfRangeException))]
68 public void UtcDateTimeOutOfRange1 ()
70 DateTime dt = DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Unspecified);
71 TimeSpan offset = new TimeSpan (1, 0, 0);
72 new DateTimeOffset (dt, offset);
76 [ExpectedException (typeof (ArgumentOutOfRangeException))]
77 public void UtcDateTimeOutOfRange2 ()
79 DateTime dt = DateTime.SpecifyKind (DateTime.MaxValue, DateTimeKind.Unspecified);
80 TimeSpan offset = new TimeSpan (-1, 0, 0);
81 new DateTimeOffset (dt, offset);
85 public void CompareTwoDateInDiffTZ ()
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));
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);
103 public void UtcDateTime ()
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));
112 public void ParameterlessToString ()
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")));
123 public void ToStringWithCultureInfo ()
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")));
134 [ExpectedException (typeof (FormatException))]
135 public void ToStringExceptionalCase1 ()
137 DateTimeOffset.Now.ToString (";");
141 [ExpectedException (typeof (FormatException))]
142 public void ToStringExceptionalCase2 ()
144 DateTimeOffset.Now.ToString ("U");
148 public void ToStringWithFormat ()
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")));
170 public void ToStringWithFormatAndCulture ()
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")));
181 public void ParseExactDatesOnly ()
183 DateTimeOffset dto = DateTimeOffset.Now;
184 CultureInfo fp = CultureInfo.InvariantCulture;
199 foreach (string format in formats)
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);
209 public void ParseExactTest ()
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)),
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",
237 foreach (DateTimeOffset dto in dtos)
238 foreach (string format in formats)
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);
246 public void ParseExactYearFormat ()
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)),
260 "M/d/yyyyyy H:m zzz",
261 "M/d/yyyyyyy H:m zzz",
264 foreach (DateTimeOffset dto in dtos)
265 foreach (string format in formats)
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);
275 [ExpectedException (typeof (FormatException))]
276 public void ParseExactYearException ()
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);
285 [ExpectedException (typeof (FormatException))]
286 public void ParseExactFormatException1 ()
288 CultureInfo fp = CultureInfo.InvariantCulture;
290 string date = "1/14/2008";
291 DateTimeOffset.ParseExact(date, format, fp);
295 [ExpectedException (typeof (FormatException))]
296 public void ParseExactFormatException2 ()
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);
305 [ExpectedException (typeof (FormatException))]
306 public void ParseExactFormatException3 ()
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);
315 [ExpectedException (typeof (FormatException))]
316 public void ParseExactFormatException4 ()
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);
325 public void ParseExactWithSeconds ()
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)),
334 "dddd, dd MMMM yyyy HH:mm:ss zzz",
337 foreach (DateTimeOffset dto in dtos)
338 foreach (string format in formats)
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);
347 public void ParseExactWithFractions ()
349 CultureInfo fp = CultureInfo.InvariantCulture;
350 DateTimeOffset[] dtos = {
351 new DateTimeOffset (2008, 1, 15, 14, 36, 44, 900, TimeSpan.Zero),
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",
371 foreach (DateTimeOffset dto in dtos)
372 foreach (string format in formats)
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);
381 public void EqualsObject ()
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");