#if FRAMEWORK20
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Globalization;
namespace DevComponents.DotNetBar.Schedule
{
internal static class DateHelper
{
#region Date/Time Helpers
///
/// Returns the date that is 30 minutes before or after input date if input date minute is 0 or 30. Otherwise it returns next increment to 0 or 30.
///
/// Date and time.
/// Indicates whether to add or subtract minutes.
/// New date time.
public static DateTime GetNext30Minute(DateTime date, bool forward)
{
if (date.Minute == 0 || date.Minute == 30)
return (date.AddMinutes(forward ? 30 : -30));
return (date.AddMinutes((forward ? 1 : -1) * Math.Abs(30 - date.Minute)));
}
///
/// Returns date that starts with the day. If passed date is not on the requested date function returns first date with day that is before passed date.
///
/// Date to inspect.
/// Day of week
/// Date that starts on given day of week.
public static DateTime GetDateForDayOfWeek(DateTime date, DayOfWeek dayOfWeek)
{
if (date.DayOfWeek != dayOfWeek)
{
// Go back to the first day of week
while (date.DayOfWeek != dayOfWeek)
{
date = date.AddDays(-1);
}
}
return (date);
}
public static DateTime GetEndOfWeek(DateTime date, DayOfWeek lastDayOfWeek)
{
while (date.DayOfWeek != lastDayOfWeek)
date = date.AddDays(1);
return (date);
}
internal static DayOfWeek GetFirstDayOfWeek()
{
return (ScheduleSettings.GetActiveCulture().DateTimeFormat.FirstDayOfWeek);
}
internal static DayOfWeek GetLastDayOfWeek()
{
DayOfWeek firstDay = GetFirstDayOfWeek();
int lastDay = (int)firstDay + 6;
if (lastDay > 6)
lastDay = lastDay - 7;
return (DayOfWeek)lastDay;
}
///
/// Returns whether two days fall on same month and year.
///
/// First date
/// Second date
/// true if dates are on same month and year
public static bool IsSameMonth(DateTime date1, DateTime date2)
{
return (date1.Year == date2.Year && date1.Month == date2.Month);
}
public static DayOfWeek GetNextDay(DayOfWeek currentDay)
{
if ((int)currentDay == 6)
return (DayOfWeek)0;
return (DayOfWeek)(currentDay + 1);
}
///
/// Returns true if time periods overlap.
///
/// Start of first period.
/// End of first period.
/// Start of second period.
/// End of second period.
/// true if periods overlap
public static bool TimePeriodsOverlap(DateTime startTime1, DateTime endTime1, DateTime startTime2, DateTime endTime2)
{
return (startTime1 <= startTime2 && endTime1 > startTime2 ||
startTime1 >= startTime2 && startTime1 < endTime2);
}
public static int GetNumberOfWeeks(DateTime startDate, DateTime endDate)
{
if (startDate == DateTime.MinValue && endDate == DateTime.MinValue)
{
startDate = DateTime.Today.Date;
endDate = startDate.AddMonths(1);
}
if (startDate == DateTime.MinValue || endDate == DateTime.MinValue || endDate < startDate)
{
return (0);
}
endDate = GetEndOfWeek(endDate, DateHelper.GetLastDayOfWeek());
startDate = GetDateForDayOfWeek(startDate, DateHelper.GetFirstDayOfWeek());
return ((int)Math.Max(Math.Ceiling(endDate.Subtract(startDate).TotalDays / 7), 1));
}
///
/// Gets the abbreviated month name for
/// the given date
///
/// Date
/// Abbreviated name
public static string GetThreeLetterMonth(DateTime date)
{
CultureInfo ci = ScheduleSettings.GetActiveCulture();
return (ci.DateTimeFormat.GetAbbreviatedMonthName(date.Month));
}
///
/// Gets the abbreviated day name for
/// the given date
///
/// Day of week
/// Abbreviated name
public static string GetThreeLetterDayOfWeek(DayOfWeek dayOfWeek)
{
CultureInfo ci = ScheduleSettings.GetActiveCulture();
return (ci.DateTimeFormat.GetAbbreviatedDayName(dayOfWeek));
}
#endregion
}
}
#endif