From a1722e4374ea6dacac8c94b0c725b386979644c8 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 21 Apr 2022 20:54:17 +0100 Subject: [PATCH] Add time-related extension methods --- X10D/src/Time/ByteExtensions.cs | 87 +++++++++++++++++ X10D/src/Time/DecimalExtensions.cs | 77 +++++++++++++++ X10D/src/Time/DoubleExtensions.cs | 77 +++++++++++++++ X10D/src/Time/HalfExtensions.cs | 77 +++++++++++++++ X10D/src/Time/Int16Extensions.cs | 87 +++++++++++++++++ X10D/src/Time/Int32Extensions.cs | 87 +++++++++++++++++ X10D/src/Time/Int64Extensions.cs | 87 +++++++++++++++++ X10D/src/Time/SByteExtensions.cs | 88 +++++++++++++++++ X10D/src/Time/SingleExtensions.cs | 77 +++++++++++++++ X10D/src/Time/TimeSpanExtensions.cs | 31 ++++++ X10D/src/Time/UInt16Extensions.cs | 88 +++++++++++++++++ X10D/src/Time/UInt32Extensions.cs | 88 +++++++++++++++++ X10D/src/Time/UInt64Extensions.cs | 143 ++++++++++++++++++++++++++++ 13 files changed, 1094 insertions(+) create mode 100644 X10D/src/Time/ByteExtensions.cs create mode 100644 X10D/src/Time/DecimalExtensions.cs create mode 100644 X10D/src/Time/DoubleExtensions.cs create mode 100644 X10D/src/Time/HalfExtensions.cs create mode 100644 X10D/src/Time/Int16Extensions.cs create mode 100644 X10D/src/Time/Int32Extensions.cs create mode 100644 X10D/src/Time/Int64Extensions.cs create mode 100644 X10D/src/Time/SByteExtensions.cs create mode 100644 X10D/src/Time/SingleExtensions.cs create mode 100644 X10D/src/Time/TimeSpanExtensions.cs create mode 100644 X10D/src/Time/UInt16Extensions.cs create mode 100644 X10D/src/Time/UInt32Extensions.cs create mode 100644 X10D/src/Time/UInt64Extensions.cs diff --git a/X10D/src/Time/ByteExtensions.cs b/X10D/src/Time/ByteExtensions.cs new file mode 100644 index 0000000..0a50aa2 --- /dev/null +++ b/X10D/src/Time/ByteExtensions.cs @@ -0,0 +1,87 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +public static class ByteExtensions +{ + /// + /// Returns a that represents this value as the number of ticks. + /// + /// The duration, in ticks. + /// A whose will equal . + public static TimeSpan Ticks(this byte value) + { + return TimeSpan.FromTicks(value); + } + + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this byte value) + { + return TimeSpan.FromMilliseconds(value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this byte value) + { + return TimeSpan.FromSeconds(value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this byte value) + { + return TimeSpan.FromMinutes(value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this byte value) + { + return TimeSpan.FromHours(value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this byte value) + { + return TimeSpan.FromDays(value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this byte value) + { + return TimeSpan.FromDays(value * 7); + } +} diff --git a/X10D/src/Time/DecimalExtensions.cs b/X10D/src/Time/DecimalExtensions.cs new file mode 100644 index 0000000..d407ebd --- /dev/null +++ b/X10D/src/Time/DecimalExtensions.cs @@ -0,0 +1,77 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +public static class DecimalExtensions +{ + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this decimal value) + { + return TimeSpan.FromMilliseconds((double)value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this decimal value) + { + return TimeSpan.FromSeconds((double)value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this decimal value) + { + return TimeSpan.FromMinutes((double)value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this decimal value) + { + return TimeSpan.FromHours((double)value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this decimal value) + { + return TimeSpan.FromDays((double)value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this decimal value) + { + return TimeSpan.FromDays((double)value * 7); + } +} diff --git a/X10D/src/Time/DoubleExtensions.cs b/X10D/src/Time/DoubleExtensions.cs new file mode 100644 index 0000000..efa51fe --- /dev/null +++ b/X10D/src/Time/DoubleExtensions.cs @@ -0,0 +1,77 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +public static class DoubleExtensions +{ + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this double value) + { + return TimeSpan.FromMilliseconds(value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this double value) + { + return TimeSpan.FromSeconds(value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this double value) + { + return TimeSpan.FromMinutes(value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this double value) + { + return TimeSpan.FromHours(value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this double value) + { + return TimeSpan.FromDays(value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this double value) + { + return TimeSpan.FromDays(value * 7); + } +} diff --git a/X10D/src/Time/HalfExtensions.cs b/X10D/src/Time/HalfExtensions.cs new file mode 100644 index 0000000..2b89ba9 --- /dev/null +++ b/X10D/src/Time/HalfExtensions.cs @@ -0,0 +1,77 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +public static class HalfExtensions +{ + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this Half value) + { + return TimeSpan.FromMilliseconds((float)value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this Half value) + { + return TimeSpan.FromSeconds((float)value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this Half value) + { + return TimeSpan.FromMinutes((float)value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this Half value) + { + return TimeSpan.FromHours((float)value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this Half value) + { + return TimeSpan.FromDays((float)value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this Half value) + { + return TimeSpan.FromDays((float)value * 7); + } +} diff --git a/X10D/src/Time/Int16Extensions.cs b/X10D/src/Time/Int16Extensions.cs new file mode 100644 index 0000000..afd4928 --- /dev/null +++ b/X10D/src/Time/Int16Extensions.cs @@ -0,0 +1,87 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +public static class Int16Extensions +{ + /// + /// Returns a that represents this value as the number of ticks. + /// + /// The duration, in ticks. + /// A whose will equal . + public static TimeSpan Ticks(this short value) + { + return TimeSpan.FromTicks(value); + } + + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this short value) + { + return TimeSpan.FromMilliseconds(value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this short value) + { + return TimeSpan.FromSeconds(value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this short value) + { + return TimeSpan.FromMinutes(value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this short value) + { + return TimeSpan.FromHours(value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this short value) + { + return TimeSpan.FromDays(value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this short value) + { + return TimeSpan.FromDays(value * 7); + } +} diff --git a/X10D/src/Time/Int32Extensions.cs b/X10D/src/Time/Int32Extensions.cs new file mode 100644 index 0000000..d2f9dba --- /dev/null +++ b/X10D/src/Time/Int32Extensions.cs @@ -0,0 +1,87 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +public static class Int32Extensions +{ + /// + /// Returns a that represents this value as the number of ticks. + /// + /// The duration, in ticks. + /// A whose will equal . + public static TimeSpan Ticks(this int value) + { + return TimeSpan.FromTicks(value); + } + + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this int value) + { + return TimeSpan.FromMilliseconds(value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this int value) + { + return TimeSpan.FromSeconds(value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this int value) + { + return TimeSpan.FromMinutes(value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this int value) + { + return TimeSpan.FromHours(value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this int value) + { + return TimeSpan.FromDays(value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this int value) + { + return TimeSpan.FromDays(value * 7); + } +} diff --git a/X10D/src/Time/Int64Extensions.cs b/X10D/src/Time/Int64Extensions.cs new file mode 100644 index 0000000..b81e612 --- /dev/null +++ b/X10D/src/Time/Int64Extensions.cs @@ -0,0 +1,87 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +public static class Int64Extensions +{ + /// + /// Returns a that represents this value as the number of ticks. + /// + /// The duration, in ticks. + /// A whose will equal . + public static TimeSpan Ticks(this long value) + { + return TimeSpan.FromTicks(value); + } + + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this long value) + { + return TimeSpan.FromMilliseconds(value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this long value) + { + return TimeSpan.FromSeconds(value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this long value) + { + return TimeSpan.FromMinutes(value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this long value) + { + return TimeSpan.FromHours(value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this long value) + { + return TimeSpan.FromDays(value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this long value) + { + return TimeSpan.FromDays(value * 7); + } +} diff --git a/X10D/src/Time/SByteExtensions.cs b/X10D/src/Time/SByteExtensions.cs new file mode 100644 index 0000000..e651f20 --- /dev/null +++ b/X10D/src/Time/SByteExtensions.cs @@ -0,0 +1,88 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +[CLSCompliant(false)] +public static class SByteExtensions +{ + /// + /// Returns a that represents this value as the number of ticks. + /// + /// The duration, in ticks. + /// A whose will equal . + public static TimeSpan Ticks(this sbyte value) + { + return TimeSpan.FromTicks(value); + } + + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this sbyte value) + { + return TimeSpan.FromMilliseconds(value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this sbyte value) + { + return TimeSpan.FromSeconds(value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this sbyte value) + { + return TimeSpan.FromMinutes(value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this sbyte value) + { + return TimeSpan.FromHours(value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this sbyte value) + { + return TimeSpan.FromDays(value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this sbyte value) + { + return TimeSpan.FromDays(value * 7); + } +} diff --git a/X10D/src/Time/SingleExtensions.cs b/X10D/src/Time/SingleExtensions.cs new file mode 100644 index 0000000..46622dc --- /dev/null +++ b/X10D/src/Time/SingleExtensions.cs @@ -0,0 +1,77 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +public static class SingleExtensions +{ + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this float value) + { + return TimeSpan.FromMilliseconds(value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this float value) + { + return TimeSpan.FromSeconds(value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this float value) + { + return TimeSpan.FromMinutes(value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this float value) + { + return TimeSpan.FromHours(value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this float value) + { + return TimeSpan.FromDays(value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this float value) + { + return TimeSpan.FromDays(value * 7); + } +} diff --git a/X10D/src/Time/TimeSpanExtensions.cs b/X10D/src/Time/TimeSpanExtensions.cs new file mode 100644 index 0000000..428544c --- /dev/null +++ b/X10D/src/Time/TimeSpanExtensions.cs @@ -0,0 +1,31 @@ +namespace X10D.Time; + +/// +/// Extension methods for . +/// +public static class TimeSpanExtensions +{ + /// + /// Returns a that is a specified duration in the past relative to the current time. + /// + /// The whose duration to subtract. + /// + /// A that is a duration of in the past relative to the current time. + /// + public static DateTime Ago(this TimeSpan value) + { + return DateTime.Now.Subtract(value); + } + + /// + /// Returns a that is a specified duration in the future relative to the current time. + /// + /// The whose duration to add. + /// + /// A that is a duration of in the future relative to the current time. + /// + public static DateTime FromNow(this TimeSpan value) + { + return DateTime.Now.Add(value); + } +} diff --git a/X10D/src/Time/UInt16Extensions.cs b/X10D/src/Time/UInt16Extensions.cs new file mode 100644 index 0000000..f936202 --- /dev/null +++ b/X10D/src/Time/UInt16Extensions.cs @@ -0,0 +1,88 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +[CLSCompliant(false)] +public static class UInt16Extensions +{ + /// + /// Returns a that represents this value as the number of ticks. + /// + /// The duration, in ticks. + /// A whose will equal . + public static TimeSpan Ticks(this ushort value) + { + return TimeSpan.FromTicks(value); + } + + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this ushort value) + { + return TimeSpan.FromMilliseconds(value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this ushort value) + { + return TimeSpan.FromSeconds(value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this ushort value) + { + return TimeSpan.FromMinutes(value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this ushort value) + { + return TimeSpan.FromHours(value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this ushort value) + { + return TimeSpan.FromDays(value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this ushort value) + { + return TimeSpan.FromDays(value * 7); + } +} diff --git a/X10D/src/Time/UInt32Extensions.cs b/X10D/src/Time/UInt32Extensions.cs new file mode 100644 index 0000000..fcf8296 --- /dev/null +++ b/X10D/src/Time/UInt32Extensions.cs @@ -0,0 +1,88 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +[CLSCompliant(false)] +public static class UInt32Extensions +{ + /// + /// Returns a that represents this value as the number of ticks. + /// + /// The duration, in ticks. + /// A whose will equal . + public static TimeSpan Ticks(this uint value) + { + return TimeSpan.FromTicks(value); + } + + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this uint value) + { + return TimeSpan.FromMilliseconds(value); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this uint value) + { + return TimeSpan.FromSeconds(value); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this uint value) + { + return TimeSpan.FromMinutes(value); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this uint value) + { + return TimeSpan.FromHours(value); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this uint value) + { + return TimeSpan.FromDays(value); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this uint value) + { + return TimeSpan.FromDays(value * 7); + } +} diff --git a/X10D/src/Time/UInt64Extensions.cs b/X10D/src/Time/UInt64Extensions.cs new file mode 100644 index 0000000..05b940a --- /dev/null +++ b/X10D/src/Time/UInt64Extensions.cs @@ -0,0 +1,143 @@ +namespace X10D.Time; + +/// +/// Time-related extension methods for . +/// +[CLSCompliant(false)] +public static class UInt64Extensions +{ + /// + /// Returns a that represents this value as the number of ticks. + /// + /// The duration, in ticks. + /// A whose will equal . + public static TimeSpan Ticks(this ulong value) + { + long remainder = 0; + if (value > long.MaxValue) + { + remainder = (long)(value - long.MaxValue); + value -= long.MaxValue; + } + + return TimeSpan.FromTicks((long)value).Add(TimeSpan.FromTicks(remainder)); + } + + /// + /// Returns a that represents this value as the number of milliseconds. + /// + /// The duration, in milliseconds. + /// + /// A whose will equal . + /// + public static TimeSpan Milliseconds(this ulong value) + { + long remainder = 0; + + if (value > long.MaxValue) + { + remainder = (long)(value - long.MaxValue); + value -= long.MaxValue; + } + + return TimeSpan.FromMilliseconds((long)value).Add(TimeSpan.FromMilliseconds(remainder)); + } + + /// + /// Returns a that represents this value as the number of seconds. + /// + /// The duration, in seconds. + /// + /// A whose will equal . + /// + public static TimeSpan Seconds(this ulong value) + { + long remainder = 0; + + if (value > long.MaxValue) + { + remainder = (long)(value - long.MaxValue); + value -= long.MaxValue; + } + + return TimeSpan.FromSeconds((long)value).Add(TimeSpan.FromSeconds(remainder)); + } + + /// + /// Returns a that represents this value as the number of minutes. + /// + /// The duration, in minutes. + /// + /// A whose will equal . + /// + public static TimeSpan Minutes(this ulong value) + { + long remainder = 0; + + if (value > long.MaxValue) + { + remainder = (long)(value - long.MaxValue); + value -= long.MaxValue; + } + + return TimeSpan.FromMinutes((long)value).Add(TimeSpan.FromMinutes(remainder)); + } + + /// + /// Returns a that represents this value as the number of hours. + /// + /// The duration, in hours. + /// + /// A whose will equal . + /// + public static TimeSpan Hours(this ulong value) + { + long remainder = 0; + + if (value > long.MaxValue) + { + remainder = (long)(value - long.MaxValue); + value -= long.MaxValue; + } + + return TimeSpan.FromHours((long)value).Add(TimeSpan.FromHours(remainder)); + } + + /// + /// Returns a that represents this value as the number of days. + /// + /// The duration, in days. + /// A whose will equal . + public static TimeSpan Days(this ulong value) + { + long remainder = 0; + + if (value > long.MaxValue) + { + remainder = (long)(value - long.MaxValue); + value -= long.MaxValue; + } + + return TimeSpan.FromDays((long)value).Add(TimeSpan.FromDays(remainder)); + } + + /// + /// Returns a that represents this value as the number of weeks. + /// + /// The duration, in weeks. + /// + /// A whose will equal × 7. + /// + public static TimeSpan Weeks(this ulong value) + { + long remainder = 0; + + if (value > long.MaxValue) + { + remainder = (long)(value - long.MaxValue); + value -= long.MaxValue; + } + + return TimeSpan.FromDays((long)value * 7).Add(TimeSpan.FromDays(remainder * 7)); + } +}