Exemplo n.º 1
0
        internal static void WriteValue(TextWriter writer, DateTime value, ODataJsonDateTimeFormat dateTimeFormat)
        {
            switch (dateTimeFormat)
            {
            case ODataJsonDateTimeFormat.ODataDateTime:
            {
                value = GetUniversalDate(value);
                string text = string.Format(CultureInfo.InvariantCulture, @"\/Date({0})\/", new object[] { DateTimeTicksToJsonTicks(value.Ticks) });
                WriteQuoted(writer, text);
                return;
            }

            case ODataJsonDateTimeFormat.ISO8601DateTime:
            {
                string str = PlatformHelper.ConvertDateTimeToString(value);
                WriteQuoted(writer, str);
                return;
            }
            }
        }
Exemplo n.º 2
0
        internal static void WriteValue(TextWriter writer, DateTime value, ODataJsonDateTimeFormat dateTimeFormat)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(writer != null, "writer != null");

            switch (dateTimeFormat)
            {
            case ODataJsonDateTimeFormat.ISO8601DateTime:
            {
                // jsonDateTime= quotation-mark
                //  YYYY-MM-DDThh:mm:ss.sTZD
                //  [("+" / "-") offset]
                //  quotation-mark
                string textValue = PlatformHelper.ConvertDateTimeToString(value);
                WriteQuoted(writer, textValue);
            }

            break;

            case ODataJsonDateTimeFormat.ODataDateTime:
            {
                // taken from the Atlas serializer
                // DevDiv 41127: Never confuse atlas serialized strings with dates
                // Serialized date: "\/Date(123)\/"
                // sb.Append(@"""\/Date(");
                // sb.Append((datetime.ToUniversalTime().Ticks - DatetimeMinTimeTicks) / 10000);
                // sb.Append(@")\/""");
                value = GetUniversalDate(value);
                System.Diagnostics.Debug.Assert(value.Kind == DateTimeKind.Utc, "dateTime.Kind == DateTimeKind.Utc");

                string textValue = String.Format(
                    CultureInfo.InvariantCulture,
                    JsonConstants.ODataDateTimeFormat,
                    DateTimeTicksToJsonTicks(value.Ticks));
                WriteQuoted(writer, textValue);
            }

            break;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts the given date/time value to the string appropriate for Atom format
        /// </summary>
        /// <param name="dt">The date/time value to convert.</param>
        /// <returns>The string version of the date/time value in Atom format.</returns>
        internal static string ToString(this DateTime dt)
        {
            DebugUtils.CheckNoExternalCallers();

            return(PlatformHelper.ConvertDateTimeToString(dt));
        }
Exemplo n.º 4
0
 internal static string DateTimeAsXml(DateTime d)
 {
     return(PlatformHelper.ConvertDateTimeToString(d));
 }
Exemplo n.º 5
0
 internal static string ToString(this DateTime dt)
 {
     return(PlatformHelper.ConvertDateTimeToString(dt));
 }
Exemplo n.º 6
0
        /// <summary>
        /// Converts a object to a string value and indicates its type and whether it successfully converted it or not
        /// </summary>
        /// <param name="value">Value to Convert</param>
        /// <param name="newStringValue">New Converted Value</param>
        /// <param name="valueType">Type of the original Value</param>
        /// <returns>True if it was converted or false if it was not</returns>
        internal static bool TryConvertPrimitiveValueToString(object value, out string newStringValue, out Type valueType)
        {
            valueType      = null;
            newStringValue = null;

            if (value == null)
            {
                return(true);
            }

            valueType = value.GetType();

            if (valueType == typeof(bool))
            {
                var valueToConvert = (bool)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(byte))
            {
                var valueToConvert = (byte)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(char))
            {
                var valueToConvert = (char)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(DateTime))
            {
                var valueToConvert = (DateTime)value;
                newStringValue = PlatformHelper.ConvertDateTimeToString(valueToConvert);
            }
            else if (valueType == typeof(DateTimeOffset))
            {
                var valueToConvert = (DateTimeOffset)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(decimal))
            {
                var valueToConvert = (decimal)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(double))
            {
                var valueToConvert = (double)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(float))
            {
                var valueToConvert = (float)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(Guid))
            {
                var valueToConvert = (Guid)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(int))
            {
                var valueToConvert = (int)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(long))
            {
                var valueToConvert = (long)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(sbyte))
            {
                var valueToConvert = (sbyte)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(short))
            {
                var valueToConvert = (short)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(string))
            {
                newStringValue = (string)value;
            }
            else if (valueType == typeof(TimeSpan))
            {
                var valueToConvert = (TimeSpan)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(uint))
            {
                var valueToConvert = (uint)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(ulong))
            {
                var valueToConvert = (ulong)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType == typeof(ushort))
            {
                var valueToConvert = (ushort)value;
                newStringValue = XmlConvert.ToString(valueToConvert);
            }
            else if (valueType.GetBaseType() == typeof(Enum))
            {
                newStringValue = Enum.GetName(valueType, value);
                if (newStringValue == null)
                {
                    Type underlyingValueType;
                    TryConvertPrimitiveValueToString(DataUtilities.ConvertFromEnum(value), out newStringValue, out underlyingValueType);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Formats the literal without a type prefix, quotes, or escaping.
        /// </summary>
        /// <param name="value">The non-null value to format.</param>
        /// <returns>The formatted literal, without type marker or quotes.</returns>
        private static string FormatRawLiteral(object value)
        {
            Debug.Assert(value != null, "value != null");

            string stringValue = value as string;

            if (stringValue != null)
            {
                return(stringValue);
            }

            if (value is bool)
            {
                return(XmlConvert.ToString((bool)value));
            }

            if (value is byte)
            {
                return(XmlConvert.ToString((byte)value));
            }

            if (value is DateTime)
            {
#if PORTABLELIB
                return(PlatformHelper.ConvertDateTimeToString((DateTime)value));
#else
                return(XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind));
#endif
            }

            if (value is decimal)
            {
                return(XmlConvert.ToString((decimal)value));
            }

            if (value is double)
            {
                string formattedDouble = XmlConvert.ToString((double)value);
                formattedDouble = SharedUtils.AppendDecimalMarkerToDouble(formattedDouble);
                return(formattedDouble);
            }

            if (value is Guid)
            {
                return(value.ToString());
            }

            if (value is short)
            {
                return(XmlConvert.ToString((Int16)value));
            }

            if (value is int)
            {
                return(XmlConvert.ToString((Int32)value));
            }

            if (value is long)
            {
                return(XmlConvert.ToString((Int64)value));
            }

            if (value is sbyte)
            {
                return(XmlConvert.ToString((SByte)value));
            }

            if (value is float)
            {
                return(XmlConvert.ToString((Single)value));
            }

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

            if (value is DateTimeOffset)
            {
                return(XmlConvert.ToString((DateTimeOffset)value));
            }

            if (value is TimeSpan)
            {
                return(XmlConvert.ToString((TimeSpan)value));
            }

            Geography geography = value as Geography;
            if (geography != null)
            {
                return(WellKnownTextSqlFormatter.Create(true).Write(geography));
            }

            Geometry geometry = value as Geometry;
            if (geometry != null)
            {
                return(WellKnownTextSqlFormatter.Create(true).Write(geometry));
            }

            throw SharedUtils.CreateExceptionForUnconvertableType(value);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Serialize the DateTime value
 /// </summary>
 /// <param name="value">dateTime value to be serialized</param>
 /// <returns>The serialized value</returns>
 public override string Serialize(DateTime value)
 {
     return("\"" + PlatformHelper.ConvertDateTimeToString(value) + "\"");
 }
Exemplo n.º 9
0
 /// <summary>
 /// Serialize a datetime value
 /// </summary>
 /// <param name="value">The value as a clr instance</param>
 /// <returns>The wire representation of the value</returns>
 public override string Serialize(DateTime value)
 {
     return(string.Concat("datetime'", PlatformHelper.ConvertDateTimeToString(value), "'"));
 }