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());
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());
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());
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"));
152 Assert.AreEqual ("Wednesday, October 31, 2007", dto.ToString ("D"));
153 Assert.AreEqual ("9:00 PM", dto.ToString ("t"));
154 Assert.AreEqual ("9:00:00 PM", dto.ToString ("T"));
155 Assert.AreEqual ("Wednesday, October 31, 2007 9:00 PM", dto.ToString ("f"));
156 Assert.AreEqual ("Wednesday, October 31, 2007 9:00:00 PM", dto.ToString ("F"));
157 Assert.AreEqual ("10/31/2007 9:00 PM", dto.ToString ("g"));
158 Assert.AreEqual ("10/31/2007 9:00:00 PM", dto.ToString ("G"));
159 Assert.AreEqual ("October 31", dto.ToString ("M"));
160 Assert.AreEqual (dto.ToString ("m"), dto.ToString ("M"));
161 Assert.AreEqual ("Thu, 01 Nov 2007 05:00:00 GMT", dto.ToString ("R"));
162 Assert.AreEqual (dto.ToString ("r"), dto.ToString ("R"));
163 Assert.AreEqual ("2007-10-31T21:00:00", dto.ToString ("s"));
164 Assert.AreEqual ("2007-11-01 05:00:00Z", dto.ToString ("u"));
165 Assert.AreEqual ("October, 2007", dto.ToString ("Y"));
166 Assert.AreEqual (dto.ToString ("y"), dto.ToString ("Y"));
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")));