diff --git a/X10D/src/DateTimeExtensions.cs b/X10D/src/DateTimeExtensions.cs deleted file mode 100644 index 871a42e..0000000 --- a/X10D/src/DateTimeExtensions.cs +++ /dev/null @@ -1,124 +0,0 @@ -using System; - -namespace X10D -{ - /// - /// Extension methods for . - /// - public static class DateTimeExtensions - { - /// - /// Returns a rounded integer of the number of years since a given date as of today. - /// - /// The date from which to start. - /// Returns the number of years since as of today. - public static int Age(this DateTime date) - { - return date.Age(DateTime.Today); - } - - /// - /// Returns a rounded integer of the number of years since a given date as of another given date. - /// - /// The date from which to start. - /// The date at which to stop counting. - /// - /// Returns the integer number of years since as of - /// . - /// - public static int Age(this DateTime date, DateTime asOf) - { - return (int)(((asOf.Date - TimeSpan.FromDays(1) - date.Date).TotalDays + 1) / 365.2425); - } - - /// - /// Gets a DateTime representing the first occurence of a specified day in the current month. - /// - /// The current day. - /// The current day of week. - /// Returns a date representing the first occurence of . - public static DateTime First(this DateTime current, DayOfWeek dayOfWeek) - { - var first = current.FirstDayOfMonth(); - - if (first.DayOfWeek != dayOfWeek) - { - first = first.Next(dayOfWeek); - } - - return first; - } - - /// - /// Gets a representing the first day in the current month. - /// - /// The current date. - /// Returns a date representing the first day of the month>. - public static DateTime FirstDayOfMonth(this DateTime current) - { - return current.AddDays(1 - current.Day); - } - - /// - /// Gets a representing the last specified day in the current month. - /// - /// The current date. - /// The current day of week. - /// Returns a date representing the final occurence of . - public static DateTime Last(this DateTime current, DayOfWeek dayOfWeek) - { - var last = current.LastDayOfMonth(); - var lastDayOfWeek = last.DayOfWeek; - - var diff = dayOfWeek - lastDayOfWeek; - var offset = diff > 0 ? diff - 7 : diff; - - return last.AddDays(offset); - } - - /// - /// Gets a representing the last day in the current month. - /// - /// The current date. - /// Returns a date representing the last day of the month>. - public static DateTime LastDayOfMonth(this DateTime current) - { - var daysInMonth = DateTime.DaysInMonth(current.Year, current.Month); - return new DateTime(current.Year, current.Month, daysInMonth); - } - - /// - /// Gets a representing the first date following the current date which falls on the - /// given day of the week. - /// - /// The current date. - /// The day of week for the next date to get. - /// Returns a date representing the next occurence of . - public static DateTime Next(this DateTime current, DayOfWeek dayOfWeek) - { - var offsetDays = dayOfWeek - current.DayOfWeek; - - if (offsetDays <= 0) - { - offsetDays += 7; - } - - return current.AddDays(offsetDays); - } - - /// - /// Converts the to a Unix timestamp. - /// - /// The instance. - /// - /// Optional. Whether or not the return value should be represented as milliseconds. - /// Defaults to . - /// - /// Returns a Unix timestamp representing the provided . - public static long ToUnixTimeStamp(this DateTime time, bool useMillis = false) - { - DateTimeOffset offset = time; - return useMillis ? offset.ToUnixTimeMilliseconds() : offset.ToUnixTimeSeconds(); - } - } -} diff --git a/X10D/src/DateTimeExtensions/DateTimeExtensions.cs b/X10D/src/DateTimeExtensions/DateTimeExtensions.cs new file mode 100644 index 0000000..0c26c34 --- /dev/null +++ b/X10D/src/DateTimeExtensions/DateTimeExtensions.cs @@ -0,0 +1,76 @@ +using System; + +namespace X10D.DateTimeExtensions +{ + /// + /// Extension methods for . + /// + public static class DateTimeExtensions + { + /// + public static int Age(this DateTime value) + { + return value.Age(DateTime.Today); + } + + /// + public static int Age(this DateTime value, DateTime asOf) + { + return ((DateTimeOffset)value).Age(asOf); + } + + /// + /// A representing the first occurence of . + public static DateTime First(this DateTime value, DayOfWeek dayOfWeek) + { + return ((DateTimeOffset)value).First(dayOfWeek).DateTime; + } + + /// + /// A representing the first day of the current month. + public static DateTime FirstDayOfMonth(this DateTime value) + { + return ((DateTimeOffset)value).FirstDayOfMonth().DateTime; + } + + /// + /// A representing the final occurence of . + public static DateTime Last(this DateTime value, DayOfWeek dayOfWeek) + { + return ((DateTimeOffset)value).Last(dayOfWeek).DateTime; + } + /// + /// A representing the last day of the current month. + public static DateTime LastDayOfMonth(this DateTime value) + { + return ((DateTimeOffset)value).LastDayOfMonth().DateTime; + } + + /// + /// A representing the next occurence of . + public static DateTime Next(this DateTime value, DayOfWeek dayOfWeek) + { + return ((DateTimeOffset)value).Next(dayOfWeek).DateTime; + } + + /// + /// Returns the number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z. + /// + /// The current date. + /// The number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z. + public static long ToUnixTimeMilliseconds(this DateTime value) + { + return ((DateTimeOffset)value).ToUnixTimeMilliseconds(); + } + + /// + /// Returns the number of seconds that have elapsed since 1970-01-01T00:00:00.000Z. + /// + /// The current date. + /// The number of seconds that have elapsed since 1970-01-01T00:00:00.000Z. + public static long ToUnixTimeSeconds(this DateTime value) + { + return ((DateTimeOffset)value).ToUnixTimeSeconds(); + } + } +} diff --git a/X10D/src/DateTimeExtensions/DateTimeOffsetExtensions.cs b/X10D/src/DateTimeExtensions/DateTimeOffsetExtensions.cs new file mode 100644 index 0000000..92e9f32 --- /dev/null +++ b/X10D/src/DateTimeExtensions/DateTimeOffsetExtensions.cs @@ -0,0 +1,108 @@ +using System; + +namespace X10D.DateTimeExtensions +{ + /// + /// Extension methods for . + /// + public static class DateTimeOffsetExtensions + { + /// + /// Returns the rounded-down integer number of years since a given date as of today. + /// + /// The date from which to calculate. + /// The rounded-down integer number of years since as of today. + public static int Age(this DateTimeOffset value) + { + return value.Age(DateTime.Today); + } + + /// + /// Returns the rounded-down integer number of years since a given date as of another specified date. + /// + /// The date from which to calculate. + /// The date at which to stop calculating. + /// + /// The rounded-down integer number of years since as of the date specified by + /// . + /// + public static int Age(this DateTimeOffset value, DateTimeOffset asOf) + { + return (int)(((asOf.Date - TimeSpan.FromDays(1) - value.Date).TotalDays + 1) / 365.2425); + } + + /// + /// Gets a date representing the first occurence of a specified day of the week in the current month. + /// + /// The current date. + /// The day of the week. + /// A representing the first occurence of . + public static DateTimeOffset First(this DateTimeOffset value, DayOfWeek dayOfWeek) + { + var first = value.FirstDayOfMonth(); + + if (first.DayOfWeek != dayOfWeek) + { + first = first.Next(dayOfWeek); + } + + return first; + } + + /// + /// Gets a date representing the first day of the current month. + /// + /// The current date. + /// A representing the first day of the current month. + public static DateTimeOffset FirstDayOfMonth(this DateTimeOffset value) + { + return value.AddDays(1 - value.Day); + } + + /// + /// Gets a date representing the final occurence of a specified day of the week in the current month. + /// + /// The current date. + /// The day of the week. + /// A representing the final occurence of . + public static DateTimeOffset Last(this DateTimeOffset value, DayOfWeek dayOfWeek) + { + var last = value.LastDayOfMonth(); + var lastDayOfWeek = last.DayOfWeek; + + var diff = dayOfWeek - lastDayOfWeek; + var offset = diff > 0 ? diff - 7 : diff; + + return last.AddDays(offset); + } + + /// + /// Gets a date representing the last day of the current month. + /// + /// The current date. + /// A representing the last day of the current month. + public static DateTimeOffset LastDayOfMonth(this DateTimeOffset value) + { + var daysInMonth = DateTime.DaysInMonth(value.Year, value.Month); + return new DateTime(value.Year, value.Month, daysInMonth); + } + + /// + /// Gets a date representing the next occurence of a specified day of the week in the current month. + /// + /// The current date. + /// The day of the week. + /// A representing the next occurence of . + public static DateTimeOffset Next(this DateTimeOffset value, DayOfWeek dayOfWeek) + { + var offsetDays = dayOfWeek - value.DayOfWeek; + + if (offsetDays <= 0) + { + offsetDays += 7; + } + + return value.AddDays(offsetDays); + } + } +}