예제 #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);
                }
            }
        }
예제 #2
0
        public void TestUnsignedMinMax()
        {
            var stream = new MsgPackStream();

            stream.WriteUInt8(byte.MinValue);
            stream.WriteUInt8(byte.MaxValue);
            stream.WriteUInt16(ushort.MinValue);
            stream.WriteUInt16(ushort.MaxValue);
            stream.WriteUInt32(uint.MinValue);
            stream.WriteUInt32(uint.MaxValue);
            stream.WriteUInt64(ulong.MinValue);
            stream.WriteUInt64(ulong.MaxValue);

            stream.Position = 0;
            Assert.AreEqual(byte.MinValue, stream.ReadUInt8());
            Assert.AreEqual(byte.MaxValue, stream.ReadUInt8());
            Assert.AreEqual(ushort.MinValue, stream.ReadUInt16());
            Assert.AreEqual(ushort.MaxValue, stream.ReadUInt16());
            Assert.AreEqual(uint.MinValue, stream.ReadUInt32());
            Assert.AreEqual(uint.MaxValue, stream.ReadUInt32());
            Assert.AreEqual(ulong.MinValue, stream.ReadUInt64());
            Assert.AreEqual(ulong.MaxValue, stream.ReadUInt64());
        }
예제 #3
0
        public void TestUnsigned()
        {
            var stream = new MsgPackStream();

            stream.WriteUInt8(255);
            Assert.AreEqual(stream.Position, 1);

            stream.WriteUInt16(12431);
            Assert.AreEqual(stream.Position, 3);

            stream.WriteUInt32(761);
            Assert.AreEqual(stream.Position, 7);

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

            stream.Position = 0;
            Assert.AreEqual(255, stream.ReadUInt8());
            Assert.AreEqual(12431, stream.ReadUInt16());
            Assert.AreEqual(761, stream.ReadUInt32());
            Assert.AreEqual(64, stream.ReadUInt64());
        }