Beispiel #1
0
        void IFormatter <DateTime> .Write(DateTime value, MsgPackStream stream, IContext context)
        {
            var secondsSinceBclEpoch = value.Ticks / TimeSpan.TicksPerSecond;
            var seconds     = secondsSinceBclEpoch - BclSecondsAtUnixEpoch;
            var nanoseconds = (value.Ticks % TimeSpan.TicksPerSecond) * NanosecondsPerTick;

            // Reference:
            // struct timespec {
            //     long tv_sec;  // seconds
            //     long tv_nsec; // nanoseconds
            // } time;
            // if ((time.tv_sec >> 34) == 0) {
            //     uint64_t data64 = (time.tv_nsec << 34) | time.tv_sec;
            //     if (data64 & 0xffffffff00000000L == 0) {
            //         // timestamp 32
            //         uint32_t data32 = data64;
            //         serialize(0xd6, -1, data32)
            //     }
            //     else {
            //         // timestamp 64
            //         serialize(0xd7, -1, data64)
            //     }
            // }
            // else {
            //     // timestamp 96
            //     serialize(0xc7, 12, -1, time.tv_nsec, time.tv_sec)
            // }

            unchecked
            {
                if (seconds >> 34 == 0)
                {
                    var data64 = (ulong)((nanoseconds << 34) | seconds);
                    if ((data64 & 0xffffffff00000000ul) == 0ul)
                    {
                        var data32 = (uint)data64;
                        StreamWriter.WriteExtensionHeader(new ExtensionHeader(TypeCode, 4u), stream);
                        stream.WriteUInt32(data32);
                    }
                    else
                    {
                        StreamWriter.WriteExtensionHeader(new ExtensionHeader(TypeCode, 8u), stream);
                        stream.WriteUInt64(data64);
                    }
                }
                else
                {
                    StreamWriter.WriteExtensionHeader(new ExtensionHeader(TypeCode, 12u), stream);
                    stream.WriteUInt32((uint)nanoseconds);
                    stream.WriteInt64(seconds);
                }
            }
        }
Beispiel #2
0
        public void TestSignedMinMax()
        {
            var stream = new MsgPackStream();

            stream.WriteInt8(sbyte.MinValue);
            stream.WriteInt8(sbyte.MaxValue);
            stream.WriteInt16(short.MinValue);
            stream.WriteInt16(short.MaxValue);
            stream.WriteInt32(int.MinValue);
            stream.WriteInt32(int.MaxValue);
            stream.WriteInt64(long.MinValue);
            stream.WriteInt64(long.MaxValue);

            stream.Position = 0;
            Assert.AreEqual(sbyte.MinValue, stream.ReadInt8());
            Assert.AreEqual(sbyte.MaxValue, stream.ReadInt8());
            Assert.AreEqual(short.MinValue, stream.ReadInt16());
            Assert.AreEqual(short.MaxValue, stream.ReadInt16());
            Assert.AreEqual(int.MinValue, stream.ReadInt32());
            Assert.AreEqual(int.MaxValue, stream.ReadInt32());
            Assert.AreEqual(long.MinValue, stream.ReadInt64());
            Assert.AreEqual(long.MaxValue, stream.ReadInt64());
        }
Beispiel #3
0
        public void TestSigned()
        {
            var stream = new MsgPackStream();

            stream.WriteInt8(-11);
            Assert.AreEqual(stream.Position, 1);

            stream.WriteInt16(192);
            Assert.AreEqual(stream.Position, 3);

            stream.WriteInt32(-213);
            Assert.AreEqual(stream.Position, 7);

            stream.WriteInt64(64);
            Assert.AreEqual(stream.Position, 15);

            stream.Position = 0;
            Assert.AreEqual(-11, stream.ReadInt8());
            Assert.AreEqual(192, stream.ReadInt16());
            Assert.AreEqual(-213, stream.ReadInt32());
            Assert.AreEqual(64, stream.ReadInt64());
        }