mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 19:28:48 +00:00
✨ Add DateTimeOffsetExtensions
Supersedes DateTimeExtensions. Resolves #26
This commit is contained in:
parent
31e4574732
commit
31d1dc21df
@ -1,124 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace X10D
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="DateTime" />.
|
||||
/// </summary>
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a rounded integer of the number of years since a given date as of today.
|
||||
/// </summary>
|
||||
/// <param name="date">The date from which to start.</param>
|
||||
/// <returns>Returns the number of years since <paramref name="date" /> as of today.</returns>
|
||||
public static int Age(this DateTime date)
|
||||
{
|
||||
return date.Age(DateTime.Today);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a rounded integer of the number of years since a given date as of another given date.
|
||||
/// </summary>
|
||||
/// <param name="date">The date from which to start.</param>
|
||||
/// <param name="asOf">The date at which to stop counting.</param>
|
||||
/// <returns>
|
||||
/// Returns the integer number of years since <paramref name="date" /> as of
|
||||
/// <paramref name="asOf" />.
|
||||
/// </returns>
|
||||
public static int Age(this DateTime date, DateTime asOf)
|
||||
{
|
||||
return (int)(((asOf.Date - TimeSpan.FromDays(1) - date.Date).TotalDays + 1) / 365.2425);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a DateTime representing the first occurence of a specified day in the current month.
|
||||
/// </summary>
|
||||
/// <param name="current">The current day.</param>
|
||||
/// <param name="dayOfWeek">The current day of week.</param>
|
||||
/// <returns>Returns a date representing the first occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTime First(this DateTime current, DayOfWeek dayOfWeek)
|
||||
{
|
||||
var first = current.FirstDayOfMonth();
|
||||
|
||||
if (first.DayOfWeek != dayOfWeek)
|
||||
{
|
||||
first = first.Next(dayOfWeek);
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DateTime" /> representing the first day in the current month.
|
||||
/// </summary>
|
||||
/// <param name="current">The current date.</param>
|
||||
/// <returns>Returns a date representing the first day of the month>.</returns>
|
||||
public static DateTime FirstDayOfMonth(this DateTime current)
|
||||
{
|
||||
return current.AddDays(1 - current.Day);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DateTime" /> representing the last specified day in the current month.
|
||||
/// </summary>
|
||||
/// <param name="current">The current date.</param>
|
||||
/// <param name="dayOfWeek">The current day of week.</param>
|
||||
/// <returns>Returns a date representing the final occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DateTime" /> representing the last day in the current month.
|
||||
/// </summary>
|
||||
/// <param name="current">The current date.</param>
|
||||
/// <returns>Returns a date representing the last day of the month>.</returns>
|
||||
public static DateTime LastDayOfMonth(this DateTime current)
|
||||
{
|
||||
var daysInMonth = DateTime.DaysInMonth(current.Year, current.Month);
|
||||
return new DateTime(current.Year, current.Month, daysInMonth);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DateTime" /> representing the first date following the current date which falls on the
|
||||
/// given day of the week.
|
||||
/// </summary>
|
||||
/// <param name="current">The current date.</param>
|
||||
/// <param name="dayOfWeek">The day of week for the next date to get.</param>
|
||||
/// <returns>Returns a date representing the next occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTime Next(this DateTime current, DayOfWeek dayOfWeek)
|
||||
{
|
||||
var offsetDays = dayOfWeek - current.DayOfWeek;
|
||||
|
||||
if (offsetDays <= 0)
|
||||
{
|
||||
offsetDays += 7;
|
||||
}
|
||||
|
||||
return current.AddDays(offsetDays);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="DateTime" /> to a Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="time">The <see cref="DateTime" /> instance.</param>
|
||||
/// <param name="useMillis">
|
||||
/// Optional. Whether or not the return value should be represented as milliseconds.
|
||||
/// Defaults to <see langword="false" />.
|
||||
/// </param>
|
||||
/// <returns>Returns a Unix timestamp representing the provided <see cref="DateTime" />.</returns>
|
||||
public static long ToUnixTimeStamp(this DateTime time, bool useMillis = false)
|
||||
{
|
||||
DateTimeOffset offset = time;
|
||||
return useMillis ? offset.ToUnixTimeMilliseconds() : offset.ToUnixTimeSeconds();
|
||||
}
|
||||
}
|
||||
}
|
76
X10D/src/DateTimeExtensions/DateTimeExtensions.cs
Normal file
76
X10D/src/DateTimeExtensions/DateTimeExtensions.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using System;
|
||||
|
||||
namespace X10D.DateTimeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="DateTime" />.
|
||||
/// </summary>
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
/// <inheritdoc cref="DateTimeOffsetExtensions.Age(DateTimeOffset)" />
|
||||
public static int Age(this DateTime value)
|
||||
{
|
||||
return value.Age(DateTime.Today);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="DateTimeOffsetExtensions.Age(DateTimeOffset, DateTimeOffset)" />
|
||||
public static int Age(this DateTime value, DateTime asOf)
|
||||
{
|
||||
return ((DateTimeOffset)value).Age(asOf);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="DateTimeOffsetExtensions.First(DateTimeOffset, DayOfWeek)" />
|
||||
/// <returns>A <see cref="DateTime" /> representing the first occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTime First(this DateTime value, DayOfWeek dayOfWeek)
|
||||
{
|
||||
return ((DateTimeOffset)value).First(dayOfWeek).DateTime;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="DateTimeOffsetExtensions.FirstDayOfMonth(DateTimeOffset)" />
|
||||
/// <returns>A <see cref="DateTime" /> representing the first day of the current month.</returns>
|
||||
public static DateTime FirstDayOfMonth(this DateTime value)
|
||||
{
|
||||
return ((DateTimeOffset)value).FirstDayOfMonth().DateTime;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="DateTimeOffsetExtensions.Last(DateTimeOffset, DayOfWeek)" />
|
||||
/// <returns>A <see cref="DateTimeOffset" /> representing the final occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTime Last(this DateTime value, DayOfWeek dayOfWeek)
|
||||
{
|
||||
return ((DateTimeOffset)value).Last(dayOfWeek).DateTime;
|
||||
}
|
||||
/// <inheritdoc cref="DateTimeOffsetExtensions.LastDayOfMonth(DateTimeOffset)" />
|
||||
/// <returns>A <see cref="DateTimeOffset" /> representing the last day of the current month.</returns>
|
||||
public static DateTime LastDayOfMonth(this DateTime value)
|
||||
{
|
||||
return ((DateTimeOffset)value).LastDayOfMonth().DateTime;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="DateTimeOffsetExtensions.Next(DateTimeOffset, DayOfWeek)" />
|
||||
/// <returns>A <see cref="DateTimeOffset" /> representing the next occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTime Next(this DateTime value, DayOfWeek dayOfWeek)
|
||||
{
|
||||
return ((DateTimeOffset)value).Next(dayOfWeek).DateTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z.
|
||||
/// </summary>
|
||||
/// <param name="value">The current date.</param>
|
||||
/// <returns>The number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z.</returns>
|
||||
public static long ToUnixTimeMilliseconds(this DateTime value)
|
||||
{
|
||||
return ((DateTimeOffset)value).ToUnixTimeMilliseconds();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of seconds that have elapsed since 1970-01-01T00:00:00.000Z.
|
||||
/// </summary>
|
||||
/// <param name="value">The current date.</param>
|
||||
/// <returns>The number of seconds that have elapsed since 1970-01-01T00:00:00.000Z.</returns>
|
||||
public static long ToUnixTimeSeconds(this DateTime value)
|
||||
{
|
||||
return ((DateTimeOffset)value).ToUnixTimeSeconds();
|
||||
}
|
||||
}
|
||||
}
|
108
X10D/src/DateTimeExtensions/DateTimeOffsetExtensions.cs
Normal file
108
X10D/src/DateTimeExtensions/DateTimeOffsetExtensions.cs
Normal file
@ -0,0 +1,108 @@
|
||||
using System;
|
||||
|
||||
namespace X10D.DateTimeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="DateTimeOffset" />.
|
||||
/// </summary>
|
||||
public static class DateTimeOffsetExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the rounded-down integer number of years since a given date as of today.
|
||||
/// </summary>
|
||||
/// <param name="value">The date from which to calculate.</param>
|
||||
/// <returns>The rounded-down integer number of years since <paramref name="value" /> as of today.</returns>
|
||||
public static int Age(this DateTimeOffset value)
|
||||
{
|
||||
return value.Age(DateTime.Today);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the rounded-down integer number of years since a given date as of another specified date.
|
||||
/// </summary>
|
||||
/// <param name="value">The date from which to calculate.</param>
|
||||
/// <param name="asOf">The date at which to stop calculating.</param>
|
||||
/// <returns>
|
||||
/// The rounded-down integer number of years since <paramref name="value" /> as of the date specified by
|
||||
/// <paramref name="asOf" />.
|
||||
/// </returns>
|
||||
public static int Age(this DateTimeOffset value, DateTimeOffset asOf)
|
||||
{
|
||||
return (int)(((asOf.Date - TimeSpan.FromDays(1) - value.Date).TotalDays + 1) / 365.2425);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a date representing the first occurence of a specified day of the week in the current month.
|
||||
/// </summary>
|
||||
/// <param name="value">The current date.</param>
|
||||
/// <param name="dayOfWeek">The day of the week.</param>
|
||||
/// <returns>A <see cref="DateTimeOffset" /> representing the first occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTimeOffset First(this DateTimeOffset value, DayOfWeek dayOfWeek)
|
||||
{
|
||||
var first = value.FirstDayOfMonth();
|
||||
|
||||
if (first.DayOfWeek != dayOfWeek)
|
||||
{
|
||||
first = first.Next(dayOfWeek);
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a date representing the first day of the current month.
|
||||
/// </summary>
|
||||
/// <param name="value">The current date.</param>
|
||||
/// <returns>A <see cref="DateTimeOffset" /> representing the first day of the current month.</returns>
|
||||
public static DateTimeOffset FirstDayOfMonth(this DateTimeOffset value)
|
||||
{
|
||||
return value.AddDays(1 - value.Day);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a date representing the final occurence of a specified day of the week in the current month.
|
||||
/// </summary>
|
||||
/// <param name="value">The current date.</param>
|
||||
/// <param name="dayOfWeek">The day of the week.</param>
|
||||
/// <returns>A <see cref="DateTimeOffset" /> representing the final occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a date representing the last day of the current month.
|
||||
/// </summary>
|
||||
/// <param name="value">The current date.</param>
|
||||
/// <returns>A <see cref="DateTimeOffset" /> representing the last day of the current month.</returns>
|
||||
public static DateTimeOffset LastDayOfMonth(this DateTimeOffset value)
|
||||
{
|
||||
var daysInMonth = DateTime.DaysInMonth(value.Year, value.Month);
|
||||
return new DateTime(value.Year, value.Month, daysInMonth);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a date representing the next occurence of a specified day of the week in the current month.
|
||||
/// </summary>
|
||||
/// <param name="value">The current date.</param>
|
||||
/// <param name="dayOfWeek">The day of the week.</param>
|
||||
/// <returns>A <see cref="DateTimeOffset" /> representing the next occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTimeOffset Next(this DateTimeOffset value, DayOfWeek dayOfWeek)
|
||||
{
|
||||
var offsetDays = dayOfWeek - value.DayOfWeek;
|
||||
|
||||
if (offsetDays <= 0)
|
||||
{
|
||||
offsetDays += 7;
|
||||
}
|
||||
|
||||
return value.AddDays(offsetDays);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user