Table of Contents

TimeSpanExtensions Class

Definition

Namespace
Cuemon.Extensions
Assemblies
Cuemon.Extensions.Core.dll
Source
src/Cuemon.Extensions.Core/TimeSpanExtensions.cs

Extension methods for the TimeSpan struct.

public static class TimeSpanExtensions
Inheritance
object
TimeSpanExtensions

Examples

TimeSpanExtensions provides extension methods for TimeSpan including high-resolution unit queries (GetTotalNanoseconds, GetTotalMicroseconds) and interval snapping (Floor, Ceiling, Round). This example creates TimeSpan.FromHours(1) and TimeSpan.FromMinutes(280), then calls GetTotalNanoseconds and GetTotalMicroseconds on the hour, Floor(1, TimeUnit.Hours) and Ceiling(1, TimeUnit.Hours) on 280 minutes, and Round on 45 minutes with both up and down directions. Console output shows the nanosecond and microsecond values, the floored/ceiling hours, and the rounded minutes.

using System;
using Cuemon;
using Cuemon.Extensions;

namespace MyApp.Examples;

public static class TimeSpanExtensionsExample
{
    public static void Demonstrate()
    {
        var hour = TimeSpan.FromHours(1);
        var duration = TimeSpan.FromMinutes(280);
        var shortDuration = TimeSpan.FromMinutes(45);

        Console.WriteLine(hour.GetTotalNanoseconds());
        Console.WriteLine(hour.GetTotalMicroseconds());
        Console.WriteLine(duration.Floor(1, TimeUnit.Hours));
        Console.WriteLine(duration.Ceiling(1, TimeUnit.Hours));
        Console.WriteLine(shortDuration.Round(TimeSpan.FromHours(1), VerticalDirection.Up));
        Console.WriteLine(shortDuration.Round(30, TimeUnit.Minutes, VerticalDirection.Down));
    }
}

Fields

Name Description
TicksPerMicrosecond

Represents the number of ticks in 1 microsecond. This field is constant.

TicksPerNanosecond

Represents the number of ticks in 1 nanosecond. This field is constant.

Methods

Name Description
Ceiling(TimeSpan, double, TimeUnit)

Returns a TimeSpan value that is rounded towards positive infinity.

Ceiling(TimeSpan, TimeSpan)

Returns a TimeSpan value that is rounded towards positive infinity.

Floor(TimeSpan, double, TimeUnit)

Returns a TimeSpan value that is rounded towards negative infinity.

Floor(TimeSpan, TimeSpan)

Returns a TimeSpan value that is rounded towards negative infinity.

GetTotalMicroseconds(TimeSpan)

Gets the total number of microseconds represented by the specified TimeSpan structure.

GetTotalNanoseconds(TimeSpan)

Gets the total number of nanoseconds represented by the specified TimeSpan structure.

Round(TimeSpan, double, TimeUnit, VerticalDirection)

Returns a TimeSpan value that is rounded either towards negative infinity or positive infinity.

Round(TimeSpan, TimeSpan, VerticalDirection)

Returns a TimeSpan value that is rounded either towards negative infinity or positive infinity.