ComponentFactory.Krypton.Toolkit.ViewDrawMonthDays.DayNearPoint C# (CSharp) Method

DayNearPoint() public method

Gets the button for the day that is nearest (date wise) to the point provided.
public DayNearPoint ( Point pt ) : System.DateTime
pt Point Point to lookup.
return System.DateTime
        public DateTime DayNearPoint(Point pt)
        {
            DateTime retDate = _month;

            // Search for an exact match
            DateTime? day = DayFromPoint(pt, true);
            if (day.HasValue)
                retDate = day.Value;
            else
            {
                // If the mouse is above area then return first day of the month
                if (pt.Y > ClientLocation.Y)
                {
                    if (pt.Y > ClientRectangle.Bottom)
                        retDate = _month.AddMonths(1).AddDays(-1);
                    else
                    {
                        // Find the row the mouse is within
                        for (int row = 0; row < WEEKS; row++)
                            if (pt.Y < _dayRects[row * WEEKDAYS].Bottom)
                            {
                                DateTime startRowDate = _firstDay.AddDays(row * WEEKDAYS);

                                if (pt.X < ClientLocation.X)
                                    retDate = startRowDate;
                                else if (pt.X >= ClientRectangle.Right)
                                    retDate = startRowDate.AddDays(WEEKDAYS - 1);
                                else
                                {
                                    int offsetDays = (pt.X - ClientLocation.X) / _dayRects[row * WEEKDAYS].Width;
                                    retDate = startRowDate.AddDays(offsetDays);
                                }

                                break;
                            }
                    }
                }
            }

            if (retDate > _lastDay)
                return _lastDay;

            if (retDate < _month)
                return _month;

            return retDate;
        }