コード例 #1
0
        public override TableTemplateTime GetTableObject()
        {
            var templateTime = new TableTemplateTime();

            templateTime.template_time_id   = TemplateTimeId;
            templateTime.template_id        = TemplateId;
            templateTime.template_time_name = TemplateTimeName;
            templateTime.begin_time         = BeginTime;
            templateTime.end_time           = EndTime;
            templateTime.time_span          = TimeSpan;
            templateTime.time_span_unit     = TimeSpanUnit.ToString();
            templateTime.template_time_type = TemplateTimeType.ToString();
            templateTime.search_direction   = SearchDirection.ToString();
            templateTime.handle_reduplicate = HandleReduplicate.ToString();
            return(templateTime);
        }
コード例 #2
0
        private static object Calc(object left, object right, TemplateTimeType dateType)
        {
            if (!IsDateTimeOrInt(left) || !IsDateTimeOrInt(right))
            {
                throw new ApplicationException("Calc exception, type error.");
            }

            if (left is DateTime && right is DateTime)
            {
                throw new ApplicationException("Calc exception, can't add DateTime with DateTime.");
            }
            else if (left is DateTime && right is int)
            {
                switch (dateType)
                {
                case TemplateTimeType.TradingDay:
                    return(((DateTime)left).AddTradingDays((int)right));

                case TemplateTimeType.WorkingDay:
                    return(((DateTime)left).AddWorkingDays((int)right));

                case TemplateTimeType.NaturalDay:
                    return(((DateTime)left).AddDays((int)right));
                }
            }
            else if (left is int && right is DateTime)
            {
                switch (dateType)
                {
                case TemplateTimeType.TradingDay:
                    return(((DateTime)right).AddTradingDays((int)left));

                case TemplateTimeType.WorkingDay:
                    return(((DateTime)right).AddWorkingDays((int)left));

                case TemplateTimeType.NaturalDay:
                    return(((DateTime)right).AddDays((int)left));
                }
            }
            else if (left is int && right is int)
            {
                return((int)left + (int)right);
            }

            return(null);
        }
コード例 #3
0
        /// <summary>
        /// 将 T+n、L-n格式的文本转换为时间
        /// </summary>
        /// <param name="text">原始文本</param>
        /// <param name="timeDictionary">T、L对应的时间</param>
        /// <returns>转换结果</returns>
        public static DateTime ParseDateSyntax(string text, Dictionary <string, DateTime> timeDictionary)
        {
            var errMsg = "时间识别错误[" + text + "]";

            DateTime temp;

            if (DateTime.TryParse(text, out temp))
            {
                return(temp);
            }

            // 解析 T、L
            if (!text.Contains('+') && !text.Contains('-'))
            {
                var key = text.Trim();
                if (!timeDictionary.ContainsKey(key))
                {
                    throw new ApplicationException(errMsg);
                }

                return(timeDictionary[key]);
            }

            var items = text.Split(new[] { '+', '-' }, StringSplitOptions.RemoveEmptyEntries).ToList()
                        .ConvertAll(item => item.Trim()).ToList();

            var chars = text.ToCharArray().ToList().Where(c => c == '+' || c == '-').ToList();

            object result = null;

            for (int i = 0; i < items.Count; ++i)
            {
                object value = items[i];

                //数值
                int v;
                var isInt = int.TryParse((string)value, out v);
                if (isInt)
                {
                    value = v;
                }
                else
                {
                    //键值
                    var isKey = timeDictionary.ContainsKey(items[i]);
                    if (isKey)
                    {
                        value = timeDictionary[items[i]];
                    }
                    else
                    {
                        var content = (string)value;
                        if (content.Contains(".NaturalDay"))
                        {
                            content = content.Replace(".NaturalDay", string.Empty);
                            if (timeDictionary.ContainsKey(content))
                            {
                                value = timeDictionary[content].Day;
                            }
                        }
                    }
                }

                if (i == 0)
                {
                    result = value;
                }
                else
                {
                    bool isAdd = (chars[i - 1] == '+');

                    TemplateTimeType timeType = TemplateTimeType.NaturalDay;
                    if (isInt)
                    {
                        timeType = TemplateTimeType.TradingDay;
                    }
                    else if (value.ToString().Contains("workingDay"))
                    {
                        timeType = TemplateTimeType.WorkingDay;
                        value    = int.Parse(value.ToString().Replace("workingDay", ""));
                    }
                    else if (value.ToString().Contains("day"))
                    {
                        timeType = TemplateTimeType.NaturalDay;
                        value    = int.Parse(value.ToString().Replace("day", ""));
                    }

                    result = Calc(result, (isAdd ? value : 0 - (int)value), timeType);
                }
            }

            return((DateTime)result);
        }
コード例 #4
0
        public static List <DateTime> GenerateDateList(DateTime first, DateTime endDate,
                                                       int timeSpan, TimeSpanUnit timeSpanUnit, TemplateTimeType dateType, bool isForward, bool ignoreReduplicateDays)
        {
            if (timeSpan < 1)
            {
                throw new ApplicationException("Time span can't be 0");
            }

            var dateList = new List <DateTime>();

            DateTime date = first;

            for (int i = 0; ; ++i)
            {
                //避免每个月日期数不同导致的的向下取整问题
                //e.g.
                //(20160131).AddMonths(1).AddMonths(1) = 20160329
                //(20160131).AddMonths(2) = 20160331
                var interval = timeSpan * i;
                switch (timeSpanUnit)
                {
                case TimeSpanUnit.Year:
                    date = first.AddYears(interval);
                    break;

                case TimeSpanUnit.Month:
                    date = first.AddMonths(interval);
                    break;

                case TimeSpanUnit.Day:
                    date = first.AddDays(interval);
                    break;
                }

                if (dateType == TemplateTimeType.TradingDay && !CalendarCache.IsTradingDay(date))
                {
                    date = isForward ? GetPreviousTradingDay(date) : GetNextTradingDay(date);
                }
                else if (dateType == TemplateTimeType.WorkingDay && !CalendarCache.IsWorkingDay(date))
                {
                    date = isForward ? GetPreviousWorkingDay(date) : GetNextWorkingDay(date);
                }

                if (date > endDate)
                {
                    break;
                }

                bool isIgnore = false;
                isIgnore = ignoreReduplicateDays && dateList.Contains(date);
                if (!isIgnore)
                {
                    dateList.Add(date);
                }
            }

            return(dateList);
        }