public static DateTimeOffset GetDateTimeOffset(DateTimeOffsetAdapter value)
        {
            DateTimeOffset offset2;

            try
            {
                if (value.UtcDateTime.Kind == DateTimeKind.Unspecified)
                {
                    return(new DateTimeOffset(value.UtcDateTime, new TimeSpan(0, value.OffsetMinutes, 0)));
                }
                offset2 = new DateTimeOffset(value.UtcDateTime).ToOffset(new TimeSpan(0, value.OffsetMinutes, 0));
            }
            catch (ArgumentException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "DateTimeOffset", exception));
            }
            return(offset2);
        }
Example #2
0
 public static int ToInt32(string value)
 {
     try
     {
         return(XmlConvert.ToInt32(value));
     }
     catch (ArgumentException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Int32", exception));
     }
     catch (FormatException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Int32", exception));
     }
     catch (OverflowException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Int32", exception));
     }
 }
Example #3
0
 public static ulong ToUInt64(string value)
 {
     try
     {
         return(ulong.Parse(value, NumberFormatInfo.InvariantInfo));
     }
     catch (ArgumentException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "UInt64", exception));
     }
     catch (FormatException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "UInt64", exception));
     }
     catch (OverflowException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "UInt64", exception));
     }
 }
Example #4
0
 public static Guid ToGuid(string value)
 {
     try
     {
         return(Guid.Parse(Trim(value)));
     }
     catch (FormatException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Guid", exception));
     }
     catch (ArgumentException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Guid", exception));
     }
     catch (OverflowException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Guid", exception));
     }
 }
Example #5
0
        internal Uri ReadElementContentAsUri()
        {
            if (isEndOfEmptyElement)
                ThrowNotAtElement();

            string str = ReadElementContentAsString();
            try
            {
                return new Uri(str, UriKind.RelativeOrAbsolute);
            }
            catch (ArgumentException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "Uri", exception));
            }
            catch (FormatException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "Uri", exception));
            }
        }
Example #6
0
        internal Uri ReadContentAsUri()
        {
            Uri    uri;
            string uriString = this.ReadContentAsString();

            try
            {
                uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
            }
            catch (ArgumentException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(uriString, "Uri", exception));
            }
            catch (FormatException exception2)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(uriString, "Uri", exception2));
            }
            return(uri);
        }
Example #7
0
 internal Guid ReadContentAsGuid()
 {
     string str = reader.ReadContentAsString();
     try
     {
         return Guid.Parse(str);
     }
     catch (ArgumentException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "Guid", exception));
     }
     catch (FormatException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "Guid", exception));
     }
     catch (OverflowException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "Guid", exception));
     }
 }
Example #8
0
        internal byte[]? ReadContentAsBase64(string? str)
        {
            if (str == null)
                return null;
            str = str.Trim();
            if (str.Length == 0)
                return Array.Empty<byte>();

            try
            {
                return Convert.FromBase64String(str);
            }
            catch (ArgumentException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "byte[]", exception));
            }
            catch (FormatException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "byte[]", exception));
            }
        }
Example #9
0
        public static DateTimeOffset GetDateTimeOffset(DateTimeOffsetAdapter value)
        {
            try
            {
                switch (value.UtcDateTime.Kind)
                {
                case DateTimeKind.Unspecified:
                    return(new DateTimeOffset(value.UtcDateTime, new TimeSpan(0, value.OffsetMinutes, 0)));

                //DateTimeKind.Utc and DateTimeKind.Local
                //Read in deserialized DateTime portion of the DateTimeOffsetAdapter and convert DateTimeKind to Unspecified.
                //Apply ofset information read from OffsetMinutes portion of the DateTimeOffsetAdapter.
                //Return converted DateTimeoffset object.
                default:
                    DateTimeOffset deserialized = new DateTimeOffset(value.UtcDateTime);
                    return(deserialized.ToOffset(new TimeSpan(0, value.OffsetMinutes, 0)));
                }
            }
            catch (ArgumentException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "DateTimeOffset", exception));
            }
        }
Example #10
0
        internal Guid ReadElementContentAsGuid()
        {
            if (isEndOfEmptyElement)
                ThrowNotAtElement();

            string str = reader.ReadElementContentAsString();
            try
            {
                return new Guid(str);
            }
            catch (ArgumentException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "Guid", exception));
            }
            catch (FormatException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "Guid", exception));
            }
            catch (OverflowException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(str, "Guid", exception));
            }
        }
Example #11
0
        internal Guid ReadContentAsGuid()
        {
            Guid   guid;
            string input = this.reader.ReadContentAsString();

            try
            {
                guid = Guid.Parse(input);
            }
            catch (ArgumentException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(input, "Guid", exception));
            }
            catch (FormatException exception2)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(input, "Guid", exception2));
            }
            catch (OverflowException exception3)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(input, "Guid", exception3));
            }
            return(guid);
        }
Example #12
0
        /// <summary>
        /// Split a QualifiedName into prefix and localname, w/o any checking.
        /// (Used for XmlReader/XPathNavigator MoveTo(name) methods)
        /// </summary>
        internal static void SplitQName(string name, out string prefix, out string lname)
        {
            int colonPos = name.IndexOf(':');

            if (-1 == colonPos)
            {
                prefix = string.Empty;
                lname  = name;
            }
            else if (0 == colonPos || (name.Length - 1) == colonPos)
            {
#if !SILVERLIGHT_XPATH
                throw new ArgumentException(Res.GetString(Res.Xml_BadNameChar, XmlException.BuildCharExceptionArgs(':', '\0')), "name");
#else
                throw new ArgumentException(Res.GetString(Res.Xml_BadNameChar, XmlExceptionHelper.BuildCharExceptionArgs(':', '\0')), "name");
#endif
            }
            else
            {
                prefix = name.Substring(0, colonPos);
                colonPos++; // move after colon
                lname = name.Substring(colonPos, name.Length - colonPos);
            }
        }
Example #13
0
        public Uri ReadElementContentAsUri()
        {
            Uri uri;

            if (this.isEndOfEmptyElement)
            {
                this.ThrowNotAtElement();
            }
            string uriString = this.ReadElementContentAsString();

            try
            {
                uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
            }
            catch (ArgumentException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(uriString, "Uri", exception));
            }
            catch (FormatException exception2)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(uriString, "Uri", exception2));
            }
            return(uri);
        }
Example #14
0
        internal static DateTime ParseJsonDateInDefaultFormat(string originalDateTimeValue)
        {
            // Dates are represented in JSON as "\/Date(number of ticks)\/".
            // The number of ticks is the number of milliseconds since January 1, 1970.

            string dateTimeValue;

            if (!string.IsNullOrEmpty(originalDateTimeValue))
            {
                dateTimeValue = originalDateTimeValue.Trim();
            }
            else
            {
                dateTimeValue = originalDateTimeValue;
            }

            if (string.IsNullOrEmpty(dateTimeValue) ||
                !dateTimeValue.StartsWith(JsonGlobals.DateTimeStartGuardReader, StringComparison.Ordinal) ||
                !dateTimeValue.EndsWith(JsonGlobals.DateTimeEndGuardReader, StringComparison.Ordinal))
            {
                throw new FormatException(SR.Format(SR.JsonInvalidDateTimeString, originalDateTimeValue, JsonGlobals.DateTimeStartGuardWriter, JsonGlobals.DateTimeEndGuardWriter));
            }

            string       ticksvalue = dateTimeValue.Substring(6, dateTimeValue.Length - 8);
            long         millisecondsSinceUnixEpoch;
            DateTimeKind dateTimeKind          = DateTimeKind.Utc;
            int          indexOfTimeZoneOffset = ticksvalue.IndexOf('+', 1);

            if (indexOfTimeZoneOffset == -1)
            {
                indexOfTimeZoneOffset = ticksvalue.IndexOf('-', 1);
            }

            if (indexOfTimeZoneOffset != -1)
            {
                dateTimeKind = DateTimeKind.Local;
                ticksvalue   = ticksvalue.Substring(0, indexOfTimeZoneOffset);
            }

            try
            {
                millisecondsSinceUnixEpoch = Int64.Parse(ticksvalue, CultureInfo.InvariantCulture);
            }
            catch (ArgumentException exception)
            {
                throw XmlExceptionHelper.CreateConversionException(ticksvalue, "Int64", exception);
            }
            catch (FormatException exception)
            {
                throw XmlExceptionHelper.CreateConversionException(ticksvalue, "Int64", exception);
            }
            catch (OverflowException exception)
            {
                throw XmlExceptionHelper.CreateConversionException(ticksvalue, "Int64", exception);
            }

            // Convert from # millseconds since epoch to # of 100-nanosecond units, which is what DateTime understands
            long ticks = millisecondsSinceUnixEpoch * 10000 + JsonGlobals.unixEpochTicks;

            try
            {
                DateTime dateTime = new DateTime(ticks, DateTimeKind.Utc);
                switch (dateTimeKind)
                {
                case DateTimeKind.Local:
                    return(dateTime.ToLocalTime());

                case DateTimeKind.Unspecified:
                    return(DateTime.SpecifyKind(dateTime.ToLocalTime(), DateTimeKind.Unspecified));

                case DateTimeKind.Utc:
                default:
                    return(dateTime);
                }
            }
            catch (ArgumentException exception)
            {
                throw XmlExceptionHelper.CreateConversionException(ticksvalue, "DateTime", exception);
            }
        }
        private object DeserializeStringIntoDateTime()
        {
            string       dateTimeValue = DeserializeString();
            string       ticksvalue    = dateTimeValue.Substring(6, dateTimeValue.Length - 8);
            long         millisecondsSinceUnixEpoch;
            DateTimeKind dateTimeKind          = DateTimeKind.Utc;
            int          indexOfTimeZoneOffset = ticksvalue.IndexOf('+', 1);

            if (indexOfTimeZoneOffset == -1)
            {
                indexOfTimeZoneOffset = ticksvalue.IndexOf('-', 1);
            }

            if (indexOfTimeZoneOffset != -1)
            {
                dateTimeKind = DateTimeKind.Local;
                ticksvalue   = ticksvalue.Substring(0, indexOfTimeZoneOffset);
            }

            try
            {
                millisecondsSinceUnixEpoch = Int64.Parse(ticksvalue, NumberStyles.Integer, CultureInfo.InvariantCulture);
            }
            catch (ArgumentException exception)
            {
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(ticksvalue, "Int64", exception));
            }
            catch (FormatException exception)
            {
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(ticksvalue, "Int64", exception));
            }
            catch (OverflowException exception)
            {
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(ticksvalue, "Int64", exception));
            }

            // Convert from # millseconds since epoch to # of 100-nanosecond units, which is what DateTime understands
            long ticks = millisecondsSinceUnixEpoch * 10000 + JsonGlobals.unixEpochTicks;

            try
            {
                DateTime dateTime = new DateTime(ticks, DateTimeKind.Utc);
                switch (dateTimeKind)
                {
                case DateTimeKind.Local:
                    dateTime = dateTime.ToLocalTime();
                    break;

                case DateTimeKind.Unspecified:
                    dateTime = DateTime.SpecifyKind(dateTime.ToLocalTime(), DateTimeKind.Unspecified);
                    break;

                case DateTimeKind.Utc:
                default:
                    break;
                }

                // This string could be serialized from DateTime or String, keeping both until DataContract information is available
                return(Tuple.Create <DateTime, string>(dateTime, dateTimeValue));
            }
            catch (ArgumentException exception)
            {
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(ticksvalue, "DateTime", exception));
            }
        }
        internal static DateTime ParseJsonDate(string originalDateTimeValue)
        {
            string   str;
            long     num;
            DateTime time2;

            if (!string.IsNullOrEmpty(originalDateTimeValue))
            {
                str = originalDateTimeValue.Trim();
            }
            else
            {
                str = originalDateTimeValue;
            }
            if ((string.IsNullOrEmpty(str) || !str.StartsWith("/Date(", StringComparison.Ordinal)) || !str.EndsWith(")/", StringComparison.Ordinal))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(System.Runtime.Serialization.SR.GetString("JsonInvalidDateTimeString", new object[] { originalDateTimeValue, @"\/Date(", @")\/" })));
            }
            string       s     = str.Substring(6, str.Length - 8);
            DateTimeKind utc   = DateTimeKind.Utc;
            int          index = s.IndexOf('+', 1);

            if (index == -1)
            {
                index = s.IndexOf('-', 1);
            }
            if (index != -1)
            {
                utc = DateTimeKind.Local;
                s   = s.Substring(0, index);
            }
            try
            {
                num = long.Parse(s, CultureInfo.InvariantCulture);
            }
            catch (ArgumentException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(s, "Int64", exception));
            }
            catch (FormatException exception2)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(s, "Int64", exception2));
            }
            catch (OverflowException exception3)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(s, "Int64", exception3));
            }
            long ticks = (num * 0x2710L) + JsonGlobals.unixEpochTicks;

            try
            {
                DateTime time = new DateTime(ticks, DateTimeKind.Utc);
                switch (utc)
                {
                case DateTimeKind.Unspecified:
                    return(DateTime.SpecifyKind(time.ToLocalTime(), DateTimeKind.Unspecified));

                case DateTimeKind.Local:
                    return(time.ToLocalTime());
                }
                time2 = time;
            }
            catch (ArgumentException exception4)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(s, "DateTime", exception4));
            }
            return(time2);
        }