Vali-Duration 1.0.0

dotnet add package Vali-Duration --version 1.0.0
                    
NuGet\Install-Package Vali-Duration -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Vali-Duration" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vali-Duration" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Vali-Duration" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Vali-Duration --version 1.0.0
                    
#r "nuget: Vali-Duration, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Vali-Duration@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Vali-Duration&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Vali-Duration&version=1.0.0
                    
Install as a Cake Tool

Vali-Duration

NuGet License .NET

Vali-Duration provides a high-precision, immutable duration value type (ValiDuration) that uses decimal arithmetic internally. It avoids the floating-point rounding errors common with double-based TimeSpan calculations and supports fluent conversions, formatting, and arithmetic operators.

Features

  • decimal-precision internal representation — no floating-point drift
  • Factory methods: FromMilliseconds, FromSeconds, FromMinutes, FromHours, FromDays, FromWeeks
  • Convert to any TimeUnit via As(TimeUnit)
  • Named accessors: TotalMilliseconds, TotalSeconds, TotalMinutes, TotalHours, TotalDays, TotalWeeks
  • Arithmetic operators: +, -, *, /
  • Comparison operators: <, >, <=, >=, ==, !=
  • Implicit conversion to/from TimeSpan
  • Human-readable Format() output
  • Implements IEquatable<ValiDuration> and IComparable<ValiDuration>

Installation

dotnet add package Vali-Duration

Compiled targets: net8.0 · net9.0. Compatible with .NET 6 and .NET 7 via NuGet backward compatibility — no separate build needed.

Quick Start

using Vali_Duration.Models;
using Vali_Time.Enums;

// Create a duration
var duration = ValiDuration.FromHours(1.5m);

// Access in different units
Console.WriteLine(duration.TotalMinutes);      // 90
Console.WriteLine(duration.As(TimeUnit.Seconds)); // 5400

// Arithmetic
var d1 = ValiDuration.FromMinutes(30);
var d2 = ValiDuration.FromMinutes(45);
var total = d1 + d2; // 75 minutes

var doubled = d1 * 2m; // 60 minutes

// Comparison
bool isLonger = d2 > d1; // true

// Implicit TimeSpan conversion
TimeSpan ts = ValiDuration.FromHours(2);
ValiDuration fromTs = TimeSpan.FromMinutes(90);

// Formatting
Console.WriteLine(ValiDuration.FromHours(1.5m).Format());
// "1h 30m 0s"

Factory Methods

ValiDuration.FromMilliseconds(500m);
ValiDuration.FromSeconds(90m);
ValiDuration.FromMinutes(45m);
ValiDuration.FromHours(2.5m);
ValiDuration.FromDays(1.5m);
ValiDuration.FromWeeks(2m);

Conversion

var d = ValiDuration.FromHours(2);

d.TotalMilliseconds  // 7_200_000
d.TotalSeconds       // 7_200
d.TotalMinutes       // 120
d.TotalHours         // 2
d.TotalDays          // 0.0833...
d.TotalWeeks         // 0.0119...

// Generic conversion
d.As(TimeUnit.Minutes) // 120
d.As(TimeUnit.Days)    // 0.0833...

Operators

var a = ValiDuration.FromMinutes(60);
var b = ValiDuration.FromMinutes(30);

var sum  = a + b;    // 90 min
var diff = a - b;    // 30 min
var mult = a * 2m;   // 120 min
var div  = a / 2m;   // 30 min

bool eq  = a == b;   // false
bool gt  = a > b;    // true
bool lte = b <= a;   // true

TimeSpan Interoperability

// Implicit from TimeSpan
ValiDuration d = TimeSpan.FromHours(1);

// Implicit to TimeSpan
TimeSpan ts = ValiDuration.FromMinutes(90);

// Use anywhere TimeSpan is accepted
Task.Delay((TimeSpan)ValiDuration.FromSeconds(5));

Why Not TimeSpan?

TimeSpan uses double internally, which causes rounding errors in financial and billing calculations:

// TimeSpan — floating-point drift
var ts = TimeSpan.FromHours(1.1) + TimeSpan.FromHours(1.2);
Console.WriteLine(ts.TotalHours); // 2.2999999999999998 ← drift

// ValiDuration — decimal precision
var vd = ValiDuration.FromHours(1.1m) + ValiDuration.FromHours(1.2m);
Console.WriteLine(vd.TotalHours);  // 2.3 ← exact

Part of the Vali-Tempo Ecosystem

Package Description
Vali-Time Core time conversion and formatting
Vali-Range Date range operations
Vali-Calendar Workday and week calculations
Vali-Duration High-precision duration struct
Vali-CountDown Countdown and deadline tracking
Vali-Age Age calculation utilities
Vali-Schedule Recurring event scheduling
Vali-Holiday Holiday providers for 35+ countries
Vali-TimeZone Timezone conversion and discovery
Vali-Tempo Meta-package: all of the above

License

MIT © 2025 Felipe Rafael Montenegro Morriberon

Donations

If this package is useful to you, consider supporting its development:


Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Vali-Duration:

Package Downloads
Vali-Tempo

Vali-Tempo is the all-in-one meta-package for the Vali-Tempo ecosystem. Installing it gives you every module — Vali-Time, Vali-Range, Vali-Calendar, Vali-CountDown, Vali-Age, Vali-Holiday, Vali-TimeZone, Vali-Schedule, and Vali-Duration — with a single NuGet reference and a single DI registration call.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 92 3/26/2026

Vali-Duration v1.0.0 — initial release: ValiDuration struct with decimal precision, all arithmetic and comparison operators, TimeSpan interop, unit factories (FromHours, FromMinutes, etc.), As(TimeUnit) conversion, and Format() output.