Пример #1
0
        private void DateTime_WellKnownEquiv(TypeModel model, DateTime when)
        {
            var time = when - epoch;

            var seconds = time.TotalSeconds > 0 ? (long)Math.Floor(time.TotalSeconds) : Math.Ceiling(time.TotalSeconds);
            var nanos   = (int)(((time.Ticks % TimeSpan.TicksPerSecond) * 1000000)
                                / TimeSpan.TicksPerMillisecond);

            if (nanos < 0)
            {
                seconds--;
                nanos += 1000000000;
            }

            // convert forwards and compare
            var hazDt = new HasDateTime {
                Value = when
            };
            var hazTs = Serializer.ChangeType <HasDateTime, HasTimestamp>(hazDt);

            Assert.Equal(seconds, hazTs.Value?.Seconds ?? 0);
            Assert.Equal(nanos, hazTs.Value?.Nanos ?? 0);

            // and back again
            hazDt = Serializer.ChangeType <HasTimestamp, HasDateTime>(hazTs);
            Assert.Equal(when, hazDt.Value);
        }
Пример #2
0
        private void AssertKnownValue(Timestamp value, DateTime expected)
        {
            var obj = new HasTimestamp {
                Value = value
            };

            Assert.Equal(expected, ChangeType <HasTimestamp, HasDateTime>(runtime, obj).Value);
            Assert.Equal(expected, ChangeType <HasTimestamp, HasDateTime>(dynamicMethod, obj).Value);
            Assert.Equal(expected, ChangeType <HasTimestamp, HasDateTime>(fullyCompiled, obj).Value);

            var obj2 = new HasDateTime {
                Value = expected
            };
            var other = ChangeType <HasDateTime, HasTimestamp>(runtime, obj2).Value ?? new Timestamp();

            Assert.Equal(value.Seconds, other.Seconds);
            Assert.Equal(value.Nanos, other.Nanos);

            other = ChangeType <HasDateTime, HasTimestamp>(dynamicMethod, obj2).Value ?? new Timestamp();
            Assert.Equal(value.Seconds, other.Seconds);
            Assert.Equal(value.Nanos, other.Nanos);

            other = ChangeType <HasDateTime, HasTimestamp>(fullyCompiled, obj2).Value ?? new Timestamp();
            Assert.Equal(value.Seconds, other.Seconds);
            Assert.Equal(value.Nanos, other.Nanos);
        }
Пример #3
0
        private void DateTime_WellKnownEquiv(TypeModel model, DateTime when)
        {
            var time = when - epoch;

            var seconds = time.TotalSeconds > 0 ? (long)Math.Floor(time.TotalSeconds) : Math.Ceiling(time.TotalSeconds);
            var nanos   = (int)(((time.Ticks % TimeSpan.TicksPerSecond) * 1000000)
                                / TimeSpan.TicksPerMillisecond);

            if (nanos < 0)
            {
                seconds--;
                nanos += 1000000000;
            }

            // convert forwards and compare
            var hazDt = new HasDateTime {
                Value = when
            };
            HasTimestamp hazTs;

            using (var ms = new MemoryStream())
            {
                model.Serialize(ms, hazDt);
                ms.Position = 0;
#pragma warning disable CS0618
                hazTs = (HasTimestamp)model.Deserialize(ms, null, typeof(HasTimestamp));
#pragma warning restore CS0618
            }
            Assert.Equal(seconds, hazTs.Value?.Seconds ?? 0);
            Assert.Equal(nanos, hazTs.Value?.Nanos ?? 0);

            // and back again
            hazDt = Serializer.ChangeType <HasTimestamp, HasDateTime>(hazTs);
            Assert.Equal(when, hazDt.Value);
        }
Пример #4
0
        public void DateTime_NullStaysNull()
        {
            var orig = new HasDateTime();

            Assert.Null(orig.Value);

            var hd = ChangeType <HasDateTime, HasTimestamp>(runtime, orig);

            Assert.Null(hd.Value);
            var clone = ChangeType <HasTimestamp, HasDateTime>(runtime, hd);

            Assert.Null(clone.Value);

            hd = ChangeType <HasDateTime, HasTimestamp>(dynamicMethod, orig);
            Assert.Null(hd.Value);
            clone = ChangeType <HasTimestamp, HasDateTime>(dynamicMethod, hd);
            Assert.Null(clone.Value);

            hd = ChangeType <HasDateTime, HasTimestamp>(fullyCompiled, orig);
            Assert.Null(hd.Value);
            clone = ChangeType <HasTimestamp, HasDateTime>(fullyCompiled, hd);
            Assert.Null(clone.Value);
        }