Пример #1
0
        private static List <FormatInfo> PythonFormatToCLIFormat(string format, bool forParse, out bool postProcess, out FoundDateComponents found)
        {
            postProcess = false;
            found       = FoundDateComponents.None;
            List <FormatInfo> newFormat = new List <FormatInfo>();


            for (int i = 0; i < format.Length; i++)
            {
                if (format[i] == '%')
                {
                    if (i + 1 == format.Length)
                    {
                        throw PythonOps.ValueError("badly formatted string");
                    }

                    switch (format[++i])
                    {
                    case 'a': newFormat.Add(new FormatInfo("ddd")); break;

                    case 'A': newFormat.Add(new FormatInfo("dddd")); break;

                    case 'b':
                        newFormat.Add(new FormatInfo("MMM"));
                        break;

                    case 'B':
                        newFormat.Add(new FormatInfo("MMMM"));
                        break;

                    case 'c':
                        found |= FoundDateComponents.Date;
                        AddDate(newFormat);
                        newFormat.Add(new FormatInfo(FormatInfoType.UserText, " "));
                        AddTime(newFormat);
                        break;

                    case 'd':
                        // if we're parsing we want to use the less-strict
                        // d format and which doesn't require both digits.
                        if (forParse)
                        {
                            newFormat.Add(new FormatInfo(FormatInfoType.CustomFormat, "d"));
                        }
                        else
                        {
                            newFormat.Add(new FormatInfo("dd"));
                        }
                        break;

                    case 'H': newFormat.Add(new FormatInfo("HH")); break;

                    case 'I': newFormat.Add(new FormatInfo("hh")); break;

                    case 'm':
                        newFormat.Add(new FormatInfo(forParse ? "M" : "MM"));
                        break;

                    case 'M': newFormat.Add(new FormatInfo(forParse ? "m" : "mm")); break;

                    case 'p':
                        newFormat.Add(new FormatInfo(FormatInfoType.CustomFormat, "t"));
                        newFormat.Add(new FormatInfo(FormatInfoType.UserText, "M"));
                        break;

                    case 'S': newFormat.Add(new FormatInfo("ss")); break;

                    case 'x':
                        found |= FoundDateComponents.Date;
                        AddDate(newFormat); break;

                    case 'X':
                        AddTime(newFormat);
                        break;

                    case 'y':
                        found |= FoundDateComponents.Year;
                        newFormat.Add(new FormatInfo("yy"));
                        break;

                    case 'Y':
                        found |= FoundDateComponents.Year;
                        newFormat.Add(new FormatInfo("yyyy"));
                        break;

                    case '%': newFormat.Add(new FormatInfo("\\%")); break;

                    // format conversions not defined by the CLR.  We leave
                    // them as \\% and then replace them by hand later
                    case 'j':     // day of year
                        newFormat.Add(new FormatInfo("\\%j"));
                        postProcess = true;
                        break;

                    case 'f':
                        postProcess = true;
                        newFormat.Add(new FormatInfo(FormatInfoType.UserText, "%f"));
                        break;

                    case 'W': newFormat.Add(new FormatInfo("\\%W")); postProcess = true; break;

                    case 'U': newFormat.Add(new FormatInfo("\\%U")); postProcess = true; break;     // week number

                    case 'w': newFormat.Add(new FormatInfo("\\%w")); postProcess = true; break;     // weekday number

                    case 'z':
                    case 'Z':
                        // !!!TODO:
                        // 'z' for offset
                        // 'Z' for time zone name; could be from PythonTimeZoneInformation
                        newFormat.Add(new FormatInfo(FormatInfoType.UserText, ""));
                        break;

                    default:
                        newFormat.Add(new FormatInfo(FormatInfoType.UserText, "")); break;
                    }
                }
                else
                {
                    if (newFormat.Count == 0 || newFormat[newFormat.Count - 1].Type != FormatInfoType.UserText)
                    {
                        newFormat.Add(new FormatInfo(FormatInfoType.UserText, format[i].ToString()));
                    }
                    else
                    {
                        newFormat[newFormat.Count - 1].Text = newFormat[newFormat.Count - 1].Text + format[i];
                    }
                }
            }

            return(newFormat);
        }
Пример #2
0
        private static List<FormatInfo> PythonFormatToCLIFormat(string format, bool forParse, out bool postProcess, out FoundDateComponents found) {
            postProcess = false;
            found = FoundDateComponents.None;
            List<FormatInfo> newFormat = new List<FormatInfo>();
            

            for (int i = 0; i < format.Length; i++) {
                if (format[i] == '%') {
                    if (i + 1 == format.Length) throw PythonOps.ValueError("badly formatted string");

                    switch (format[++i]) {
                        case 'a':
                            found |= FoundDateComponents.DayOfWeek;
                            newFormat.Add(new FormatInfo("ddd")); break;
                        case 'A':
                            found |= FoundDateComponents.DayOfWeek;
                            newFormat.Add(new FormatInfo("dddd")); break;
                        case 'b': 
                            newFormat.Add(new FormatInfo("MMM")); 
                            break;
                        case 'B':
                            newFormat.Add(new FormatInfo("MMMM")); 
                            break;
                        case 'c':
                            found |= FoundDateComponents.Date;
                            AddDate(newFormat);
                            newFormat.Add(new FormatInfo(FormatInfoType.UserText, " "));
                            AddTime(newFormat);
                            break;
                        case 'd':
                            // if we're parsing we want to use the less-strict
                            // d format and which doesn't require both digits.
                            if (forParse) newFormat.Add(new FormatInfo(FormatInfoType.CustomFormat, "d"));
                            else newFormat.Add(new FormatInfo("dd"));
                            break;
                        case 'H': newFormat.Add(new FormatInfo(forParse ? "H" : "HH")); break;
                        case 'I': newFormat.Add(new FormatInfo(forParse ? "h" : "hh")); break;
                        case 'm':
                            newFormat.Add(new FormatInfo(forParse ? "M" : "MM"));
                            break;
                        case 'M': newFormat.Add(new FormatInfo(forParse ? "m" : "mm")); break;
                        case 'p':
                            newFormat.Add(new FormatInfo(FormatInfoType.CustomFormat, "t"));
                            newFormat.Add(new FormatInfo(FormatInfoType.UserText, "M"));
                            break;
                        case 'S': newFormat.Add(new FormatInfo("ss")); break;
                        case 'x':
                            found |= FoundDateComponents.Date;
                            AddDate(newFormat); break;
                        case 'X':
                            AddTime(newFormat);
                            break;
                        case 'y':
                            found |= FoundDateComponents.Year;
                            newFormat.Add(new FormatInfo("yy")); 
                            break;
                        case 'Y':
                            found |= FoundDateComponents.Year;
                            newFormat.Add(new FormatInfo("yyyy")); 
                            break;
                        case '%': newFormat.Add(new FormatInfo("\\%")); break;

                        // format conversions not defined by the CLR.  We leave
                        // them as \\% and then replace them by hand later
                        case 'j': // day of year
                            newFormat.Add(new FormatInfo("\\%j")); 
                            postProcess = true; 
                            break;
                        case 'f':
                            postProcess = true;
                            newFormat.Add(new FormatInfo(FormatInfoType.UserText, "%f")); 
                            break;
                        case 'W': newFormat.Add(new FormatInfo("\\%W")); postProcess = true; break;
                        case 'U': newFormat.Add(new FormatInfo("\\%U")); postProcess = true; break; // week number
                        case 'w': newFormat.Add(new FormatInfo("\\%w")); postProcess = true; break; // weekday number
                        case 'z':
                        case 'Z':
                            // !!!TODO: 
                            // 'z' for offset
                            // 'Z' for time zone name; could be from PythonTimeZoneInformation
                            newFormat.Add(new FormatInfo(FormatInfoType.UserText, ""));
                            break;
                        default:
                            newFormat.Add(new FormatInfo(FormatInfoType.UserText, "")); break;
                    }
                } else {
                    if (newFormat.Count == 0 || newFormat[newFormat.Count - 1].Type != FormatInfoType.UserText)
                        newFormat.Add(new FormatInfo(FormatInfoType.UserText, format[i].ToString()));
                    else
                        newFormat[newFormat.Count - 1].Text = newFormat[newFormat.Count - 1].Text + format[i];
                }
            }

            return newFormat;
        }