当前位置: 首页>>代码示例>>C#>>正文


C# ParseContext.Advance方法代码示例

本文整理汇总了C#中ParseContext.Advance方法的典型用法代码示例。如果您正苦于以下问题:C# ParseContext.Advance方法的具体用法?C# ParseContext.Advance怎么用?C# ParseContext.Advance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ParseContext的用法示例。


在下文中一共展示了ParseContext.Advance方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParseDateTime

        protected bool ParseDateTime(string s, DateTimePart part)
        {
            ParseContext context = new ParseContext(s);

            bool bDatePart = ( part & DateTimePart.Date ) != 0;
            bool bTimePart = ( part & DateTimePart.Time ) != 0;

            int year = 0, month = 0, day = 0;
            int hour = 0, minute = 0;
            double second = 0;

            eTZ = ETZ.Missing;
            offsetTZ = 0;

            if ( bDatePart )
            {
                // parse date
                bool bNegative = context.CheckAndAdvance( '-' );

                if ( (part & DateTimePart.Year ) != 0 )
                {
                    int digits = 0;
                    int temp = 0;
                    while ( context.ReadDigitAndAdvance( ref temp, 1, 9 ) )
                    {
                        year = year * 10 + temp;
                        digits += 1;
                        temp = 0;
                        if (digits >= 8) // overflow
                            return false;
                    }
                    if ( digits < 4 ) // invalid.
                        return false;
                    if ( digits > 4 && year < 10000 )
                        return false;
                    if (bNegative)
                        year = -year;
                }

                if ( (part & ( DateTimePart.Month | DateTimePart.Day )) != 0 )
                {
                    if ( !context.CheckAndAdvance( '-' ) ) return false;

                    if ( ( part & DateTimePart.Month ) != 0 )
                    {
                        if ( !context.ReadDigitAndAdvance( ref month, 10, 1 ) ) return false;
                        if ( !context.ReadDigitAndAdvance( ref month, 1, month < 10 ? 9 : 2 ) ) return false;
                        if ( month == 0 ) return false;
                    }

                    if ( ( part & DateTimePart.Day ) != 0 )
                    {
                        if ( !context.CheckAndAdvance(  '-') ) return false;

                        int maxFirstDigit = month != 2 ? 3 : 2;

                        // complicate things by making them complicated.
                        if ( !context.ReadDigitAndAdvance( ref day, 10, maxFirstDigit ) ) return false;
                        if ( !context.ReadDigitAndAdvance( ref day, 1, 9 ) ) return false;
                        if ( day == 0 || day > 31 ) return false;

                        if ( ( part & DateTimePart.Month ) != 0 )
                        {
                            bool b1 = month <= 7;
                            bool b2 = ( month & 1 ) == 0;

                            // month 1, 3, 5, 7, 8, 10, 12
                            if ( b1 == b2 && day > 30 )
                                return false;

                            // february.
                            if ( month == 2 && day > 29 )
                                return false;

                            // leap years.
                            if ( month == 2 && ( part & DateTimePart.Year ) != 0 &&
                                ( year % 4 != 0 || year % 100 == 0 ) && year % 400 != 0 &&
                                day > 28 )
                                return false;
                        }
                    }
                }

                if ( bTimePart )
                {
                    // a 'T' must follow
                    if ( !context.CheckAndAdvance( 'T') ) return false;
                }
            }

            if ( bTimePart )
            {
                // check format here

                // hour from 0 to 2
                if ( !context.ReadDigitAndAdvance( ref hour, 10, 2 ) ) return false;
                if ( !context.ReadDigitAndAdvance( ref hour, 1, hour < 20 ? 9 : 4 ) ) return false;
                if ( !context.CheckAndAdvance( ':' ) ) return false;
                int maxFirstDigit = hour == 24 ? 0 : 5;
                int maxSecondDigit = hour == 24 ? 0 : 9;
//.........这里部分代码省略.........
开发者ID:paladin74,项目名称:Dapple,代码行数:101,代码来源:SchemaTypes.cs


注:本文中的ParseContext.Advance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。