Vali-Time 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Vali-Time --version 1.0.0
                    
NuGet\Install-Package Vali-Time -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-Time" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vali-Time" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Vali-Time" />
                    
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-Time --version 1.0.0
                    
#r "nuget: Vali-Time, 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-Time@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-Time&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Vali-Time&version=1.0.0
                    
Install as a Cake Tool

Vali-Time - Time Unit Conversion and Management for .NET

Introduction πŸš€

Welcome to Vali-Time, a lightweight .NET library designed to simplify time unit conversions and management. Whether you're working with milliseconds, seconds, minutes, or hours, Vali-Time provides a fluent and intuitive API to convert times, sum multiple values, break down times into components, and format them into human-readable strings. With automatic unit detection and cultural formatting support, this library is perfect for applications requiring accurate time handling and integrates seamlessly into any .NET project.

Installation πŸ“¦

To add Vali-Time to your .NET project, install it via NuGet with the following command:

dotnet add package Vali-Time

Ensure your project targets a compatible .NET version (e.g., .NET Standard 2.0 or later). Vali-Time is lightweight and has minimal dependencies, making it an easy addition to your application.

Usage πŸ› οΈ

Vali-Time focuses on converting time between units, summing times, breaking them down, and formatting them for display. The library provides a simple class, TimeConverter, with methods to perform these operations with precision and flexibility.

Basic Example

Here’s how you can convert and format a time value:

using Vali_Time.Converters;
using Vali_Time.Enums;

var converter = new TimeConverter();

// Convert 1.5 hours to seconds
decimal seconds = converter.Convert(1.5m, TimeUnit.Hours, TimeUnit.Seconds);
Console.WriteLine(seconds); // Outputs: 5400

// Format a time in minutes
string formattedTime = converter.FormatTime(123.456m, TimeUnit.Minutes);
Console.WriteLine(formattedTime); // Outputs: "123.46 min"

Key Methods πŸ“

Vali-Time offers a straightforward API for time management. Below are the key methods provided by the TimeConverter class:

Convert πŸ—οΈ

Converts a file size from one unit to another with precision:

var converter = new TimeConverter();

// Convert 2.5 minutes to seconds with 2 decimal places
decimal seconds = converter.Convert(2.5m, TimeUnit.Minutes, TimeUnit.Seconds, decimalPlaces: 2);
Console.WriteLine(seconds); // Outputs: 150.00

SumTimes 🎨

Sums multiple time values in different units and returns the result in a specified unit:

var converter = new ValiFileSize();

// Format 1,234,567 bytes as megabytes
string formatted = converter.FormatSize(1234567, FileSizeUnit.Megabytes, decimalPlaces: 3);
Console.WriteLine(formatted); // Outputs: "1.177 MB"

FormatTime 🎨

Formats a time value into a human-readable string with customizable decimal places and cultural formatting:

var converter = new TimeConverter();

// Format 1234.567 seconds as minutes
string formatted = converter.FormatTime(1234.567m, TimeUnit.Minutes, decimalPlaces: 3);
Console.WriteLine(formatted); // Outputs: "20.576 min"

GetBestUnit πŸ”

Automatically determines the most appropriate unit for a given time in seconds:

var converter = new TimeConverter();

// Find the best unit for 7200 seconds
var (time, unit) = converter.GetBestUnit(7200m);
string bestFormat = converter.FormatTime(time, unit);
Console.WriteLine(bestFormat); // Outputs: "2.00 h"

Breakdown 🧩

Breaks down a time in seconds into a dictionary of units (hours, minutes, seconds, milliseconds):

var converter = new TimeConverter();

// Breakdown 3665.678 seconds
var breakdown = converter.Breakdown(3665.678m);
Console.WriteLine($"{breakdown[TimeUnit.Hours]}h {breakdown[TimeUnit.Minutes]}m {breakdown[TimeUnit.Seconds]}s {breakdown[TimeUnit.Milliseconds]}ms");
// Outputs: "1h 1m 5s 678ms"

Working with Advanced Features 🧩

Vali-Time supports advanced use cases like cultural formatting and precise time management:

Cultural Formatting

Format times according to specific cultures:

using System.Globalization;

var converter = new TimeConverter();
var germanCulture = new CultureInfo("de-DE");

string formatted = converter.FormatTime(1234.567m, TimeUnit.Seconds, 2, germanCulture);
Console.WriteLine(formatted); // Outputs: "1234,57 s" (uses comma as decimal separator)

Combining Features

Convert, sum, and format times in one flow:

var converter = new TimeConverter();
var times = new (decimal, TimeUnit)[]
{
    (1.5m, TimeUnit.Hours),
    (30m, TimeUnit.Minutes)
};
decimal totalSeconds = converter.Add(TimeUnit.Seconds, times);
var (bestTime, bestUnit) = converter.GetBestUnit(totalSeconds);
string result = converter.FormatTime(bestTime, bestUnit);
Console.WriteLine(result); // Outputs: "2.00 h"

Comparison: Without vs. With Vali-FileSize βš–οΈ

Without Vali-Time (Manual Conversion)

Manually handling time conversions can be error-prone and cumbersome:

decimal hours = 1.5m;
decimal seconds = hours * 3600;
decimal totalSeconds = seconds + (30m * 60);
Console.WriteLine($"{totalSeconds:F2} s"); // Outputs: "7200.00 s"

With Vali-Time (Simplified Conversion)

Vali-Time streamlines the process with a clean and precise API:

var converter = new TimeConverter();
var times = new List<(decimal, TimeUnit)>()
{
    (1.5m, TimeUnit.Hours),
    (30m, TimeUnit.Minutes)
};
decimal totalSeconds = converter.Add(TimeUnit.Seconds, times);
string formatted = converter.FormatTime(totalSeconds, TimeUnit.Seconds);
Console.WriteLine(formatted); // Outputs: "7200.00 s"

Features and Enhancements 🌟

Recent Updates

  • Initial release (v1.0.0) with support for conversions across milliseconds, seconds, minutes, and hours. -Added (or SumTimes) for summing multiple time values with precision.
  • Introduced Breakdown for decomposing time into detailed components..
  • Included FormatTime with customizable decimal precision and cultural support.
  • Ensured robust validation with negative time checks and comprehensive exception handling.

Planned Features

  • Support for additional units like days, weeks, and months.
  • Enhanced formatting options, such as combined unit strings (e.g., "1h 30min 45s").
  • Integration with TimeSpan for broader compatibility.

Follow the project on GitHub for updates on new features and improvements!

Donations πŸ’–

If you find Vali-Time useful and would like to support its development, consider making a donation:

Your contributions help keep this project alive and improve its development! πŸš€

License πŸ“œ

This project is licensed under the Apache License 2.0.

Contributions 🀝

Feel free to open issues and submit pull requests to improve this library!

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (10)

Showing the top 5 NuGet packages that depend on Vali-Time:

Package Downloads
Vali-Holiday

Vali-Holiday is a comprehensive .NET library providing official public holiday data for 35+ countries across Latin America and Europe. Supports multilingual holiday names (es, en, pt, fr, de), Easter-based movable holidays, regional holidays, and an extensible IHolidayProvider interface for custom implementations.

Vali-TimeZone

Vali-TimeZone is a .NET library for timezone-aware date and time operations. Provides conversion between timezones, UTC offset calculations, DST detection, local/UTC interoperability, and a curated list of IANA-compatible timezone identifiers for 30+ countries.

Vali-Range

Vali-Range is a lightweight .NET library for working with date ranges. It provides a precise and flexible API for creating, querying, and iterating date ranges, including overlap detection, intersections, unions, and calendar-based splits.

Vali-Calendar

Vali-Calendar is a .NET library for calendar-based operations. Provides workday calculations, ISO week numbers, CalendarWeek struct, month/year utilities, and business-day arithmetic without requiring external holiday providers.

Vali-CountDown

Vali-CountDown is a .NET library for deadline and countdown tracking. Provides expiry detection, time-until/elapsed calculations, progress percentage, time breakdowns, and human-readable countdown formatting.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 194 10/14/2026
1.0.1 632 3/26/2026
1.0.0 311 4/10/2025

Vali-FileSize v1.0.0 introduces a lightweight and efficient solution for file size management in .NET applications:
           - Support for conversions across multiple units (bytes, kilobytes, megabytes, gigabytes, terabytes, and petabytes), providing precise and flexible size handling.
           - Addition of human-readable formatting with customizable decimal precision and cultural support, making file sizes easy to display and understand.
           - Introduction of automatic unit detection, allowing developers to effortlessly determine the most appropriate unit for any given size, enhancing usability in diverse scenarios.
           Designed for seamless integration with .NET Standard 2.0 and above, Vali-FileSize ensures compatibility and reliability for modern .NET development.