FromDateTime() public static method

Convert specified DateTime to Int64 as MessagePack defacto-standard.
public static FromDateTime ( System.DateTime value ) : long
value System.DateTime .
return long
Esempio n. 1
0
 public void TestFromDateTime_MaxValue_AsUnixEpoc()
 {
     Assert.AreEqual(
         checked ((UtcMaxValue.ToUniversalTime().Subtract(UtcEpoc).Ticks / TicksToMilliseconds)),
         MessagePackConvert.FromDateTime(UtcMaxValue.ToUniversalTime())
         );
 }
Esempio n. 2
0
 public void TestFromDateTime_UtcEpoc_Zero()
 {
     Assert.AreEqual(
         0L,
         MessagePackConvert.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
         );
 }
Esempio n. 3
0
 public void TestFromDateTime_UtcNow_AsUnixEpoc()
 {
     Assert.AreEqual(
         checked (DateTime.UtcNow.Subtract(UtcEpoc).Ticks / TicksToMilliseconds),
         MessagePackConvert.FromDateTime(DateTime.UtcNow)
         );
 }
Esempio n. 4
0
 public void TestFromDateTime_MaxValue_AsUnixEpoc()
 {
     Assert.AreEqual(
         checked (( long )(DateTime.MaxValue.ToUniversalTime().Subtract(_utcEpoc).TotalMilliseconds)),
         MessagePackConvert.FromDateTime(DateTime.MaxValue.ToUniversalTime())
         );
 }
Esempio n. 5
0
 public void TestFromDateTime_UtcNow_AsUnixEpoc()
 {
     Assert.AreEqual(
         checked (( long )DateTime.UtcNow.Subtract(_utcEpoc).TotalMilliseconds),
         MessagePackConvert.FromDateTime(DateTime.UtcNow)
         );
 }
Esempio n. 6
0
        public void TestIssue43()
        {
            var expected = new DateTime(9999, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc);
            var actual   = new DateTime(3155378975999999999L, DateTimeKind.Utc);

            Assert.AreEqual(
                MessagePackConvert.ToDateTime(MessagePackConvert.FromDateTime(expected)),
                MessagePackConvert.ToDateTime(MessagePackConvert.FromDateTime(actual))
                );
        }
Esempio n. 7
0
        public void TestFromDateTime_Now_AsUtcUnixEpoc()
        {
            // LocalTime will be converted to UtcTime
            var now = DateTime.Now;

            Assert.AreEqual(
                checked (now.ToUniversalTime().Subtract(UtcEpoc).Ticks / TicksToMilliseconds),
                MessagePackConvert.FromDateTime(now)
                );
        }
        private static MessagePackObject?TryValidateObjectArgument(object value)
        {
            if (value == null)
            {
                return(MessagePackObject.Nil);
            }

            if (value is MessagePackObject)
            {
                return(( MessagePackObject )value);
            }

            byte[] asBytes;
            if ((asBytes = value as byte[]) != null)
            {
                return(asBytes);
            }

            string asString;

            if ((asString = value as string) != null)
            {
                return(asString);
            }

            MessagePackString asMessagePackString;

            if ((asMessagePackString = value as MessagePackString) != null)
            {
                return(new MessagePackObject(asMessagePackString));
            }

#if (NETSTANDARD1_1 || NETSTANDARD1_3)
            switch (NetStandardCompatibility.GetTypeCode(value.GetType()))
#else
            switch (Type.GetTypeCode(value.GetType()))
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
            {
            case TypeCode.Boolean:
            {
                return(( bool )value);
            }

            case TypeCode.Byte:
            {
                return(( byte )value);
            }

            case TypeCode.DateTime:
            {
                return(MessagePackConvert.FromDateTime(( DateTime )value));
            }

            case TypeCode.DBNull:
            case TypeCode.Empty:
            {
                return(MessagePackObject.Nil);
            }

            case TypeCode.Double:
            {
                return(( double )value);
            }

            case TypeCode.Int16:
            {
                return(( short )value);
            }

            case TypeCode.Int32:
            {
                return(( int )value);
            }

            case TypeCode.Int64:
            {
                return(( long )value);
            }

            case TypeCode.SByte:
            {
                return(( sbyte )value);
            }

            case TypeCode.Single:
            {
                return(( float )value);
            }

            case TypeCode.String:
            {
                return(value.ToString());
            }

            case TypeCode.UInt16:
            {
                return(( ushort )value);
            }

            case TypeCode.UInt32:
            {
                return(( uint )value);
            }

            case TypeCode.UInt64:
            {
                return(( ulong )value);
            }

            // ReSharper disable RedundantCaseLabel
            case TypeCode.Char:
            case TypeCode.Decimal:
            case TypeCode.Object:
            // ReSharper restore RedundantCaseLabel
            default:
            {
                return(null);
            }
            }
        }
        private static MessagePackObject?TryValidateObjectArgument(object value)
        {
            if (value == null)
            {
                return(new MessagePackObject?(MessagePackObject.Nil));
            }
            if (value is MessagePackObject)
            {
                return(new MessagePackObject?((MessagePackObject)value));
            }
            if (value is MessagePackObject?)
            {
                MessagePackObject?nullable2 = (MessagePackObject?)value;
                return(new MessagePackObject?(nullable2.HasValue ? nullable2.GetValueOrDefault() : MessagePackObject.Nil));
            }
            byte[] buffer = value as byte[];
            if (buffer != null)
            {
                return(new MessagePackObject?(buffer));
            }
            string str = value as string;

            if (str != null)
            {
                return(new MessagePackObject?(str));
            }
            MessagePackString messagePackString = value as MessagePackString;

            if (messagePackString != null)
            {
                return(new MessagePackObject(messagePackString));
            }
            switch (Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.Empty:
            case TypeCode.DBNull:
                return(new MessagePackObject?(MessagePackObject.Nil));

            case TypeCode.Boolean:
                return(new MessagePackObject?((bool)value));

            case TypeCode.SByte:
                return(new MessagePackObject?((sbyte)value));

            case TypeCode.Byte:
                return(new MessagePackObject?((byte)value));

            case TypeCode.Int16:
                return(new MessagePackObject?((short)value));

            case TypeCode.UInt16:
                return(new MessagePackObject?((ushort)value));

            case TypeCode.Int32:
                return(new MessagePackObject?((int)value));

            case TypeCode.UInt32:
                return(new MessagePackObject?((uint)value));

            case TypeCode.Int64:
                return(new MessagePackObject?((long)value));

            case TypeCode.UInt64:
                return(new MessagePackObject?((ulong)value));

            case TypeCode.Single:
                return(new MessagePackObject?((float)value));

            case TypeCode.Double:
                return(new MessagePackObject?((double)value));

            case TypeCode.DateTime:
                return(new MessagePackObject?(MessagePackConvert.FromDateTime((DateTime)value)));

            case TypeCode.String:
                return(new MessagePackObject?(value.ToString()));
            }
            return(null);
        }
Esempio n. 10
0
        private static MessagePackObject?TryValidateObjectArgument(object value)
        {
            if (value == null)
            {
                return(MessagePackObject.Nil);
            }

            if (value is MessagePackObject)
            {
                return(( MessagePackObject )value);
            }

            if (value is MessagePackObject? )
            {
                return(( MessagePackObject? )value ?? MessagePackObject.Nil);
            }

            byte[] asBytes;
            if ((asBytes = value as byte[]) != null)
            {
                return(asBytes);
            }

            string asString;

            if ((asString = value as string) != null)
            {
                return(asString);
            }

            MessagePackString asMessagePackString;

            if ((asMessagePackString = value as MessagePackString) != null)
            {
                return(new MessagePackObject(asMessagePackString));
            }

#if NETFX_CORE
            switch (WinRTCompatibility.GetTypeCode(value.GetType()))
#else
            switch (Type.GetTypeCode(value.GetType()))
#endif
            {
            case TypeCode.Boolean:
                {
                    return(( bool )value);
                }

            case TypeCode.Byte:
            {
                return(( byte )value);
            }

            case TypeCode.DateTime:
            {
                return(MessagePackConvert.FromDateTime(( DateTime )value));
            }

            case TypeCode.DBNull:
            case TypeCode.Empty:
            {
                return(MessagePackObject.Nil);
            }

            case TypeCode.Double:
            {
                return(( double )value);
            }

            case TypeCode.Int16:
            {
                return(( short )value);
            }

            case TypeCode.Int32:
            {
                return(( int )value);
            }

            case TypeCode.Int64:
            {
                return(( long )value);
            }

            case TypeCode.SByte:
            {
                return(( sbyte )value);
            }

            case TypeCode.Single:
            {
                return(( float )value);
            }

            case TypeCode.String:
            {
                return(value.ToString());
            }

            case TypeCode.UInt16:
            {
                return(( ushort )value);
            }

            case TypeCode.UInt32:
            {
                return(( uint )value);
            }

            case TypeCode.UInt64:
            {
                return(( ulong )value);
            }

            case TypeCode.Char:
            case TypeCode.Decimal:
            case TypeCode.Object:
            default:
            {
                return(null);
            }
            }
        }