Class TimeSpanExtensions
- Namespace
- Garyon.Extensions
- Assembly
- Garyon.dll
Provides extensions for the TimeSpan struct.
public static class TimeSpanExtensions
- Inheritance
-
TimeSpanExtensions
- Inherited Members
Methods
Absolute(TimeSpan)
Get the absolute value of the TimeSpan.
public static TimeSpan Absolute(this TimeSpan timeSpan)
Parameters
timeSpanTimeSpan
Returns
- TimeSpan
A non-negative TimeSpan representing the same duration as the provided instance, but without a negative sign.
GetComponent(TimeSpan, TimeSpanComponent)
Gets the specified component of the given TimeSpan instance.
public static int GetComponent(this TimeSpan timeSpan, TimeSpanComponent component)
Parameters
timeSpanTimeSpanThe TimeSpan instance whose component to get.
componentTimeSpanComponentThe component of the TimeSpan instance to get. To get the Ticks component, use GetComponentInt64(TimeSpan, TimeSpanComponent).
Returns
- int
The requested component of the TimeSpan instance, represented by the specified TimeSpanComponent.
GetComponentInt64(TimeSpan, TimeSpanComponent)
Gets the specified component of the given TimeSpan instance.
public static long GetComponentInt64(this TimeSpan timeSpan, TimeSpanComponent component)
Parameters
timeSpanTimeSpanThe TimeSpan instance whose component to get.
componentTimeSpanComponentThe component of the TimeSpan instance to get.
Returns
- long
The requested component of the TimeSpan instance, represented by the specified TimeSpanComponent.
Sign(TimeSpan)
Gets the sign of the given TimeSpan.
public static int Sign(this TimeSpan timeSpan)
Parameters
Returns
- int
The sign of the Ticks property, in accordance with Sign(long).
WithComponent(TimeSpan, int, TimeSpanComponent)
Creates a copy of a given TimeSpan, where the specified component of the copied value is set to the specified value.
[ExcludeFromCodeCoverage]
public static TimeSpan WithComponent(this TimeSpan timeSpan, int value, TimeSpanComponent component)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted time components.
valueintThe value to set the component to.
componentTimeSpanComponentThe component of the TimeSpan instance that will be changed. Ticks is not accepted in this overload, consider using WithComponent(TimeSpan, long, TimeSpanComponent).
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
valuefalls out of the valid range for the given component, or an invalid day is represented.
WithComponent(TimeSpan, long, TimeSpanComponent)
Creates a copy of a given TimeSpan, where the specified component of the copied value is set to the specified value.
[ExcludeFromCodeCoverage]
public static TimeSpan WithComponent(this TimeSpan timeSpan, long value, TimeSpanComponent component)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted time components.
valuelongThe value to set the component to.
componentTimeSpanComponentThe component of the TimeSpan instance that will be changed. Ticks is also accepted.
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
valuefalls out of the valid range for the given component, or an invalid day is represented.
WithDays(TimeSpan, int)
Creates a copy of a given TimeSpan, where the days component of the copied value is set to a specified value.
public static TimeSpan WithDays(this TimeSpan timeSpan, int days)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted days component.
daysintThe value of the days component of the resulting copied TimeSpan. It must match the sign of the given TimeSpan, if non-zero.
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
daysdoes not match the sign of the given TimeSpan, if non-zero.
WithDaysHours(TimeSpan, int, int)
Creates a copy of a given TimeSpan, where the days and hours components of the copied value are set to a specified value.
public static TimeSpan WithDaysHours(this TimeSpan timeSpan, int days, int hours)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted days and hours components.
daysintThe value of the days component of the resulting copied TimeSpan. It must match the sign of the given TimeSpan, if non-zero.
hoursintThe value of the hours component of the resulting copied TimeSpan. It must be within the range [-23, 23] and match the sign of the given TimeSpan, if non-zero.
Returns
- TimeSpan
A copy of the original TimeSpan with the days and hours components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for
hoursis outside the range [-23, 23] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value fordaysdoes not match the sign of the given TimeSpan, if non-zero.
WithDaysHoursMinutes(TimeSpan, int, int, int)
Creates a copy of a given TimeSpan, where the days, hours and minutes components of the copied value are set to a specified value.
public static TimeSpan WithDaysHoursMinutes(this TimeSpan timeSpan, int days, int hours, int minutes)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted days, hours and minutes components.
daysintThe value of the days component of the resulting copied TimeSpan. It must match the sign of the given TimeSpan, if non-zero.
hoursintThe value of the hours component of the resulting copied TimeSpan. It must be within the range [-23, 23] and match the sign of the given TimeSpan, if non-zero.
minutesintThe value of the minutes component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
Returns
- TimeSpan
A copy of the original TimeSpan with the days, hours and minutes components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for
minutesis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forhoursis outside the range [-23, 23] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value fordaysdoes not match the sign of the given TimeSpan, if non-zero.
WithHours(TimeSpan, int)
Creates a copy of a given TimeSpan, where the hours component of the copied value is set to a specified value.
public static TimeSpan WithHours(this TimeSpan timeSpan, int hours)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted hours component.
hoursintThe value of the hours component of the resulting copied TimeSpan. It must be within the range [-23, 23] and match the sign of the given TimeSpan, if non-zero.
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
hoursis outside the range [-23, 23] -or- does not match the sign of the given TimeSpan, if non-zero.
WithHoursMinutes(TimeSpan, HourMinute)
Creates a copy of a given TimeSpan, where the hours and minutes components of the copied value are set to a specified value.
public static TimeSpan WithHoursMinutes(this TimeSpan timeSpan, HourMinute hourMinute)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted hours and minutes components.
hourMinuteHourMinuteThe hours and minutes components of the resulting copied TimeSpan.
Returns
- TimeSpan
A copy of the original TimeSpan with the hours and minutes components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for the minutes component is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for the hours component is outside the range [-23, 23] -or- does not match the sign of the given TimeSpan, if non-zero.
WithHoursMinutes(TimeSpan, int, int)
Creates a copy of a given TimeSpan, where the hours and minutes components of the copied value are set to a specified value.
public static TimeSpan WithHoursMinutes(this TimeSpan timeSpan, int hours, int minutes)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted hours and minutes components.
hoursintThe value of the hours component of the resulting copied TimeSpan. It must be within the range [-23, 23] and match the sign of the given TimeSpan, if non-zero.
minutesintThe value of the minutes component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
Returns
- TimeSpan
A copy of the original TimeSpan with the hours and minutes components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for
minutesis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forhoursis outside the range [-23, 23] -or- does not match the sign of the given TimeSpan, if non-zero.
WithHoursMinutesSeconds(TimeSpan, int, int, int)
Creates a copy of a given TimeSpan, where the hours, minutes and seconds components of the copied value are set to a specified value.
public static TimeSpan WithHoursMinutesSeconds(this TimeSpan timeSpan, int hours, int minutes, int seconds)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted hours, minutes and seconds components.
hoursintThe value of the hours component of the resulting copied TimeSpan. It must be within the range [-23, 23] and match the sign of the given TimeSpan, if non-zero.
minutesintThe value of the minutes component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
secondsintThe value of the seconds component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
Returns
- TimeSpan
A copy of the original TimeSpan with the hours, minutes and seconds components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for
secondsis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forminutesis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forhoursis outside the range [-23, 23] -or- does not match the sign of the given TimeSpan, if non-zero.
WithHoursMinutesSecondsMilliseconds(TimeSpan, int, int, int, int)
Creates a copy of a given TimeSpan, where the hours, minutes and seconds components of the copied value are set to a specified value.
public static TimeSpan WithHoursMinutesSecondsMilliseconds(this TimeSpan timeSpan, int hours, int minutes, int seconds, int milliseconds)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted hours, minutes and seconds components.
hoursintThe value of the hours component of the resulting copied TimeSpan. It must be within the range [-23, 23] and match the sign of the given TimeSpan, if non-zero.
minutesintThe value of the minutes component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
secondsintThe value of the seconds component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
millisecondsintThe value of the milliseconds component of the resulting copied TimeSpan. It must be within the range [-999, 999] and match the sign of the given TimeSpan, if non-zero.
Returns
- TimeSpan
A copy of the original TimeSpan with the hours, minutes and seconds components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for
millisecondsis outside the range [-999, 999] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forsecondsis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forminutesis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forhoursis outside the range [-23, 23] -or- does not match the sign of the given TimeSpan, if non-zero.
WithMilliseconds(TimeSpan, int)
Creates a copy of a given TimeSpan, where the milliseconds component of the copied value is set to a specified value.
public static TimeSpan WithMilliseconds(this TimeSpan timeSpan, int milliseconds)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted milliseconds component.
millisecondsintThe value of the milliseconds component of the resulting copied TimeSpan. It must be within the range [-999, 999] and match the sign of the given TimeSpan, if non-zero.
Returns
- TimeSpan
A copy of the original TimeSpan with the milliseconds component set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for
millisecondsis outside the range [-999, 999] -or- does not match the sign of the given TimeSpan, if non-zero.
WithMinutes(TimeSpan, int)
Creates a copy of a given TimeSpan, where the minutes component of the copied value is set to a specified value.
public static TimeSpan WithMinutes(this TimeSpan timeSpan, int minutes)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted minutes component.
minutesintThe value of the minutes component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
minutesis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero.
WithMinutesSeconds(TimeSpan, MinuteSecond)
Creates a copy of a given TimeSpan, where the minutes and seconds components of the copied value are set to a specified value.
public static TimeSpan WithMinutesSeconds(this TimeSpan timeSpan, MinuteSecond minuteSecond)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted minutes and seconds components.
minuteSecondMinuteSecondThe minutes and seconds components of the resulting copied TimeSpan.
Returns
- TimeSpan
A copy of the original TimeSpan with the minutes and seconds components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for the seconds component is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for the minutes component is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero.
WithMinutesSeconds(TimeSpan, int, int)
Creates a copy of a given TimeSpan, where the minutes and seconds components of the copied value are set to a specified value.
public static TimeSpan WithMinutesSeconds(this TimeSpan timeSpan, int minutes, int seconds)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted minutes and seconds components.
minutesintThe value of the minutes component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
secondsintThe value of the seconds component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
Returns
- TimeSpan
A copy of the original TimeSpan with the minutes and seconds components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for
secondsis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forminutesis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero.
WithMinutesSecondsMilliseconds(TimeSpan, int, int, int)
Creates a copy of a given TimeSpan, where the minutes, seconds and milliseconds components of the copied value are set to a specified value.
public static TimeSpan WithMinutesSecondsMilliseconds(this TimeSpan timeSpan, int minutes, int seconds, int milliseconds)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted minutes, seconds and milliseconds components.
minutesintThe value of the minutes component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
secondsintThe value of the seconds component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
millisecondsintThe value of the milliseconds component of the resulting copied TimeSpan. It must be within the range [-999, 999] and match the sign of the given TimeSpan, if non-zero.
Returns
- TimeSpan
A copy of the original TimeSpan with the minutes, seconds and milliseconds components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for
millisecondsis outside the range [-999, 999] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forsecondsis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forminutesis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero.
WithSeconds(TimeSpan, int)
Creates a copy of a given TimeSpan, where the seconds component of the copied value is set to a specified value.
public static TimeSpan WithSeconds(this TimeSpan timeSpan, int seconds)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted seconds component.
secondsintThe value of the seconds component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
secondsis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero.
WithSecondsMilliseconds(TimeSpan, int, int)
Creates a copy of a given TimeSpan, where the seconds and milliseconds components of the copied value are set to a specified value.
public static TimeSpan WithSecondsMilliseconds(this TimeSpan timeSpan, int seconds, int milliseconds)
Parameters
timeSpanTimeSpanThe TimeSpan from which to create the copy with the adjusted seconds and milliseconds components.
secondsintThe value of the seconds component of the resulting copied TimeSpan. It must be within the range [-59, 59] and match the sign of the given TimeSpan, if non-zero.
millisecondsintThe value of the milliseconds component of the resulting copied TimeSpan. It must be within the range [-999, 999] and match the sign of the given TimeSpan, if non-zero.
Returns
- TimeSpan
A copy of the original TimeSpan with the seconds and milliseconds components set to the specified value.
Exceptions
- ArgumentOutOfRangeException
The given value for
millisecondsis outside the range [-999, 999] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value forsecondsis outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero.
WithinDay(TimeSpan)
Creates a new TimeSpan instance reflecting the duration within the final day.
public static TimeSpan WithinDay(this TimeSpan timeSpan)
Parameters
Returns
- TimeSpan
The duration within the final day. In other words, this reflects the same duration, except the Days property is 0.
WithinHour(TimeSpan)
Creates a new TimeSpan instance reflecting the duration within the final hour.
public static TimeSpan WithinHour(this TimeSpan timeSpan)
Parameters
Returns
- TimeSpan
The duration within the final hour. In other words, this reflects the same duration, except the Days and Hours properties are 0.
WithinMillisecond(TimeSpan)
Creates a new TimeSpan instance reflecting the duration within the final millisecond.
public static TimeSpan WithinMillisecond(this TimeSpan timeSpan)
Parameters
Returns
- TimeSpan
The duration within the final millisecond. In other words, this reflects the same duration, except the Days, Hours, Minutes, Seconds and Milliseconds properties are 0.
WithinMinute(TimeSpan)
Creates a new TimeSpan instance reflecting the duration within the final minute.
public static TimeSpan WithinMinute(this TimeSpan timeSpan)
Parameters
Returns
- TimeSpan
The duration within the final minute. In other words, this reflects the same duration, except the Days, Hours and Minutes properties are 0.
WithinSecond(TimeSpan)
Creates a new TimeSpan instance reflecting the duration within the final second.
public static TimeSpan WithinSecond(this TimeSpan timeSpan)