Table of Contents

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

timeSpan TimeSpan

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

timeSpan TimeSpan

The TimeSpan instance whose component to get.

component TimeSpanComponent

The 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

timeSpan TimeSpan

The TimeSpan instance whose component to get.

component TimeSpanComponent

The 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

timeSpan TimeSpan

The TimeSpan instance whose sign to get.

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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted time components.

value int

The value to set the component to.

component TimeSpanComponent

The component of the TimeSpan instance that will be changed. Ticks is not accepted in this overload, consider using WithComponent(TimeSpan, long, TimeSpanComponent).

Returns

TimeSpan

A copy of the original TimeSpan with the specified component set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for value falls 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted time components.

value long

The value to set the component to.

component TimeSpanComponent

The component of the TimeSpan instance that will be changed. Ticks is also accepted.

Returns

TimeSpan

A copy of the original TimeSpan with the specified component set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for value falls 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted days component.

days int

The value of the days component of the resulting copied TimeSpan. It must match the sign of the given TimeSpan, if non-zero.

Returns

TimeSpan

A copy of the original TimeSpan with the days component set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for days does 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted days and hours components.

days int

The value of the days component of the resulting copied TimeSpan. It must match the sign of the given TimeSpan, if non-zero.

hours int

The 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 hours is outside the range [-23, 23] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for days does 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted days, hours and minutes components.

days int

The value of the days component of the resulting copied TimeSpan. It must match the sign of the given TimeSpan, if non-zero.

hours int

The 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.

minutes int

The 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 minutes is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for hours is outside the range [-23, 23] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for days does 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted hours component.

hours int

The 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 hours component set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for hours is 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted hours and minutes components.

hourMinute HourMinute

The 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted hours and minutes components.

hours int

The 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.

minutes int

The 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 minutes is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for hours is 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted hours, minutes and seconds components.

hours int

The 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.

minutes int

The 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.

seconds int

The 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 seconds is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for minutes is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for hours is 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted hours, minutes and seconds components.

hours int

The 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.

minutes int

The 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.

seconds int

The 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.

milliseconds int

The 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 milliseconds is outside the range [-999, 999] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for seconds is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for minutes is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for hours is 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted milliseconds component.

milliseconds int

The 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 milliseconds is 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted minutes component.

minutes int

The 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 minutes component set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for minutes is 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted minutes and seconds components.

minuteSecond MinuteSecond

The 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted minutes and seconds components.

minutes int

The 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.

seconds int

The 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 seconds is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for minutes is 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted minutes, seconds and milliseconds components.

minutes int

The 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.

seconds int

The 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.

milliseconds int

The 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 milliseconds is outside the range [-999, 999] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for seconds is outside the range [-59, 59] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for minutes is 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted seconds component.

seconds int

The 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 seconds component set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for seconds is 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

timeSpan TimeSpan

The TimeSpan from which to create the copy with the adjusted seconds and milliseconds components.

seconds int

The 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.

milliseconds int

The 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 milliseconds is outside the range [-999, 999] -or- does not match the sign of the given TimeSpan, if non-zero -or- the given value for seconds is 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

timeSpan TimeSpan

The TimeSpan instance whose same-day duration to calculate.

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

timeSpan TimeSpan

The TimeSpan instance whose same-hour duration to calculate.

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

timeSpan TimeSpan

The TimeSpan instance whose same-millisecond duration to calculate.

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

timeSpan TimeSpan

The TimeSpan instance whose same-minute duration to calculate.

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)

Parameters

timeSpan TimeSpan

The TimeSpan instance whose same-second duration to calculate.

Returns

TimeSpan

The duration within the final second. In other words, this reflects the same duration, except the Days, Hours, Minutes and Seconds properties are 0.