コード例 #1
0
ファイル: time.cs プロジェクト: IronLanguages/ironpython3
        internal static string strftime(CodeContext /*!*/ context, string format, DateTime dt, int?microseconds)
        {
            bool postProc;
            List <FormatInfo> formatInfoList = PythonFormatToCLIFormat(format, false, out postProc, out _);
            StringBuilder     res            = new StringBuilder();
            var lc_info = PythonLocale.GetLocaleInfo(context);

            foreach (FormatInfo formatInfo in formatInfoList)
            {
                switch (formatInfo.Type)
                {
                case FormatInfoType.UserText: res.Append(formatInfo.Text); break;

                case FormatInfoType.SimpleFormat: res.Append(dt.ToString(formatInfo.Text, lc_info.Time.DateTimeFormat)); break;

                case FormatInfoType.CustomFormat:
                    // custom format strings need to be at least 2 characters long
                    string custom_field_format = formatInfo.Text.Length == 1 ? "%" + formatInfo.Text : formatInfo.Text;
                    res.Append(dt.ToString(custom_field_format, lc_info.Time.DateTimeFormat));
                    break;
                }
            }

            if (postProc)
            {
                res = res.Replace("%f", microseconds != null ? string.Format("{0:D6}", microseconds) : "");

                res = res.Replace("%j", dt.DayOfYear.ToString("D03"));  // day of the year (001 - 366)

                // figure out first day of the year...
                DateTime first         = new DateTime(dt.Year, 1, 1);
                int      weekOneSunday = (7 - (int)first.DayOfWeek) % 7;
                int      dayOffset     = (8 - (int)first.DayOfWeek) % 7;

                // week of year  (sunday first day, 0-53), all days before Sunday are 0
                res = res.Replace("%U", (((dt.DayOfYear + 6 - weekOneSunday) / 7)).ToString());
                // week number of year (monday first day, 0-53), all days before Monday are 0
                res = res.Replace("%W", (((dt.DayOfYear + 6 - dayOffset) / 7)).ToString());
                res = res.Replace("%w", ((int)dt.DayOfWeek).ToString());
            }
            return(res.ToString());
        }
コード例 #2
0
ファイル: time.cs プロジェクト: rudimk/dlr-dotnet
 public static void PerformModuleReload(PythonContext /*!*/ context, PythonDictionary /*!*/ dict)
 {
     // we depend on locale, it needs to be initialized
     PythonLocale.EnsureLocaleInitialized(context);
 }
コード例 #3
0
ファイル: time.cs プロジェクト: rudimk/dlr-dotnet
        public static object strptime(CodeContext /*!*/ context, string @string, string format)
        {
            bool postProc;
            FoundDateComponents foundDateComp;
            List <FormatInfo>   formatInfo = PythonFormatToCLIFormat(format, true, out postProc, out foundDateComp);

            DateTime res;

            if (postProc)
            {
                int doyIndex  = FindFormat(formatInfo, "\\%j");
                int dowMIndex = FindFormat(formatInfo, "\\%W");
                int dowSIndex = FindFormat(formatInfo, "\\%U");

                if (doyIndex != -1 && dowMIndex == -1 && dowSIndex == -1)
                {
                    res = new DateTime(1900, 1, 1);
                    res = res.AddDays(Int32.Parse(@string));
                }
                else if (dowMIndex != -1 && doyIndex == -1 && dowSIndex == -1)
                {
                    res = new DateTime(1900, 1, 1);
                    res = res.AddDays(Int32.Parse(@string) * 7);
                }
                else if (dowSIndex != -1 && doyIndex == -1 && dowMIndex == -1)
                {
                    res = new DateTime(1900, 1, 1);
                    res = res.AddDays(Int32.Parse(@string) * 7);
                }
                else
                {
                    throw PythonOps.ValueError("cannot parse %j, %W, or %U w/ other values");
                }
            }
            else
            {
                string[] formats = new string[formatInfo.Count];
                for (int i = 0; i < formatInfo.Count; i++)
                {
                    switch (formatInfo[i].Type)
                    {
                    case FormatInfoType.UserText: formats[i] = "'" + formatInfo[i].Text + "'"; break;

                    case FormatInfoType.SimpleFormat: formats[i] = formatInfo[i].Text; break;

                    case FormatInfoType.CustomFormat:
                        // include % if we only have one specifier to mark that it's a custom
                        // specifier
                        if (formatInfo.Count == 1 && formatInfo[i].Text.Length == 1)
                        {
                            formats[i] = "%" + formatInfo[i].Text;
                        }
                        else
                        {
                            formats[i] = formatInfo[i].Text;
                        }
                        break;
                    }
                }

                try {
                    if (!StringUtils.TryParseDateTimeExact(@string,
                                                           String.Join("", formats),
                                                           PythonLocale.GetLocaleInfo(context).Time.DateTimeFormat,
                                                           DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.NoCurrentDateDefault,
                                                           out res))
                    {
                        // If TryParseExact fails, fall back to DateTime.Parse which does a better job in some cases...
                        res = DateTime.Parse(@string, PythonLocale.GetLocaleInfo(context).Time.DateTimeFormat);
                    }
                } catch (FormatException e) {
                    throw PythonOps.ValueError(e.Message + Environment.NewLine + "data=" + @string + ", fmt=" + format + ", to: " + String.Join("", formats));
                }
            }

            if ((foundDateComp & FoundDateComponents.Year) == 0)
            {
                res = new DateTime(1900, res.Month, res.Day, res.Hour, res.Minute, res.Second, res.Millisecond, res.Kind);
            }

            return(GetDateTimeTuple(res));
        }
コード例 #4
0
ファイル: time.cs プロジェクト: rudimk/dlr-dotnet
 public static object strptime(CodeContext /*!*/ context, string @string)
 {
     return(DateTime.Parse(@string, PythonLocale.GetLocaleInfo(context).Time.DateTimeFormat));
 }
コード例 #5
0
ファイル: time.cs プロジェクト: zhangjiedy/ironpython3
        // returns object array containing 2 elements: DateTime and DayOfWeek
        internal static object[] _strptime(CodeContext /*!*/ context, string @string, string format)
        {
            bool postProc;
            FoundDateComponents foundDateComp;
            List <FormatInfo>   formatInfo = PythonFormatToCLIFormat(format, true, out postProc, out foundDateComp);

            DateTime res;

            if (postProc)
            {
                int doyIndex  = FindFormat(formatInfo, "\\%j");
                int dowMIndex = FindFormat(formatInfo, "\\%W");
                int dowSIndex = FindFormat(formatInfo, "\\%U");

                if (doyIndex != -1 && dowMIndex == -1 && dowSIndex == -1)
                {
                    res = new DateTime(1900, 1, 1);
                    res = res.AddDays(int.Parse(@string));
                }
                else if (dowMIndex != -1 && doyIndex == -1 && dowSIndex == -1)
                {
                    res = new DateTime(1900, 1, 1);
                    res = res.AddDays(int.Parse(@string) * 7);
                }
                else if (dowSIndex != -1 && doyIndex == -1 && dowMIndex == -1)
                {
                    res = new DateTime(1900, 1, 1);
                    res = res.AddDays(int.Parse(@string) * 7);
                }
                else
                {
                    throw PythonOps.ValueError("cannot parse %j, %W, or %U w/ other values");
                }
            }
            else
            {
                var      fIdx        = -1;
                string[] formatParts = new string[formatInfo.Count];
                for (int i = 0; i < formatInfo.Count; i++)
                {
                    switch (formatInfo[i].Type)
                    {
                    case FormatInfoType.UserText: formatParts[i] = "'" + formatInfo[i].Text + "'"; break;

                    case FormatInfoType.SimpleFormat: formatParts[i] = formatInfo[i].Text; break;

                    case FormatInfoType.CustomFormat:
                        if (formatInfo[i].Text == "f")
                        {
                            fIdx = i;
                        }
                        // include % if we only have one specifier to mark that it's a custom
                        // specifier
                        if (formatInfo.Count == 1 && formatInfo[i].Text.Length == 1)
                        {
                            formatParts[i] = "%" + formatInfo[i].Text;
                        }
                        else
                        {
                            formatParts[i] = formatInfo[i].Text;
                        }
                        break;
                    }
                }
                var formats =
                    fIdx == -1 ? new [] { string.Join("", formatParts) } : ExpandMicrosecondFormat(fIdx, formatParts);
                try {
                    if (!StringUtils.TryParseDateTimeExact(@string,
                                                           formats,
                                                           PythonLocale.GetLocaleInfo(context).Time.DateTimeFormat,
                                                           DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.NoCurrentDateDefault,
                                                           out res))
                    {
                        throw PythonOps.ValueError("time data does not match format" + Environment.NewLine +
                                                   "data=" + @string + ", fmt=" + format + ", to: " + formats[0]);
                    }
                } catch (FormatException e) {
                    throw PythonOps.ValueError(e.Message + Environment.NewLine +
                                               "data=" + @string + ", fmt=" + format + ", to: " + formats[0]);
                }
            }

            DayOfWeek?dayOfWeek = null;

            if ((foundDateComp & FoundDateComponents.DayOfWeek) != 0)
            {
                dayOfWeek = res.DayOfWeek;
            }

            if ((foundDateComp & FoundDateComponents.Year) == 0)
            {
                res = new DateTime(1900, res.Month, res.Day, res.Hour, res.Minute, res.Second, res.Millisecond, res.Kind);
            }

            return(new object[] { res, dayOfWeek });
        }