using System; using System.Collections.Generic; using System.Text; namespace VanWassenhove.Util { /// /// This class contains helper methods for the manipulation of DateTime instances. /// public class DateTimeHelpers { /// /// The base DateTime. /// public static DateTime UnixTimeBase = new DateTime(1970, 1, 1); /// /// Converts a unixtime into a DateTime. /// /// The unixtime to convert. /// An equivalent datetime for the given unixtime. public static DateTime FromUnixTime(double timestamp) { return UnixTimeBase.AddSeconds(timestamp); } /// /// Converts a DateTime to a unixtime. /// /// The datetime to convert. /// An equivalent unixtime for the given datetime. public static double ToUnixTime(DateTime dateTime) { TimeSpan timeSpan = dateTime - UnixTimeBase; return timeSpan.TotalSeconds; } /// /// Returns the first day of the week for a given datetime. /// /// The given datetime. /// The first day of the week. public static DateTime FirstDayOfWeek(DateTime dateTime) { if (dateTime.DayOfWeek == DayOfWeek.Sunday) { dateTime = dateTime.AddDays(-7); } return dateTime.AddDays(-Convert.ToInt32(dateTime.DayOfWeek) + 1); } /// /// Returns the last day of the week for a given datetime. /// /// The given datetime. /// The last day of the week. public static DateTime LastDayOfWeek(DateTime dateTime) { if (dateTime.DayOfWeek == DayOfWeek.Sunday) { dateTime = dateTime.AddDays(-7); } return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day).AddDays(7 - Convert.ToInt32(dateTime.DayOfWeek)); } /// /// Returns the first day of the month of a given datetime. /// /// The given datetime. /// The first day of the month. public static DateTime FirstDayOfMonth(DateTime dateTime) { return new DateTime(dateTime.Year, dateTime.Month, 1); } /// /// Returns the last day of the month for a given datetme. /// /// The given datetime. /// The last day of the month. public static DateTime LastDayOfMonth(DateTime dateTime) { return new DateTime(dateTime.Year, dateTime.Month, DateTime.DaysInMonth(dateTime.Year, dateTime.Month)); } /// /// Returns the interval that exist between end and begin expressed in units of the given spanUnit. /// /// The begin of the interval. /// The end of the interval>. /// The unit in which the interval should be expressed. /// The number of units that exist between end and begin. public static long TimeSpan(DateTime begin, DateTime end, SpanUnit spanUnit) { TimeSpan ts = ts = end - begin; switch (spanUnit) { case SpanUnit.Year: return end.Year - begin.Year; case SpanUnit.Month: return (end.Month - begin.Month) + (12 * (end.Year - begin.Year)); case SpanUnit.Weekday: return Fix(ts.TotalDays) / 7; case SpanUnit.Day: return Fix(ts.TotalDays); case SpanUnit.Hour: return Fix(ts.TotalHours); case SpanUnit.Minute: return Fix(ts.TotalMinutes); default: return Fix(ts.TotalSeconds); } } private static long Fix(double number) { if (number >= 0) { return (long)Math.Floor(number); } return (long)Math.Ceiling(number); } } /// /// This enum represents the Unit in which a TimeSpan (interval) number is expressed. /// public enum SpanUnit { Year, Month, Weekday, Day, Hour, Minute, Second } }