Ejemplo n.º 1
0
        public static bool StartsWith(this StringReference s, string text)
        {
            if (text.Length > s.Length)
            {
                return(false);
            }

            var chars = s.Chars;

            for (var i = 0; i < text.Length; i++)
            {
                if (text[i] != chars[i + s.StartIndex])
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        internal static bool TryParseDateTime(StringReference s, DateTimeZoneHandling dateTimeZoneHandling, string?dateFormatString,
                                              CultureInfo culture, out DateTime dt
                                              )
        {
            if (s.Length > 0)
            {
                var i = s.StartIndex;
                if (s[i] == '/')
                {
                    if (s.Length >= 9 && s.StartsWith("/Date(") && s.EndsWith(")/"))
                    {
                        if (TryParseDateTimeMicrosoft(s, dateTimeZoneHandling, out dt))
                        {
                            return(true);
                        }
                    }
                }
                else if (s.Length >= 19 && s.Length <= 40 && char.IsDigit(s[i]) && s[i + 10] == 'T')
                {
                    if (TryParseDateTimeIso(s, dateTimeZoneHandling, out dt))
                    {
                        return(true);
                    }
                }

                if (!StringUtils.IsNullOrEmpty(dateFormatString))
                {
                    if (TryParseDateTimeExact(s.ToString(), dateTimeZoneHandling, dateFormatString, culture, out dt))
                    {
                        return(true);
                    }
                }
            }

            dt = default;
            return(false);
        }
Ejemplo n.º 3
0
        internal static bool TryParseDateTimeIso(StringReference text, DateTimeZoneHandling dateTimeZoneHandling, out DateTime dt)
        {
            var dateTimeParser = new DateTimeParser();

            if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length))
            {
                dt = default;
                return(false);
            }

            var d = CreateDateTime(dateTimeParser);

            long ticks;

            switch (dateTimeParser.Zone)
            {
            case ParserTimeZone.Utc:
                d = new DateTime(d.Ticks, DateTimeKind.Utc);
                break;

            case ParserTimeZone.LocalWestOfUtc:
            {
                var offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0);
                ticks = d.Ticks + offset.Ticks;
                if (ticks <= DateTime.MaxValue.Ticks)
                {
                    d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime();
                }
                else
                {
                    ticks += d.GetUtcOffset().Ticks;
                    if (ticks > DateTime.MaxValue.Ticks)
                    {
                        ticks = DateTime.MaxValue.Ticks;
                    }

                    d = new DateTime(ticks, DateTimeKind.Local);
                }
                break;
            }

            case ParserTimeZone.LocalEastOfUtc:
            {
                var offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0);
                ticks = d.Ticks - offset.Ticks;
                if (ticks >= DateTime.MinValue.Ticks)
                {
                    d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime();
                }
                else
                {
                    ticks += d.GetUtcOffset().Ticks;
                    if (ticks < DateTime.MinValue.Ticks)
                    {
                        ticks = DateTime.MinValue.Ticks;
                    }

                    d = new DateTime(ticks, DateTimeKind.Local);
                }
                break;
            }
            }

            dt = EnsureDateTime(d, dateTimeZoneHandling);
            return(true);
        }