public void ToDateTime_OutOfRange(long seconds, int nanoseconds) { var value = new Timestamp { Seconds = seconds, Nanos = nanoseconds }; Assert.Throws<InvalidOperationException>(() => value.ToDateTime()); }
public void ToDateTimeTruncation() { var t1 = new Timestamp { Seconds = 1, Nanos = 1000000 + Duration.NanosecondsPerTick - 1 }; Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 1, DateTimeKind.Utc).AddMilliseconds(1), t1.ToDateTime()); var t2 = new Timestamp { Seconds = -1, Nanos = 1000000 + Duration.NanosecondsPerTick - 1 }; Assert.AreEqual(new DateTime(1969, 12, 31, 23, 59, 59).AddMilliseconds(1), t2.ToDateTime()); }
public static DateTime ToDateTime(this Timestamp timestamp) { var protoTimestamp = new Google.Protobuf.WellKnownTypes.Timestamp { Nanos = timestamp.Nanos, Seconds = timestamp.Seconds }; return(protoTimestamp.ToDateTime()); }
private static void AssertRoundtrip(Timestamp timestamp, DateTime dateTime) { Assert.AreEqual(timestamp, Timestamp.FromDateTime(dateTime)); Assert.AreEqual(dateTime, timestamp.ToDateTime()); Assert.AreEqual(DateTimeKind.Utc, timestamp.ToDateTime().Kind); }
public void ToDateTime_ValidBoundaries(long seconds, int nanoseconds) { var value = new Timestamp { Seconds = seconds, Nanos = nanoseconds }; value.ToDateTime(); }
private void WriteTimestamp(StringBuilder builder, IMessage value) { builder.Append('"'); // TODO: In the common case where this *is* using the built-in Timestamp type, we could // avoid all the reflection at this point, by casting to Timestamp. In the interests of // avoiding subtle bugs, don't do that until we've implemented DynamicMessage so that we can prove // it still works in that case. int nanos = (int) value.Descriptor.Fields[Timestamp.NanosFieldNumber].Accessor.GetValue(value); long seconds = (long) value.Descriptor.Fields[Timestamp.SecondsFieldNumber].Accessor.GetValue(value); // Even if the original message isn't using the built-in classes, we can still build one... and its // conversion will check whether or not it's normalized. // TODO: Perhaps the diagnostic-only formatter should not throw for non-normalized values? Timestamp ts = new Timestamp { Seconds = seconds, Nanos = nanos }; // Use .NET's formatting for the value down to the second, including an opening double quote (as it's a string value) DateTime dateTime = ts.ToDateTime(); builder.Append(dateTime.ToString("yyyy'-'MM'-'dd'T'HH:mm:ss", CultureInfo.InvariantCulture)); AppendNanoseconds(builder, Math.Abs(ts.Nanos)); builder.Append("Z\""); }