Ejemplo n.º 1
0
        internal static bool TryParse(string text, XsdDateTimeFlags kinds, out XsdDateTime result)
        {
            Parser parser = new Parser();

            if (!parser.Parse(text, kinds))
            {
                result = new XsdDateTime();
                return(false);
            }
            result = new XsdDateTime(parser);
            return(true);
        }
        public static string ToLocalXsdDateTimeString(DateTime dateTime)
        {
#if    PLATFORM_USE_XML_DLL
#if NETFX_CORE
            return(XmlConvert.ToString(dateTime, XsdDateTimeFormat));
#else
            return(XmlConvert.ToString(dateTime, XmlDateTimeSerializationMode.Local));
#endif
#else
            XsdDateTime xsdDT = new XsdDateTime(dateTime, XsdDateTimeFlags.AllXsd);

            return(xsdDT.ToString( ));
#endif
        }
        public static DateTime ParseXsdDateTime(string dateTimeStr)
        {
            dateTimeStr = RepairXsdTimeSeparator(dateTimeStr);

#if    PLATFORM_USE_XML_DLL
#if NETFX_CORE
            return(XmlConvert.ToDateTimeOffset(dateTimeStr).DateTime);
#else
            return(XmlConvert.ToDateTime(dateTimeStr, XmlDateTimeSerializationMode.Utc));
#endif
#else
            XsdDateTime xsdDT = new XsdDateTime(dateTimeStr, XsdDateTimeFlags.AllXsd);


            return(( DateTime )xsdDT);
#endif
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Compares two DateTime values, returning an integer that indicates
        /// their relationship.
        /// </summary>
        public static int Compare(XsdDateTime left, XsdDateTime right)
        {
            if (left.extra == right.extra)
            {
                return(DateTime.Compare(left.dt, right.dt));
            }
            else
            {
                // Xsd types should be the same for it to be comparable
                if (left.InternalTypeCode != right.InternalTypeCode)
                {
                    //throw new ArgumentException(Res.GetString(Res.Sch_XsdDateTimeCompare, left.TypeCode, right.TypeCode));

                    throw new ArgumentException("Res.GetString(Res.Sch_XsdDateTimeCompare, left.TypeCode, right.TypeCode");
                }
                // Convert both to UTC
                return(DateTime.Compare(left.GetZuluDateTime(), right.GetZuluDateTime()));
            }
        }
        public static DateTime ParseShortestXsdDateTime(string dateTimeStr)
        {
            if (string.IsNullOrEmpty(dateTimeStr))
            {
                return(DateTime.MinValue);
            }

            if (dateTimeStr.StartsWith(EscapedWcfJsonPrefix, StringComparison.Ordinal) || dateTimeStr.StartsWith(WcfJsonPrefix, StringComparison.Ordinal))
            {
                return(ParseWcfJsonDate(dateTimeStr).Prepare());
            }

            if (dateTimeStr.Length == DefaultDateTimeFormat.Length ||
                dateTimeStr.Length == DefaultDateTimeFormatWithFraction.Length)
            {
                return(DateTime.Parse(dateTimeStr, CultureInfo.InvariantCulture).Prepare());
            }

            dateTimeStr = RepairXsdTimeSeparator(dateTimeStr);

            if (dateTimeStr.Length == XsdDateTimeFormatSeconds.Length)
            {
                return(DateTime.ParseExact(dateTimeStr, XsdDateTimeFormatSeconds, null, DateTimeStyles.AdjustToUniversal).Prepare(parsedAsUtc: true));
            }



            if (dateTimeStr.Length >= XsdDateTimeFormat3F.Length &&
                dateTimeStr.Length <= XsdDateTimeFormat.Length &&
                dateTimeStr.EndsWith(XsdUtcSuffix))
            {
#if NETFX_CORE
                var dateTimeType = JsConfig.DateHandler != JsonDateHandler.ISO8601
                    ? "yyyy-MM-ddTHH:mm:sszzzzzzz"
                        : "yyyy-MM-ddTHH:mm:sszzzzzzz";

                return(XmlConvert.ToDateTimeOffset(dateTimeStr, dateTimeType).DateTime.Prepare());
#else
                var dateTime = Env.IsMono ? ParseManual(dateTimeStr) : null;
                if (dateTime != null)
                {
                    return(dateTime.Value);
                }


#if    PLATFORM_USE_XML_DLL
                return(XmlConvert.ToDateTime(dateTimeStr, XmlDateTimeSerializationMode.Utc).Prepare(parsedAsUtc: true));
#else
                XsdDateTime xsdDT = new XsdDateTime(dateTimeStr, XsdDateTimeFlags.AllXsd);

                DateTime dt = ( DateTime )xsdDT;

                dt.ToStableUniversalTime( );
#endif
#endif
            }



            try
            {
                return(DateTime.Parse(dateTimeStr, null, DateTimeStyles.AssumeLocal).Prepare());
            }
            catch (FormatException)
            {
                var manualDate = ParseManual(dateTimeStr);
                if (manualDate != null)
                {
                    return(manualDate.Value);
                }

                throw;
            }
        }