1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2015 Google Inc. All rights reserved.
4 // https://developers.google.com/protocol-buffers/
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 using NUnit.Framework;
36 namespace Google.Protobuf.WellKnownTypes
38 public class TimestampTest
41 public void FromAndToDateTime()
43 DateTime utcMin = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
44 DateTime utcMax = DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc);
45 AssertRoundtrip(new Timestamp { Seconds = -62135596800 }, utcMin);
46 AssertRoundtrip(new Timestamp { Seconds = 253402300799, Nanos = 999999900 }, utcMax);
47 AssertRoundtrip(new Timestamp(), new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
48 AssertRoundtrip(new Timestamp { Nanos = 1000000}, new DateTime(1970, 1, 1, 0, 0, 0, 1, DateTimeKind.Utc));
49 AssertRoundtrip(new Timestamp { Seconds = -1, Nanos = 999000000 }, new DateTime(1969, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc));
50 AssertRoundtrip(new Timestamp { Seconds = 3600 }, new DateTime(1970, 1, 1, 1, 0, 0, DateTimeKind.Utc));
51 AssertRoundtrip(new Timestamp { Seconds = -3600 }, new DateTime(1969, 12, 31, 23, 0, 0, DateTimeKind.Utc));
55 public void ToDateTimeTruncation()
57 var t1 = new Timestamp { Seconds = 1, Nanos = 1000000 + Duration.NanosecondsPerTick - 1 };
58 Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 1, DateTimeKind.Utc).AddMilliseconds(1), t1.ToDateTime());
60 var t2 = new Timestamp { Seconds = -1, Nanos = 1000000 + Duration.NanosecondsPerTick - 1 };
61 Assert.AreEqual(new DateTime(1969, 12, 31, 23, 59, 59).AddMilliseconds(1), t2.ToDateTime());
65 [TestCase(Timestamp.UnixSecondsAtBclMinValue - 1, Timestamp.MaxNanos)]
66 [TestCase(Timestamp.UnixSecondsAtBclMaxValue + 1, 0)]
68 [TestCase(0, Timestamp.MaxNanos + 1)]
69 public void ToDateTime_OutOfRange(long seconds, int nanoseconds)
71 var value = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
72 Assert.Throws<InvalidOperationException>(() => value.ToDateTime());
75 // 1ns larger or smaller than the above values
77 [TestCase(Timestamp.UnixSecondsAtBclMinValue, 0)]
78 [TestCase(Timestamp.UnixSecondsAtBclMaxValue, Timestamp.MaxNanos)]
80 [TestCase(0, Timestamp.MaxNanos)]
81 public void ToDateTime_ValidBoundaries(long seconds, int nanoseconds)
83 var value = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
87 private static void AssertRoundtrip(Timestamp timestamp, DateTime dateTime)
89 Assert.AreEqual(timestamp, Timestamp.FromDateTime(dateTime));
90 Assert.AreEqual(dateTime, timestamp.ToDateTime());
91 Assert.AreEqual(DateTimeKind.Utc, timestamp.ToDateTime().Kind);
95 public void Arithmetic()
97 Timestamp t1 = new Timestamp { Seconds = 10000, Nanos = 5000 };
98 Timestamp t2 = new Timestamp { Seconds = 8000, Nanos = 10000 };
99 Duration difference = new Duration { Seconds = 1999, Nanos = Duration.NanosecondsPerSecond - 5000 };
100 Assert.AreEqual(difference, t1 - t2);
101 Assert.AreEqual(-difference, t2 - t1);
103 Assert.AreEqual(t1, t2 + difference);
104 Assert.AreEqual(t2, t1 - difference);
108 public void ToString_NonNormalized()
110 // Just a single example should be sufficient...
111 var duration = new Timestamp { Seconds = 1, Nanos = -1 };
112 Assert.AreEqual("{ \"@warning\": \"Invalid Timestamp\", \"seconds\": \"1\", \"nanos\": -1 }", duration.ToString());