ParseErr.
Inheritance: Err
示例#1
0
文件: ParseErr.cs 项目: nomit007/f4
 public static void make_(ParseErr self, string msg, Err cause)
 {
     Err.make_(self, msg, cause);
 }
示例#2
0
        public static TimeZone fromStr(string name, bool check)
        {
            // check cache first
            TimeZone tz;

            lock (cache)
            {
                tz = (TimeZone)cache[name];
                if (tz != null)
                {
                    return(tz);
                }
            }

            // try to load from database
            try
            {
                tz = loadTimeZone(name);
            }
            catch (Exception e)
            {
                Err.dumpStack(e);
                throw IOErr.make("Cannot load from timezone database: " + name).val;
            }

            // if not found, check aliases
            if (tz == null)
            {
                if (aliases == null)
                {
                    loadAliases();
                }
                string alias = (string)aliases[name];
                if (alias != null)
                {
                    tz = fromStr(alias); // better be found
                    lock (cache)
                    {
                        cache[tz.m_name]     = tz;
                        cache[tz.m_fullName] = tz;
                        return(tz);
                    }
                }
            }

            // if found, then cache and return
            if (tz != null)
            {
                lock (cache)
                {
                    cache[tz.m_name]     = tz;
                    cache[tz.m_fullName] = tz;
                    return(tz);
                }
            }

            // not found
            if (check)
            {
                throw ParseErr.make("TimeZone not found: " + name).val;
            }
            return(null);
        }
示例#3
0
文件: ParseErr.cs 项目: nomit007/f4
 public static void make_(ParseErr self)
 {
     make_(self, null);
 }
示例#4
0
文件: ParseErr.cs 项目: nomit007/f4
 public static void make_(ParseErr self, string msg)
 {
     make_(self, msg, null);
 }
示例#5
0
        //////////////////////////////////////////////////////////////////////////
        // Parse
        //////////////////////////////////////////////////////////////////////////

        internal DateTime parseDateTime(string s, TimeZone defTz, bool check)
        {
            try
            {
                // parse into fields
                tzOffset = System.Int32.MaxValue;
                parse(s);

                // now figure out what timezone to use
                TimeZone.Rule defRule = defTz.rule(year);
                if (tzName != null)
                {
                    // use defTz if tzName was specified and matches any variations of defTz
                    if (tzName == defTz.name() ||
                        tzName == defRule.stdAbbr ||
                        tzName == defRule.dstAbbr)
                    {
                        tz = defTz;
                    }

                    // try to map tzName to TimeZone, use defTz as fallback
                    else
                    {
                        tz = TimeZone.fromStr(tzName, false);
                        if (tz == null)
                        {
                            tz = defTz;
                        }
                    }
                }

                // if tzOffset was specified...
                else if (tzOffset != System.Int32.MaxValue)
                {
                    // figure out what expected offset was for defTz
                    int time      = hour * 3600 + min * 60 + sec;
                    int defOffset = defRule.offset + TimeZone.dstOffset(defRule, year, (int)mon.ordinal(), day, time);

                    // if specified offset matches expected offset for defTz then
                    // use defTz, otherwise use a vanilla GMT+/- timezone
                    if (tzOffset == defOffset)
                    {
                        tz = defTz;
                    }
                    else
                    {
                        tz = TimeZone.fromGmtOffset(tzOffset);
                    }
                }

                // no tzName or tzOffset specified, use defTz
                else
                {
                    tz = defTz;
                }

                // construct DateTime
                return(new DateTime(year, (int)mon.ordinal(), day, hour, min, sec, ns, tzOffset, tz));
            }
            catch (Exception) {}
            if (check)
            {
                throw ParseErr.make("DateTime", s).val;
            }
            return(null);
        }
示例#6
0
文件: ParseErr.cs 项目: nomit007/f4
 public static new ParseErr make(string msg, Err cause)
 {
     ParseErr err = new ParseErr();
       make_(err, msg, cause);
       return err;
 }
示例#7
0
        public static MimeType fromStr(string s, bool check)
        {
            try
            {
                // common interned mime types
                switch (s[0])
                {
                case 'i':
                    if (s == "image/png")
                    {
                        return(m_imagePng);
                    }
                    if (s == "image/jpeg")
                    {
                        return(m_imageJpeg);
                    }
                    if (s == "image/gif")
                    {
                        return(m_imageGif);
                    }
                    break;

                case 't':
                    if (s == "text/plain")
                    {
                        return(m_textPlain);
                    }
                    if (s == "text/html")
                    {
                        return(m_textHtml);
                    }
                    if (s == "text/xml")
                    {
                        return(m_textXml);
                    }
                    if (s == "text/plain; charset=utf-8")
                    {
                        return(m_textPlainUtf8);
                    }
                    if (s == "text/html; charset=utf-8")
                    {
                        return(m_textHtmlUtf8);
                    }
                    if (s == "text/xml; charset=utf-8")
                    {
                        return(m_textXmlUtf8);
                    }
                    break;

                case 'x':
                    if (s == "x-directory/normal")
                    {
                        return(m_dir);
                    }
                    break;
                }

                return(parse(s));
            }
            catch (ParseErr.Val e)
            {
                if (!check)
                {
                    return(null);
                }
                throw e;
            }
            catch (System.Exception)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("MimeType", s).val;
            }
        }
示例#8
0
 public static void make_(ParseErr self, string msg, Err cause)
 {
     Err.make_(self, msg, cause);
 }
示例#9
0
 public static void make_(ParseErr self, string msg)
 {
     make_(self, msg, null);
 }
示例#10
0
 public static void make_(ParseErr self)
 {
     make_(self, null);
 }
示例#11
0
        private static DateTime fromStr(string s, bool check, bool iso)
        {
            try
            {
                // YYYY-MM-DD'T'hh:mm:ss
                int year  = num(s, 0) * 1000 + num(s, 1) * 100 + num(s, 2) * 10 + num(s, 3);
                int month = num(s, 5) * 10 + num(s, 6) - 1;
                int day   = num(s, 8) * 10 + num(s, 9);
                int hour  = num(s, 11) * 10 + num(s, 12);
                int min   = num(s, 14) * 10 + num(s, 15);
                int sec   = num(s, 17) * 10 + num(s, 18);

                // check separator symbols
                if (s[4] != '-' || s[7] != '-' ||
                    s[10] != 'T' || s[13] != ':' ||
                    s[16] != ':')
                {
                    throw new System.Exception();
                }

                // optional .FFFFFFFFF
                int i     = 19;
                int ns    = 0;
                int tenth = 100000000;
                if (s[i] == '.')
                {
                    ++i;
                    while (true)
                    {
                        int c = s[i];
                        if (c < '0' || c > '9')
                        {
                            break;
                        }
                        ns    += (c - '0') * tenth;
                        tenth /= 10;
                        ++i;
                    }
                }

                // zone offset
                int offset = 0;
                int ch     = s[i++];
                if (ch != 'Z')
                {
                    int offHour = num(s, i++) * 10 + num(s, i++);
                    if (s[i++] != ':')
                    {
                        throw new System.Exception();
                    }
                    int offMin = num(s, i++) * 10 + num(s, i++);
                    offset = offHour * 3600 + offMin * 60;
                    if (ch == '-')
                    {
                        offset = -offset;
                    }
                    else if (ch != '+')
                    {
                        throw new System.Exception();
                    }
                }

                // timezone - we share this method b/w fromStr and fromIso
                TimeZone tz;
                if (iso)
                {
                    if (i < s.Length)
                    {
                        throw new System.Exception();
                    }
                    tz = TimeZone.fromGmtOffset(offset);
                }
                else
                {
                    if (s[i++] != ' ')
                    {
                        throw new System.Exception();
                    }
                    tz = TimeZone.fromStr(s.Substring(i), true);
                }

                return(new DateTime(year, month, day, hour, min, sec, ns, offset, tz));
            }
            catch (System.Exception)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("DateTime", s).val;
            }
        }