Beispiel #1
0
        /// <summary>
        /// 使用所有Kou支持的单位获取时间间隔;(支持中文)(纯数字默认为s)(若是日期会自动转换为距离当前时间的时间间隔)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="timeSpan"></param>
        /// <param name="kouType"></param>
        /// <returns></returns>
        public static bool TryToTimeSpan(string str, out TimeSpan timeSpan, bool kouType = true)
        {
            if (str.Length > 1000)
            {
                return(false);                  //这么大基本是来找麻烦的
            }
            bool success = false;

            timeSpan = new TimeSpan();
            if (str.IsNullOrWhiteSpace())
            {
                return(false);
            }
            if (!kouType)
            {
                return(TimeSpan.TryParse(str, out timeSpan));
            }
            if (ZhNumber.IsContainZhNumber(str))
            {
                str = ZhNumber.ToArabicNumber(str);
            }
            if (TryGetTimeSpanFromZhDescription(str, out timeSpan))
            {
                return(true);
            }
            if (DateTime.TryParse(str, out DateTime dateTime)) //使用日期格式尝试转换
            {
                timeSpan = dateTime - DateTime.Now;
                return(true);
            }

            if (str.IsMatch(@"^\d+$") && int.TryParse(str, out int second))
            {
                timeSpan = new TimeSpan(0, 0, second);
                return(true);
            }
            if (str.TryGetTimeSpan(out TimeSpan timeSpanFormal, false))
            {
                timeSpan += timeSpanFormal;
                return(true);
            }

            if (TryGetTimeSpanFromStr(str, out TimeSpan timeSpanModern))
            {
                timeSpan += timeSpanModern;
                success   = true;
            }

            if (TryGetTimeSpanFromAncientStr(str, out TimeSpan timeSpanAncient))
            {
                timeSpan += timeSpanAncient;
                success   = true;
            }
            return(success);
        }
Beispiel #2
0
        /// <summary>
        /// 将字符串类型的数字转换为double类型,支持中文以及带单位
        /// </summary>
        /// <param name="str"></param>
        /// <param name="doubleResult"></param>
        /// <param name="kouType"></param>
        /// <returns></returns>
        public static bool TryToDouble(string str, out double doubleResult, bool kouType = true)
        {
            doubleResult = 0;
            if (str.IsNullOrWhiteSpace())
            {
                return(false);
            }
            if (!kouType)
            {
                return(double.TryParse(str, out doubleResult));
            }
            //如果有中文的数字,转为阿拉伯数字
            if (ZhNumber.IsContainZhNumber(str))
            {
                str = ZhNumber.ToArabicNumber(str);
            }
            if (!double.TryParse(str, NumberStyles.Float, null, out doubleResult))
            {
                if (NumberConvertor.WebUnitDouble(str, out string parsedStr))
                {
                    str = parsedStr;
                }

                ExpressionCalculator calculator = new ExpressionCalculator();//BUG 没有处理abc等非表达式情况,仍然会认为输入了正确的表达式
                if (str.Contains("上"))
                {
                    str = str.Replace("上", "");                   //乘上、加上等
                }
                if (str.Contains("去"))
                {
                    str = str.Replace("去", "");
                }
                str = str.ReplaceAllFromPairSet(KouStaticData.ZhMathToSymbolMath);
                var result = calculator.Calculate(str)?.ToString();
                if (result != null && result != double.NaN.ToString(CultureInfo.InvariantCulture))
                {
                    return(double.TryParse(result, out doubleResult));
                }
                return(false);
            }
            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// 使用现代时间计数格式尝试转换为TimeSpan格式的时间间隔
        /// </summary>
        /// <param name="str">支持格式为世纪(century)[c|世纪];年数[y|年|];季[季];月数[M|月];周数[w|周];天数[d|天|日];小时数[h|小时|时];分钟数[m|分];秒数[s|秒];毫秒数(millisecond)[ms|毫秒];</param>
        /// <param name="timeSpan"></param>
        /// <returns></returns>
        public static bool TryGetTimeSpanFromStr(string str, out TimeSpan timeSpan)
        {
            timeSpan = new TimeSpan();
            if (str.IsNullOrWhiteSpace())
            {
                return(false);
            }
            if (ZhNumber.IsContainZhNumber(str))
            {
                str = ZhNumber.ToArabicNumber(str);
            }
            str = str.Replace("个", "");
            List <string> patternList = new List <string>()
            {
                @"\d+(\.\d+)?(c|世纪)",        //0
                @"\d+(\.\d+)?(y|年)",         //1
                @"\d+(\.\d+)?(季)",           //2
                @"\d+(\.\d+)?(M|月)",         //3
                @"\d+(\.\d+)?(w|周)",         //4
                @"\d+(\.\d+)?(d|天|日)",       //5
                @"\d+(\.\d+)?(h|小时|时(?!辰))", //6
                @"\d+(\.\d+)?(m(?!s)|分)",    //7
                @"\d+(\.\d+)?(s|秒)",         //8
                @"\d+(\.\d+)?(ms|毫秒)",       //9
            };
            long day = 0, hour = 0, minute = 0, second = 0, millisecond = 0;
            bool success = false;//指示是否成功转换过一次

            for (int i = 0; i < patternList.Count; i++)
            {
                var timeStr = str.Match(patternList[i]);
                if (timeStr.IsNullOrEmpty() || !double.TryParse(timeStr.Match(@"\d+(\.\d+)?"), out double num))
                {
                    continue;
                }
                success = true;
                switch (i)
                {
                //转化为天
                case 0:                                                               //世纪
                    day += (num *= 36500) > long.MaxValue ? 0 : Convert.ToInt64(num); //Convert.ToInt32可以四舍六入五取偶
                    break;

                case 1:    //年
                    day += (num *= 365) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 2:    //季
                    day += (num *= 91.25) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                //转化为小时
                case 3:    //月
                    hour += (num *= 730) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 4:    //周
                    hour += (num *= 168) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                //转化为分钟
                case 5:    //天
                    minute += (num *= 1440) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                //转化为秒
                case 6:    //时
                    second += (num *= 3600) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 7:    //分
                    second += (num *= 60) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                //转化为毫秒:
                case 8:    //秒
                    millisecond += (num *= 1000) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 9:    //毫秒
                    millisecond += Convert.ToInt64(num);
                    break;

                default:
                    break;
                }
            }
            //继续突破最高限制
            if (millisecond > int.MaxValue)
            {
                second     += (millisecond - int.MaxValue) / 1000;
                millisecond = int.MaxValue;
            }
            if (second > int.MaxValue)
            {
                minute += (second - int.MaxValue) / 60;
                second  = int.MaxValue;
            }
            if (minute > int.MaxValue)
            {
                hour  += (minute - int.MaxValue) / 60;
                minute = int.MaxValue;
            }
            if (hour > int.MaxValue)
            {
                day += (hour - int.MaxValue) / 24;
                hour = int.MaxValue;
            }
            if (!success)
            {
                return(false);
            }
            try
            {
                timeSpan = new TimeSpan((int)day, (int)hour, (int)minute, (int)second, (int)millisecond);
            }
            catch (Exception)
            {
                timeSpan = TimeSpan.MaxValue;
            }
            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// 使用古代格式的字符尝试转换为TimeSpan格式的时间间隔
        /// </summary>
        /// <param name="str">支持格式为 旬10天[旬];候5天[候];须臾48分钟[须臾];昼夜24小时[昼夜];2小时[更|鼓|时辰];30分钟[炷香|顿饭];15分[刻|盏茶];144秒[罗预];7200毫秒[弹指];360毫秒[瞬];18毫秒[念|刹那];</param>
        /// <param name="timeSpan"></param>
        /// <returns></returns>
        public static bool TryGetTimeSpanFromAncientStr(string str, out TimeSpan timeSpan)
        {
            timeSpan = new TimeSpan();
            if (str.IsNullOrWhiteSpace())
            {
                return(false);
            }
            if (ZhNumber.IsContainZhNumber(str))
            {
                str = ZhNumber.ToArabicNumber(str);
            }
            List <string> patternList = new List <string>()
            {
                @"\d+(\.\d+)?(旬)",        //0
                @"\d+(\.\d+)?(候)",        //1
                @"\d+(\.\d+)?(昼夜)",       //2
                @"\d+(\.\d+)?(须臾)",       //3
                @"\d+(\.\d+)?(更|鼓|时辰)",   //4
                @"\d+(\.\d+)?(柱香|炷香|顿饭)", //5
                @"\d+(\.\d+)?(刻|盏茶)",     //6
                @"\d+(\.\d+)?(罗预)",       //7
                @"\d+(\.\d+)?(弹指)",       //8
                @"\d+(\.\d+)?(瞬)",        //9
                @"\d+(\.\d+)?(念|刹那)",     //10
            };
            double day = 0, hour = 0, minute = 0, second = 0, millisecond = 0;
            bool   success = false;//指示是否成功转换过一次

            for (int i = 0; i < patternList.Count; i++)
            {
                var timeStr = str.Match(patternList[i]);
                if (timeStr.IsNullOrEmpty() || !double.TryParse(timeStr.Match(@"\d+(\.\d+)?"), out double num))
                {
                    continue;
                }
                success = true;
                switch (i)
                {
                //转化为天
                case 0:                                                            //旬
                    day += (num *= 10) > long.MaxValue ? 0 : Convert.ToInt64(num); //Convert.ToInt64可以四舍六入五取偶
                    break;

                case 1:    //候
                    day += (num *= 5) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 2:    //昼夜
                    hour += (num *= 24) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                //转化为分钟
                case 3:    //须臾
                    minute += (num *= 48) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 4:    //更|鼓|时辰
                    minute += (num *= 120) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 5:    //炷香|顿饭
                    minute += (num *= 30) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 6:    //刻|盏茶
                    minute += (num *= 15) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                //转化为秒:
                case 7:    //罗预
                    second += (num *= 144) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 8:    //弹指
                    millisecond += (num *= 7200) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 9:    //瞬
                    millisecond += (num *= 360) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                case 10:    //念|刹那
                    millisecond += (num *= 18) > long.MaxValue ? 0 : Convert.ToInt64(num);
                    break;

                default:
                    break;
                }
            }
            if (!success)
            {
                return(false);
            }

            try
            {
                timeSpan = new TimeSpan((int)day, (int)hour, (int)minute, (int)second, (int)millisecond);
            }
            catch (Exception)
            {
                timeSpan = TimeSpan.MaxValue;//10675199.02:48:05.4775807
            }
            return(true);
        }