Exemple #1
0
        public XsdDateTime(DateTimeOffset dateTimeOffset, XsdDateTimeFlags kinds)
        {
            Debug.Assert(Bits.ExactlyOne((uint)kinds), "Only one DateTime type code can be set.");

            _dt = dateTimeOffset.DateTime;

            TimeSpan         zoneOffset = dateTimeOffset.Offset;
            DateTimeTypeCode code       = (DateTimeTypeCode)(Bits.LeastPosition((uint)kinds) - 1);
            XsdDateTimeKind  kind;

            if (zoneOffset.TotalMinutes < 0)
            {
                zoneOffset = zoneOffset.Negate();
                kind       = XsdDateTimeKind.LocalWestOfZulu;
            }
            else if (zoneOffset.TotalMinutes > 0)
            {
                kind = XsdDateTimeKind.LocalEastOfZulu;
            }
            else
            {
                kind = XsdDateTimeKind.Zulu;
            }

            _extra = (uint)(((int)code << TypeShift) | ((int)kind << KindShift) | (zoneOffset.Hours << ZoneHourShift) | zoneOffset.Minutes);
        }
 public XsdDateTime(string text, XsdDateTimeFlags kinds)
 {
     this = new XsdDateTime();
     Parser parser = new Parser();
     if (!parser.Parse(text, kinds))
     {
         throw new FormatException(Res.GetString("XmlConvert_BadFormat", new object[] { text, kinds }));
     }
     this.InitiateXsdDateTime(parser);
 }
Exemple #3
0
        /// <summary>
        /// Constructs an XsdDateTime from a string using specific format.
        /// </summary>
        public XsdDateTime(string text, XsdDateTimeFlags kinds) : this()
        {
            Parser parser = new Parser();

            if (!parser.Parse(text, kinds))
            {
                throw new FormatException(Res.GetString(Res.XmlConvert_BadFormat, text, kinds));
            }
            InitiateXsdDateTime(parser);
        }
Exemple #4
0
        /// <summary>
        /// Constructs an XsdDateTime from a string using specific format.
        /// </summary>
        public XsdDateTime(string text, XsdDateTimeFlags kinds) : this()
        {
            Parser parser = default;

            if (!parser.Parse(text, kinds))
            {
                throw new FormatException(SR.Format(SR.XmlConvert_BadFormat, text, kinds));
            }
            InitiateXsdDateTime(parser);
        }
Exemple #5
0
        public XsdDateTime(string text, XsdDateTimeFlags kinds) : this()
        {
            Parser parser = new Parser();

            if (!parser.Parse(text, kinds))
            {
                //TODO:
                throw new FormatException(string.Format("The string '{0}' is not a valid {1} value.", text, kinds));
            }
            InitiateXsdDateTime(parser);
        }
 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;
 }
Exemple #7
0
        public XsdDateTime(string text, XsdDateTimeFlags kinds)
        {
            this = new XsdDateTime();
            Parser parser = new Parser();

            if (!parser.Parse(text, kinds))
            {
                throw new FormatException(Res.GetString("XmlConvert_BadFormat", new object[] { text, kinds }));
            }
            this.InitiateXsdDateTime(parser);
        }
Exemple #8
0
        internal static bool TryParse(string text, XsdDateTimeFlags kinds, out XsdDateTime result)
        {
            Parser parser = default;

            if (!parser.Parse(text, kinds))
            {
                result = default;
                return(false);
            }
            result = new XsdDateTime(parser);
            return(true);
        }
        /// <summary>
        /// Constructs an XsdDateTime from a DateTime.
        /// </summary>
        public XsdDateTime(DateTime dateTime, XsdDateTimeFlags kinds)
        {
            Debug.Assert(Bits.ExactlyOne((uint)kinds), "Only one DateTime type code can be set.");
            dt = dateTime;

            DateTimeTypeCode code      = (DateTimeTypeCode)(Bits.LeastPosition((uint)kinds) - 1);
            int             zoneHour   = 0;
            int             zoneMinute = 0;
            XsdDateTimeKind kind;

            switch (dateTime.Kind)
            {
            case DateTimeKind.Unspecified: kind = XsdDateTimeKind.Unspecified; break;

            case DateTimeKind.Utc: kind = XsdDateTimeKind.Zulu; break;

            default: {
                Debug.Assert(dateTime.Kind == DateTimeKind.Local, "Unknown DateTimeKind: " + dateTime.Kind);


#if NET20 || PLATFORM_USE_AOT
                TimeSpan utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
#else
                TimeSpan utcOffset = TimeZoneInfo.Local.GetUtcOffset(dateTime);
#endif



                if (utcOffset.Ticks < 0)
                {
                    kind       = XsdDateTimeKind.LocalWestOfZulu;
                    zoneHour   = -utcOffset.Hours;
                    zoneMinute = -utcOffset.Minutes;
                }
                else
                {
                    kind       = XsdDateTimeKind.LocalEastOfZulu;
                    zoneHour   = utcOffset.Hours;
                    zoneMinute = utcOffset.Minutes;
                }
                break;
            }
            }

            extra = (uint)(((int)code << TypeShift) | ((int)kind << KindShift) | (zoneHour << ZoneHourShift) | zoneMinute);
        }
Exemple #10
0
        public XsdDateTime(DateTime dateTime, XsdDateTimeFlags kinds)
        {
            dt = dateTime;

            DateTimeTypeCode code      = (DateTimeTypeCode)(Bits.LeastPosition((uint)kinds) - 1);
            int             zoneHour   = 0;
            int             zoneMinute = 0;
            XsdDateTimeKind kind;

            switch (dateTime.Kind)
            {
            case DateTimeKind.Unspecified:
                kind = XsdDateTimeKind.Unspecified;
                break;

            case DateTimeKind.Utc:
                kind = XsdDateTimeKind.Zulu;
                break;

            default:
            {
                TimeSpan utcOffset = TimeZoneInfo.Local.GetUtcOffset(dateTime);

                if (utcOffset.Ticks < 0)
                {
                    kind       = XsdDateTimeKind.LocalWestOfZulu;
                    zoneHour   = -utcOffset.Hours;
                    zoneMinute = -utcOffset.Minutes;
                }
                else
                {
                    kind       = XsdDateTimeKind.LocalEastOfZulu;
                    zoneHour   = utcOffset.Hours;
                    zoneMinute = utcOffset.Minutes;
                }
                break;
            }
            }

            extra = (uint)(((int)code << TypeShift) | ((int)kind << KindShift) | (zoneHour << ZoneHourShift) |
                           zoneMinute);
        }
Exemple #11
0
        public XsdDateTime(DateTime dateTime, XsdDateTimeFlags kinds)
        {
            XsdDateTimeKind unspecified;

            this.dt = dateTime;
            DateTimeTypeCode code = (DateTimeTypeCode)(Bits.LeastPosition((uint)kinds) - 1);
            int hours             = 0;
            int minutes           = 0;

            switch (dateTime.Kind)
            {
            case DateTimeKind.Unspecified:
                unspecified = XsdDateTimeKind.Unspecified;
                break;

            case DateTimeKind.Utc:
                unspecified = XsdDateTimeKind.Zulu;
                break;

            default:
            {
                TimeSpan utcOffset = TimeZoneInfo.Local.GetUtcOffset(dateTime);
                if (utcOffset.Ticks < 0L)
                {
                    unspecified = XsdDateTimeKind.LocalWestOfZulu;
                    hours       = -utcOffset.Hours;
                    minutes     = -utcOffset.Minutes;
                }
                else
                {
                    unspecified = XsdDateTimeKind.LocalEastOfZulu;
                    hours       = utcOffset.Hours;
                    minutes     = utcOffset.Minutes;
                }
                break;
            }
            }
            this.extra = (uint)((((((int)code) << 0x18) | (((int)unspecified) << 0x10)) | (hours << 8)) | minutes);
        }
Exemple #12
0
        public XsdDateTime(DateTimeOffset dateTimeOffset, XsdDateTimeFlags kinds)
        {
            XsdDateTimeKind localWestOfZulu;

            this.dt = dateTimeOffset.DateTime;
            TimeSpan         offset = dateTimeOffset.Offset;
            DateTimeTypeCode code   = (DateTimeTypeCode)(Bits.LeastPosition((uint)kinds) - 1);

            if (offset.TotalMinutes < 0.0)
            {
                offset          = offset.Negate();
                localWestOfZulu = XsdDateTimeKind.LocalWestOfZulu;
            }
            else if (offset.TotalMinutes > 0.0)
            {
                localWestOfZulu = XsdDateTimeKind.LocalEastOfZulu;
            }
            else
            {
                localWestOfZulu = XsdDateTimeKind.Zulu;
            }
            this.extra = (uint)((((((int)code) << 0x18) | (((int)localWestOfZulu) << 0x10)) | (offset.Hours << 8)) | offset.Minutes);
        }
 public XsdDateTime(DateTimeOffset dateTimeOffset, XsdDateTimeFlags kinds)
 {
     XsdDateTimeKind localWestOfZulu;
     this.dt = dateTimeOffset.DateTime;
     TimeSpan offset = dateTimeOffset.Offset;
     DateTimeTypeCode code = (DateTimeTypeCode) (Bits.LeastPosition((uint) kinds) - 1);
     if (offset.TotalMinutes < 0.0)
     {
         offset = offset.Negate();
         localWestOfZulu = XsdDateTimeKind.LocalWestOfZulu;
     }
     else if (offset.TotalMinutes > 0.0)
     {
         localWestOfZulu = XsdDateTimeKind.LocalEastOfZulu;
     }
     else
     {
         localWestOfZulu = XsdDateTimeKind.Zulu;
     }
     this.extra = (uint) ((((((int) code) << 0x18) | (((int) localWestOfZulu) << 0x10)) | (offset.Hours << 8)) | offset.Minutes);
 }
 public bool Parse(string text, XsdDateTimeFlags kinds)
 {
     this.text = text;
     this.length = text.Length;
     int start = 0;
     while ((start < this.length) && char.IsWhiteSpace(text[start]))
     {
         start++;
     }
     if (Test(kinds, XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrDateTimeNoTz | XsdDateTimeFlags.Date | XsdDateTimeFlags.DateTime) && this.ParseDate(start))
     {
         if ((Test(kinds, XsdDateTimeFlags.DateTime) && this.ParseChar(start + XsdDateTime.Lzyyyy_MM_dd, 'T')) && this.ParseTimeAndZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM_ddT))
         {
             this.typeCode = XsdDateTime.DateTimeTypeCode.DateTime;
             return true;
         }
         if (Test(kinds, XsdDateTimeFlags.Date) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM_dd))
         {
             this.typeCode = XsdDateTime.DateTimeTypeCode.Date;
             return true;
         }
         if (Test(kinds, XsdDateTimeFlags.XdrDateTime) && (this.ParseZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM_dd) || (this.ParseChar(start + XsdDateTime.Lzyyyy_MM_dd, 'T') && this.ParseTimeAndZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM_ddT))))
         {
             this.typeCode = XsdDateTime.DateTimeTypeCode.XdrDateTime;
             return true;
         }
         if (Test(kinds, XsdDateTimeFlags.XdrDateTimeNoTz))
         {
             if (!this.ParseChar(start + XsdDateTime.Lzyyyy_MM_dd, 'T'))
             {
                 this.typeCode = XsdDateTime.DateTimeTypeCode.XdrDateTime;
                 return true;
             }
             if (this.ParseTimeAndWhitespace(start + XsdDateTime.Lzyyyy_MM_ddT))
             {
                 this.typeCode = XsdDateTime.DateTimeTypeCode.XdrDateTime;
                 return true;
             }
         }
     }
     if (Test(kinds, XsdDateTimeFlags.Time) && this.ParseTimeAndZoneAndWhitespace(start))
     {
         this.year = 0x770;
         this.month = 1;
         this.day = 1;
         this.typeCode = XsdDateTime.DateTimeTypeCode.Time;
         return true;
     }
     if (Test(kinds, XsdDateTimeFlags.XdrTimeNoTz) && this.ParseTimeAndWhitespace(start))
     {
         this.year = 0x770;
         this.month = 1;
         this.day = 1;
         this.typeCode = XsdDateTime.DateTimeTypeCode.Time;
         return true;
     }
     if ((Test(kinds, XsdDateTimeFlags.GYear | XsdDateTimeFlags.GYearMonth) && this.Parse4Dig(start, ref this.year)) && (1 <= this.year))
     {
         if (((Test(kinds, XsdDateTimeFlags.GYearMonth) && this.ParseChar(start + XsdDateTime.Lzyyyy, '-')) && (this.Parse2Dig(start + XsdDateTime.Lzyyyy_, ref this.month) && (1 <= this.month))) && ((this.month <= 12) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM)))
         {
             this.day = 1;
             this.typeCode = XsdDateTime.DateTimeTypeCode.GYearMonth;
             return true;
         }
         if (Test(kinds, XsdDateTimeFlags.GYear) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lzyyyy))
         {
             this.month = 1;
             this.day = 1;
             this.typeCode = XsdDateTime.DateTimeTypeCode.GYear;
             return true;
         }
     }
     if (((Test(kinds, XsdDateTimeFlags.GMonth | XsdDateTimeFlags.GMonthDay) && this.ParseChar(start, '-')) && (this.ParseChar(start + XsdDateTime.Lz_, '-') && this.Parse2Dig(start + XsdDateTime.Lz__, ref this.month))) && ((1 <= this.month) && (this.month <= 12)))
     {
         if (((Test(kinds, XsdDateTimeFlags.GMonthDay) && this.ParseChar(start + XsdDateTime.Lz__mm, '-')) && (this.Parse2Dig(start + XsdDateTime.Lz__mm_, ref this.day) && (1 <= this.day))) && ((this.day <= DateTime.DaysInMonth(0x770, this.month)) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lz__mm_dd)))
         {
             this.year = 0x770;
             this.typeCode = XsdDateTime.DateTimeTypeCode.GMonthDay;
             return true;
         }
         if (Test(kinds, XsdDateTimeFlags.GMonth) && (this.ParseZoneAndWhitespace(start + XsdDateTime.Lz__mm) || ((this.ParseChar(start + XsdDateTime.Lz__mm, '-') && this.ParseChar(start + XsdDateTime.Lz__mm_, '-')) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lz__mm__))))
         {
             this.year = 0x770;
             this.day = 1;
             this.typeCode = XsdDateTime.DateTimeTypeCode.GMonth;
             return true;
         }
     }
     if (((Test(kinds, XsdDateTimeFlags.GDay) && this.ParseChar(start, '-')) && (this.ParseChar(start + XsdDateTime.Lz_, '-') && this.ParseChar(start + XsdDateTime.Lz__, '-'))) && ((this.Parse2Dig(start + XsdDateTime.Lz___, ref this.day) && (1 <= this.day)) && ((this.day <= DateTime.DaysInMonth(0x770, 1)) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lz___dd))))
     {
         this.year = 0x770;
         this.month = 1;
         this.typeCode = XsdDateTime.DateTimeTypeCode.GDay;
         return true;
     }
     return false;
 }
        public XsdDateTime(DateTime dateTime, XsdDateTimeFlags kinds)
        {
            XsdDateTimeKind unspecified;
            this.dt = dateTime;
            DateTimeTypeCode code = (DateTimeTypeCode) (Bits.LeastPosition((uint) kinds) - 1);
            int hours = 0;
            int minutes = 0;
            switch (dateTime.Kind)
            {
                case DateTimeKind.Unspecified:
                    unspecified = XsdDateTimeKind.Unspecified;
                    break;

                case DateTimeKind.Utc:
                    unspecified = XsdDateTimeKind.Zulu;
                    break;

                default:
                {
                    TimeSpan utcOffset = TimeZoneInfo.Local.GetUtcOffset(dateTime);
                    if (utcOffset.Ticks < 0L)
                    {
                        unspecified = XsdDateTimeKind.LocalWestOfZulu;
                        hours = -utcOffset.Hours;
                        minutes = -utcOffset.Minutes;
                    }
                    else
                    {
                        unspecified = XsdDateTimeKind.LocalEastOfZulu;
                        hours = utcOffset.Hours;
                        minutes = utcOffset.Minutes;
                    }
                    break;
                }
            }
            this.extra = (uint) ((((((int) code) << 0x18) | (((int) unspecified) << 0x10)) | (hours << 8)) | minutes);
        }
 internal Datatype_dateTimeBase(XsdDateTimeFlags dateTimeFlags) {
     this.dateTimeFlags = dateTimeFlags;
 }
Exemple #17
0
 private static bool Test(XsdDateTimeFlags left, XsdDateTimeFlags right)
 {
     return (left & right) != 0;
 }
Exemple #18
0
            public bool Parse(string text, XsdDateTimeFlags kinds)
            {
                this.text = text;
                this.length = text.Length;

                // Skip leading whitespace
                int start = 0;
                while (start < length && char.IsWhiteSpace(text[start]))
                {
                    start++;
                }
                // Choose format starting from the most common and trying not to reparse the same thing too many times

                if (Test(kinds, XsdDateTimeFlags.DateTime | XsdDateTimeFlags.Date | XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrDateTimeNoTz))
                {
                    if (ParseDate(start))
                    {
                        if (Test(kinds, XsdDateTimeFlags.DateTime))
                        {
                            if (ParseChar(start + Lzyyyy_MM_dd, 'T') && ParseTimeAndZoneAndWhitespace(start + Lzyyyy_MM_ddT))
                            {
                                typeCode = DateTimeTypeCode.DateTime;
                                return true;
                            }
                        }

                        if (Test(kinds, XsdDateTimeFlags.Date))
                        {
                            if (ParseZoneAndWhitespace(start + Lzyyyy_MM_dd))
                            {
                                typeCode = DateTimeTypeCode.Date;
                                return true;
                            }
                        }
                        if (Test(kinds, XsdDateTimeFlags.XdrDateTime))
                        {
                            if (ParseZoneAndWhitespace(start + Lzyyyy_MM_dd) || (ParseChar(start + Lzyyyy_MM_dd, 'T') && ParseTimeAndZoneAndWhitespace(start + Lzyyyy_MM_ddT)))
                            {
                                typeCode = DateTimeTypeCode.XdrDateTime;
                                return true;
                            }
                        }
                        if (Test(kinds, XsdDateTimeFlags.XdrDateTimeNoTz))
                        {
                            if (ParseChar(start + Lzyyyy_MM_dd, 'T'))
                            {
                                if (ParseTimeAndWhitespace(start + Lzyyyy_MM_ddT))
                                {
                                    typeCode = DateTimeTypeCode.XdrDateTime;
                                    return true;
                                }
                            }
                            else
                            {
                                typeCode = DateTimeTypeCode.XdrDateTime;
                                return true;
                            }
                        }
                    }
                }

                if (Test(kinds, XsdDateTimeFlags.Time))
                {
                    if (ParseTimeAndZoneAndWhitespace(start))
                    { //Equivalent to NoCurrentDateDefault on DateTimeStyles while parsing xs:time
                        year = leapYear;
                        month = firstMonth;
                        day = firstDay;
                        typeCode = DateTimeTypeCode.Time;
                        return true;
                    }
                }

                if (Test(kinds, XsdDateTimeFlags.XdrTimeNoTz))
                {
                    if (ParseTimeAndWhitespace(start))
                    { //Equivalent to NoCurrentDateDefault on DateTimeStyles while parsing xs:time
                        year = leapYear;
                        month = firstMonth;
                        day = firstDay;
                        typeCode = DateTimeTypeCode.Time;
                        return true;
                    }
                }

                if (Test(kinds, XsdDateTimeFlags.GYearMonth | XsdDateTimeFlags.GYear))
                {
                    if (Parse4Dig(start, ref year) && 1 <= year)
                    {
                        if (Test(kinds, XsdDateTimeFlags.GYearMonth))
                        {
                            if (
                                ParseChar(start + Lzyyyy, '-') &&
                                Parse2Dig(start + Lzyyyy_, ref month) && 1 <= month && month <= 12 &&
                                ParseZoneAndWhitespace(start + Lzyyyy_MM)
                            )
                            {
                                day = firstDay;
                                typeCode = DateTimeTypeCode.GYearMonth;
                                return true;
                            }
                        }
                        if (Test(kinds, XsdDateTimeFlags.GYear))
                        {
                            if (ParseZoneAndWhitespace(start + Lzyyyy))
                            {
                                month = firstMonth;
                                day = firstDay;
                                typeCode = DateTimeTypeCode.GYear;
                                return true;
                            }
                        }
                    }
                }
                if (Test(kinds, XsdDateTimeFlags.GMonthDay | XsdDateTimeFlags.GMonth))
                {
                    if (
                        ParseChar(start, '-') &&
                        ParseChar(start + Lz_, '-') &&
                        Parse2Dig(start + Lz__, ref month) && 1 <= month && month <= 12
                    )
                    {
                        if (Test(kinds, XsdDateTimeFlags.GMonthDay) && ParseChar(start + Lz__mm, '-'))
                        {
                            if (
                                Parse2Dig(start + Lz__mm_, ref day) && 1 <= day && day <= DateTime.DaysInMonth(leapYear, month) &&
                                ParseZoneAndWhitespace(start + Lz__mm_dd)
                            )
                            {
                                year = leapYear;
                                typeCode = DateTimeTypeCode.GMonthDay;
                                return true;
                            }
                        }
                        if (Test(kinds, XsdDateTimeFlags.GMonth))
                        {
                            if (ParseZoneAndWhitespace(start + Lz__mm) || (ParseChar(start + Lz__mm, '-') && ParseChar(start + Lz__mm_, '-') && ParseZoneAndWhitespace(start + Lz__mm__)))
                            {
                                year = leapYear;
                                day = firstDay;
                                typeCode = DateTimeTypeCode.GMonth;
                                return true;
                            }
                        }
                    }
                }

                if (Test(kinds, XsdDateTimeFlags.GDay))
                {
                    if (
                        ParseChar(start, '-') &&
                        ParseChar(start + Lz_, '-') &&
                        ParseChar(start + Lz__, '-') &&
                        Parse2Dig(start + Lz___, ref day) && 1 <= day && day <= DateTime.DaysInMonth(leapYear, firstMonth) &&
                        ParseZoneAndWhitespace(start + Lz___dd))
                    {
                        year = leapYear;
                        month = firstMonth;
                        typeCode = DateTimeTypeCode.GDay;
                        return true;
                    }
                }
                return false;
            }
Exemple #19
0
        public XsdDateTime(DateTimeOffset dateTimeOffset, XsdDateTimeFlags kinds)
        {
            Debug.Assert(Bits.ExactlyOne((uint)kinds), "Only one DateTime type code can be set.");

            dt = dateTimeOffset.DateTime;

            TimeSpan zoneOffset = dateTimeOffset.Offset;
            DateTimeTypeCode code = (DateTimeTypeCode)(Bits.LeastPosition((uint)kinds) - 1);
            XsdDateTimeKind kind;
            if (zoneOffset.TotalMinutes < 0)
            {
                zoneOffset = zoneOffset.Negate();
                kind = XsdDateTimeKind.LocalWestOfZulu;
            }
            else if (zoneOffset.TotalMinutes > 0)
            {
                kind = XsdDateTimeKind.LocalEastOfZulu;
            }
            else
            {
                kind = XsdDateTimeKind.Zulu;
            }

            extra = (uint)(((int)code << TypeShift) | ((int)kind << KindShift) | (zoneOffset.Hours << ZoneHourShift) | zoneOffset.Minutes);
        }
Exemple #20
0
        /// <summary>
        /// Constructs an XsdDateTime from a DateTime.
        /// </summary>
        public XsdDateTime(DateTime dateTime, XsdDateTimeFlags kinds)
        {
            Debug.Assert(Bits.ExactlyOne((uint)kinds), "Only one DateTime type code can be set.");
            dt = dateTime;

            DateTimeTypeCode code = (DateTimeTypeCode)(Bits.LeastPosition((uint)kinds) - 1);
            int zoneHour = 0;
            int zoneMinute = 0;
            XsdDateTimeKind kind;

            switch (dateTime.Kind)
            {
                case DateTimeKind.Unspecified: kind = XsdDateTimeKind.Unspecified; break;
                case DateTimeKind.Utc: kind = XsdDateTimeKind.Zulu; break;

                default:
                    {
                        Debug.Assert(dateTime.Kind == DateTimeKind.Local, "Unknown DateTimeKind: " + dateTime.Kind);
                        TimeSpan utcOffset = TimeZoneInfo.Local.GetUtcOffset(dateTime);

                        if (utcOffset.Ticks < 0)
                        {
                            kind = XsdDateTimeKind.LocalWestOfZulu;
                            zoneHour = -utcOffset.Hours;
                            zoneMinute = -utcOffset.Minutes;
                        }
                        else
                        {
                            kind = XsdDateTimeKind.LocalEastOfZulu;
                            zoneHour = utcOffset.Hours;
                            zoneMinute = utcOffset.Minutes;
                        }
                        break;
                    }
            }

            extra = (uint)(((int)code << TypeShift) | ((int)kind << KindShift) | (zoneHour << ZoneHourShift) | zoneMinute);
        }
Exemple #21
0
 /// <summary>
 /// Constructs an XsdDateTime from a string using specific format.
 /// </summary>
 public XsdDateTime(string text, XsdDateTimeFlags kinds) : this()
 {
     Parser parser = new Parser();
     if (!parser.Parse(text, kinds))
     {
         throw new FormatException(SR.Format(SR.XmlConvert_BadFormat, text, kinds));
     }
     InitiateXsdDateTime(parser);
 }
Exemple #22
0
            public bool Parse(string text, XsdDateTimeFlags kinds)
            {
                _text   = text;
                _length = text.Length;

                // Skip leading withitespace
                int start = 0;

                while (start < _length && char.IsWhiteSpace(text[start]))
                {
                    start++;
                }
                // Choose format starting from the most common and trying not to reparse the same thing too many times

#if !SILVERLIGHT // XDR is not supported in Silverlight
                if (Test(kinds, XsdDateTimeFlags.DateTime | XsdDateTimeFlags.Date | XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrDateTimeNoTz))
                {
#else
                if (Test(kinds, XsdDateTimeFlags.DateTime | XsdDateTimeFlags.Date))
                {
#endif
                    if (ParseDate(start))
                    {
                        if (Test(kinds, XsdDateTimeFlags.DateTime))
                        {
                            if (ParseChar(start + s_lzyyyy_MM_dd, 'T') && ParseTimeAndZoneAndWhitespace(start + s_lzyyyy_MM_ddT))
                            {
                                typeCode = DateTimeTypeCode.DateTime;
                                return(true);
                            }
                        }
                        if (Test(kinds, XsdDateTimeFlags.Date))
                        {
                            if (ParseZoneAndWhitespace(start + s_lzyyyy_MM_dd))
                            {
                                typeCode = DateTimeTypeCode.Date;
                                return(true);
                            }
                        }
#if !SILVERLIGHT // XDR is not supported in Silverlight
                        if (Test(kinds, XsdDateTimeFlags.XdrDateTime))
                        {
                            if (ParseZoneAndWhitespace(start + s_lzyyyy_MM_dd) || (ParseChar(start + s_lzyyyy_MM_dd, 'T') && ParseTimeAndZoneAndWhitespace(start + s_lzyyyy_MM_ddT)))
                            {
                                typeCode = DateTimeTypeCode.XdrDateTime;
                                return(true);
                            }
                        }
                        if (Test(kinds, XsdDateTimeFlags.XdrDateTimeNoTz))
                        {
                            if (ParseChar(start + s_lzyyyy_MM_dd, 'T'))
                            {
                                if (ParseTimeAndWhitespace(start + s_lzyyyy_MM_ddT))
                                {
                                    typeCode = DateTimeTypeCode.XdrDateTime;
                                    return(true);
                                }
                            }
                            else
                            {
                                typeCode = DateTimeTypeCode.XdrDateTime;
                                return(true);
                            }
                        }
#endif
                    }
                }

                if (Test(kinds, XsdDateTimeFlags.Time))
                {
                    if (ParseTimeAndZoneAndWhitespace(start))
                    { //Equivalent to NoCurrentDateDefault on DateTimeStyles while parsing xs:time
                        year     = leapYear;
                        month    = firstMonth;
                        day      = firstDay;
                        typeCode = DateTimeTypeCode.Time;
                        return(true);
                    }
                }

#if !SILVERLIGHT // XDR is not supported in Silverlight
                if (Test(kinds, XsdDateTimeFlags.XdrTimeNoTz))
                {
                    if (ParseTimeAndWhitespace(start))
                    { //Equivalent to NoCurrentDateDefault on DateTimeStyles while parsing xs:time
                        year     = leapYear;
                        month    = firstMonth;
                        day      = firstDay;
                        typeCode = DateTimeTypeCode.Time;
                        return(true);
                    }
                }
#endif

                if (Test(kinds, XsdDateTimeFlags.GYearMonth | XsdDateTimeFlags.GYear))
                {
                    if (Parse4Dig(start, ref year) && 1 <= year)
                    {
                        if (Test(kinds, XsdDateTimeFlags.GYearMonth))
                        {
                            if (
                                ParseChar(start + s_lzyyyy, '-') &&
                                Parse2Dig(start + s_lzyyyy_, ref month) && 1 <= month && month <= 12 &&
                                ParseZoneAndWhitespace(start + s_lzyyyy_MM)
                                )
                            {
                                day      = firstDay;
                                typeCode = DateTimeTypeCode.GYearMonth;
                                return(true);
                            }
                        }
                        if (Test(kinds, XsdDateTimeFlags.GYear))
                        {
                            if (ParseZoneAndWhitespace(start + s_lzyyyy))
                            {
                                month    = firstMonth;
                                day      = firstDay;
                                typeCode = DateTimeTypeCode.GYear;
                                return(true);
                            }
                        }
                    }
                }
                if (Test(kinds, XsdDateTimeFlags.GMonthDay | XsdDateTimeFlags.GMonth))
                {
                    if (
                        ParseChar(start, '-') &&
                        ParseChar(start + s_Lz_, '-') &&
                        Parse2Dig(start + s_Lz__, ref month) && 1 <= month && month <= 12
                        )
                    {
                        if (Test(kinds, XsdDateTimeFlags.GMonthDay) && ParseChar(start + s_lz__mm, '-'))
                        {
                            if (
                                Parse2Dig(start + s_lz__mm_, ref day) && 1 <= day && day <= DateTime.DaysInMonth(leapYear, month) &&
                                ParseZoneAndWhitespace(start + s_lz__mm_dd)
                                )
                            {
                                year     = leapYear;
                                typeCode = DateTimeTypeCode.GMonthDay;
                                return(true);
                            }
                        }
                        if (Test(kinds, XsdDateTimeFlags.GMonth))
                        {
                            if (ParseZoneAndWhitespace(start + s_lz__mm) || (ParseChar(start + s_lz__mm, '-') && ParseChar(start + s_lz__mm_, '-') && ParseZoneAndWhitespace(start + s_lz__mm__)))
                            {
                                year     = leapYear;
                                day      = firstDay;
                                typeCode = DateTimeTypeCode.GMonth;
                                return(true);
                            }
                        }
                    }
                }
                if (Test(kinds, XsdDateTimeFlags.GDay))
                {
                    if (
                        ParseChar(start, '-') &&
                        ParseChar(start + s_Lz_, '-') &&
                        ParseChar(start + s_Lz__, '-') &&
                        Parse2Dig(start + s_Lz___, ref day) && 1 <= day && day <= DateTime.DaysInMonth(leapYear, firstMonth) &&
                        ParseZoneAndWhitespace(start + s_lz___dd)

                        )
                    {
                        year     = leapYear;
                        month    = firstMonth;
                        typeCode = DateTimeTypeCode.GDay;
                        return(true);
                    }
                }
                return(false);
            }
Exemple #23
0
 private static bool Test(XsdDateTimeFlags left, XsdDateTimeFlags right)
 {
     return((left & right) != 0);
 }
 internal Datatype_dateTimeBase(XsdDateTimeFlags dateTimeFlags)
 {
     this.dateTimeFlags = dateTimeFlags;
 }
Exemple #25
0
            public bool Parse(string text, XsdDateTimeFlags kinds)
            {
                this.text   = text;
                this.length = text.Length;
                int start = 0;

                while ((start < this.length) && char.IsWhiteSpace(text[start]))
                {
                    start++;
                }
                if (Test(kinds, XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrDateTimeNoTz | XsdDateTimeFlags.Date | XsdDateTimeFlags.DateTime) && this.ParseDate(start))
                {
                    if ((Test(kinds, XsdDateTimeFlags.DateTime) && this.ParseChar(start + XsdDateTime.Lzyyyy_MM_dd, 'T')) && this.ParseTimeAndZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM_ddT))
                    {
                        this.typeCode = XsdDateTime.DateTimeTypeCode.DateTime;
                        return(true);
                    }
                    if (Test(kinds, XsdDateTimeFlags.Date) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM_dd))
                    {
                        this.typeCode = XsdDateTime.DateTimeTypeCode.Date;
                        return(true);
                    }
                    if (Test(kinds, XsdDateTimeFlags.XdrDateTime) && (this.ParseZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM_dd) || (this.ParseChar(start + XsdDateTime.Lzyyyy_MM_dd, 'T') && this.ParseTimeAndZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM_ddT))))
                    {
                        this.typeCode = XsdDateTime.DateTimeTypeCode.XdrDateTime;
                        return(true);
                    }
                    if (Test(kinds, XsdDateTimeFlags.XdrDateTimeNoTz))
                    {
                        if (!this.ParseChar(start + XsdDateTime.Lzyyyy_MM_dd, 'T'))
                        {
                            this.typeCode = XsdDateTime.DateTimeTypeCode.XdrDateTime;
                            return(true);
                        }
                        if (this.ParseTimeAndWhitespace(start + XsdDateTime.Lzyyyy_MM_ddT))
                        {
                            this.typeCode = XsdDateTime.DateTimeTypeCode.XdrDateTime;
                            return(true);
                        }
                    }
                }
                if (Test(kinds, XsdDateTimeFlags.Time) && this.ParseTimeAndZoneAndWhitespace(start))
                {
                    this.year     = 0x770;
                    this.month    = 1;
                    this.day      = 1;
                    this.typeCode = XsdDateTime.DateTimeTypeCode.Time;
                    return(true);
                }
                if (Test(kinds, XsdDateTimeFlags.XdrTimeNoTz) && this.ParseTimeAndWhitespace(start))
                {
                    this.year     = 0x770;
                    this.month    = 1;
                    this.day      = 1;
                    this.typeCode = XsdDateTime.DateTimeTypeCode.Time;
                    return(true);
                }
                if ((Test(kinds, XsdDateTimeFlags.GYear | XsdDateTimeFlags.GYearMonth) && this.Parse4Dig(start, ref this.year)) && (1 <= this.year))
                {
                    if (((Test(kinds, XsdDateTimeFlags.GYearMonth) && this.ParseChar(start + XsdDateTime.Lzyyyy, '-')) && (this.Parse2Dig(start + XsdDateTime.Lzyyyy_, ref this.month) && (1 <= this.month))) && ((this.month <= 12) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lzyyyy_MM)))
                    {
                        this.day      = 1;
                        this.typeCode = XsdDateTime.DateTimeTypeCode.GYearMonth;
                        return(true);
                    }
                    if (Test(kinds, XsdDateTimeFlags.GYear) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lzyyyy))
                    {
                        this.month    = 1;
                        this.day      = 1;
                        this.typeCode = XsdDateTime.DateTimeTypeCode.GYear;
                        return(true);
                    }
                }
                if (((Test(kinds, XsdDateTimeFlags.GMonth | XsdDateTimeFlags.GMonthDay) && this.ParseChar(start, '-')) && (this.ParseChar(start + XsdDateTime.Lz_, '-') && this.Parse2Dig(start + XsdDateTime.Lz__, ref this.month))) && ((1 <= this.month) && (this.month <= 12)))
                {
                    if (((Test(kinds, XsdDateTimeFlags.GMonthDay) && this.ParseChar(start + XsdDateTime.Lz__mm, '-')) && (this.Parse2Dig(start + XsdDateTime.Lz__mm_, ref this.day) && (1 <= this.day))) && ((this.day <= DateTime.DaysInMonth(0x770, this.month)) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lz__mm_dd)))
                    {
                        this.year     = 0x770;
                        this.typeCode = XsdDateTime.DateTimeTypeCode.GMonthDay;
                        return(true);
                    }
                    if (Test(kinds, XsdDateTimeFlags.GMonth) && (this.ParseZoneAndWhitespace(start + XsdDateTime.Lz__mm) || ((this.ParseChar(start + XsdDateTime.Lz__mm, '-') && this.ParseChar(start + XsdDateTime.Lz__mm_, '-')) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lz__mm__))))
                    {
                        this.year     = 0x770;
                        this.day      = 1;
                        this.typeCode = XsdDateTime.DateTimeTypeCode.GMonth;
                        return(true);
                    }
                }
                if (((Test(kinds, XsdDateTimeFlags.GDay) && this.ParseChar(start, '-')) && (this.ParseChar(start + XsdDateTime.Lz_, '-') && this.ParseChar(start + XsdDateTime.Lz__, '-'))) && ((this.Parse2Dig(start + XsdDateTime.Lz___, ref this.day) && (1 <= this.day)) && ((this.day <= DateTime.DaysInMonth(0x770, 1)) && this.ParseZoneAndWhitespace(start + XsdDateTime.Lz___dd))))
                {
                    this.year     = 0x770;
                    this.month    = 1;
                    this.typeCode = XsdDateTime.DateTimeTypeCode.GDay;
                    return(true);
                }
                return(false);
            }