Vali-Time
1.0.0
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
<PackageReference Include="Vali-Time" Version="1.0.0" />
<PackageVersion Include="Vali-Time" Version="1.0.0" />
<PackageReference Include="Vali-Time" />
paket add Vali-Time --version 1.0.0
#r "nuget: Vali-Time, 1.0.0"
#:package Vali-Time@1.0.0
#addin nuget:?package=Vali-Time&version=1.0.0
#tool nuget:?package=Vali-Time&version=1.0.0
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:
- For Latin America: Donate via MercadoPago
- For International Donations: Donate via PayPal
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 | Versions 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. |
-
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.
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.