1 // OpenTween - Client of Twitter
2 // Copyright (c) 2018 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
27 public class DateTimeUtcTest
30 public void Constructor_DateTest()
32 var utc = new DateTimeUtc(2018, 5, 6);
34 Assert.Equal(new DateTime(2018, 5, 6, 0, 0, 0, 0, DateTimeKind.Utc),
35 utc.ToDateTimeUnsafe());
39 public void Constructor_DateAndTimeTest()
41 var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33);
43 Assert.Equal(new DateTime(2018, 5, 6, 11, 22, 33, 0, DateTimeKind.Utc),
44 utc.ToDateTimeUnsafe());
48 public void Constructor_DateAndTimeMillisecondsTest()
50 var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 456);
52 Assert.Equal(new DateTime(2018, 5, 6, 11, 22, 33, 456, DateTimeKind.Utc),
53 utc.ToDateTimeUnsafe());
57 public void Constructor_DateTimeOffsetTest()
59 var datetimeOffset = new DateTimeOffset(2018, 5, 6, 11, 22, 33, 456, TimeSpan.FromHours(9));
60 var utc = new DateTimeUtc(datetimeOffset);
62 Assert.Equal(new DateTime(2018, 5, 6, 2, 22, 33, 456, DateTimeKind.Utc),
63 utc.ToDateTimeUnsafe());
67 public void Constructor_DateTimeTest()
69 var datetime = new DateTime(2018, 5, 6, 11, 22, 33, DateTimeKind.Utc);
70 var utc = new DateTimeUtc(datetime);
72 Assert.Equal(datetime, utc.ToDateTimeUnsafe());
76 public void Constructor_LocalDateTimeTest()
78 Assert.Throws<ArgumentException>(
79 () => new DateTimeUtc(new DateTime(2018, 5, 6, 12, 0, 0, DateTimeKind.Local)));
81 Assert.Throws<ArgumentException>(
82 () => new DateTimeUtc(new DateTime(2018, 5, 6, 12, 0, 0, DateTimeKind.Unspecified)));
86 public void ToUnixTime_Test()
88 var utc = new DateTimeUtc(2009, 2, 13, 23, 31, 30, 0);
90 Assert.Equal(1234567890, utc.ToUnixTime());
94 public void ToDateTimeOffset()
96 var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
98 Assert.Equal(new DateTimeOffset(2018, 5, 6, 11, 22, 33, 111, TimeSpan.Zero),
99 utc.ToDateTimeOffset());
103 public void CompareTo_Test()
105 var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
106 var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
107 var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
109 Assert.Equal(0, utc1.CompareTo(utc2));
110 Assert.True(utc1.CompareTo(utc3) < 0);
111 Assert.True(utc3.CompareTo(utc1) > 0);
115 public void Equals_Test()
117 var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
118 var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
119 var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
121 Assert.True(utc1.Equals(utc2));
122 Assert.True(utc1.Equals((object)utc2));
124 Assert.False(utc1.Equals(utc3));
125 Assert.False(utc1.Equals((object)utc3));
129 public void GetHashCode_Test()
131 var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
132 var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
133 var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
135 Assert.Equal(utc1.GetHashCode(), utc2.GetHashCode());
136 Assert.NotEqual(utc1.GetHashCode(), utc3.GetHashCode());
140 public void OperatorPlus_Test()
142 var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
143 var diff = TimeSpan.FromDays(1);
145 Assert.Equal(new DateTime(2018, 5, 7, 11, 22, 33, 111, DateTimeKind.Utc),
146 (utc + diff).ToDateTimeUnsafe());
150 public void OperatorMinus_TimeSpanTest()
152 var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
153 var diff = TimeSpan.FromDays(1);
155 Assert.Equal(new DateTime(2018, 5, 5, 11, 22, 33, 111, DateTimeKind.Utc),
156 (utc - diff).ToDateTimeUnsafe());
160 public void OperatorMinus_DateTimeTest()
162 var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
163 var utc2 = new DateTimeUtc(2018, 5, 7, 11, 22, 33, 111);
165 Assert.Equal(TimeSpan.Zero, utc1 - utc1);
166 Assert.Equal(TimeSpan.FromDays(-1), utc1 - utc2);
167 Assert.Equal(TimeSpan.FromDays(1), utc2 - utc1);
171 public void OperatorEqual_Test()
173 var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
174 var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
176 #pragma warning disable CS1718
177 Assert.True(utc1 == utc1);
178 Assert.False(utc1 == utc2);
180 Assert.False(utc1 != utc1);
181 Assert.True(utc1 != utc2);
182 #pragma warning restore CS1718
186 public void OperatorCompare_Test()
188 var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
189 var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
191 #pragma warning disable CS1718
192 Assert.False(utc1 < utc1);
193 Assert.True(utc1 < utc2);
194 Assert.False(utc2 < utc1);
196 Assert.True(utc1 <= utc1);
197 Assert.True(utc1 <= utc2);
198 Assert.False(utc2 <= utc1);
200 Assert.False(utc1 > utc1);
201 Assert.False(utc1 > utc2);
202 Assert.True(utc2 > utc1);
204 Assert.True(utc1 >= utc1);
205 Assert.False(utc1 >= utc2);
206 Assert.True(utc2 >= utc1);
207 #pragma warning restore CS1718
211 public void MinValue_Test()
212 => Assert.Equal(DateTime.MinValue.Ticks, DateTimeUtc.MinValue.ToDateTimeUnsafe().Ticks);
215 public void MaxValue_Test()
216 => Assert.Equal(DateTime.MaxValue.Ticks, DateTimeUtc.MaxValue.ToDateTimeUnsafe().Ticks);
219 public void FromUnixTime_Test()
221 var utc = DateTimeUtc.FromUnixTime(1234567890);
223 Assert.Equal(new DateTime(2009, 2, 13, 23, 31, 30, 0, DateTimeKind.Utc),
224 utc.ToDateTimeUnsafe());