Example #1
0
        /// <summary>
        /// Function: Return the business day closest to a given date.
        /// </summary>
        /// <param name="date">Reference date.
        /// </param>
        /// <param name="type">Type of search.
        /// </param>
        public BusinessDay GetClosestBusinessDay(DateTime date, TimeSeries.DateSearchType type)
        {
            BusinessDay res = GetBusinessDay(date);

            if (res != null)
            {
                BusinessDay ret = new BusinessDay(res.DateTime, res.DayMonth, res.DayYear, res.DayIndex, res.CalendarID);
                return(ret);
            }
            if (type == TimeSeries.DateSearchType.Previous)
            {
                DateTime firstDate = _dateTimes[0];
                DateTime lastDate  = _dateTimes[_dateTimes.Count - 1];

                if (date >= lastDate)
                {
                    BusinessDay temp = GetBusinessDay(lastDate);
                    BusinessDay bday = new BusinessDay(temp.DateTime, temp.DayMonth, temp.DayYear, temp.DayIndex, temp.CalendarID);

                    res.SetTime(date.TimeOfDay);

                    return(res);
                }

                for (DateTime d = date; d >= firstDate; d = d.AddDays(-1))
                {
                    res = GetBusinessDay(d);
                    if (res != null)
                    {
                        BusinessDay bday = new BusinessDay(res.DateTime, res.DayMonth, res.DayYear, res.DayIndex, res.CalendarID);

                        bday.SetTime(date.TimeOfDay);

                        return(bday);
                    }
                }
            }
            else
            {
                DateTime lastDate = _dateTimes[_dateTimes.Count - 1];

                for (DateTime d = date; d <= lastDate; d = d.AddDays(1))
                {
                    res = GetBusinessDay(d);
                    if (res != null)
                    {
                        BusinessDay bday = new BusinessDay(res.DateTime, res.DayMonth, res.DayYear, res.DayIndex, res.CalendarID);

                        bday.SetTime(date.TimeOfDay);

                        return(bday);
                    }
                }
            }

            return(null);
        }
Example #2
0
        /// <summary>
        /// Function: Return the business day representing a given DateTime.
        /// </summary>
        /// <param name="date">Date to be translated to a business day.
        /// </param>
        public BusinessDay GetBusinessDay(DateTime date)
        {
            if (_dateTimeDictionary.ContainsKey(date.Date))
            {
                BusinessDay temp = _dateTimeDictionary[date.Date];
                BusinessDay bday = new BusinessDay(temp.DateTime, temp.DayMonth, temp.DayYear, temp.DayIndex, temp.CalendarID);

                //bday.SetTime(date.Hour, date.Minute, date.Second, date.Millisecond);
                bday.SetTime(date.TimeOfDay);

                return(bday);// _dateTimeDictionary[date.Date].SetTime(date.Hour, date.Minute, date.Second, date.Millisecond);
            }
            else
            {
                return(null);
            }
        }