Haakjell.DecimalTime
1.1.0
dotnet add package Haakjell.DecimalTime --version 1.1.0
NuGet\Install-Package Haakjell.DecimalTime -Version 1.1.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="Haakjell.DecimalTime" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Haakjell.DecimalTime" Version="1.1.0" />
<PackageReference Include="Haakjell.DecimalTime" />
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 Haakjell.DecimalTime --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Haakjell.DecimalTime, 1.1.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 Haakjell.DecimalTime@1.1.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=Haakjell.DecimalTime&version=1.1.0
#tool nuget:?package=Haakjell.DecimalTime&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Haakjell.DecimalTime
A .NET library that provides decimal (metric) time functionality through the DecDateTime, DecTimeSpan, and DecTimeOnly types.
Disclaimer
This package is completely useless, unless you really like decimal time. 😉
Decimal Time System
In decimal time:
- 1 day = 10 decimal hours
- 1 decimal hour = 100 decimal minutes
- 1 decimal minute = 100 decimal seconds
- 1 day = 100,000 decimal seconds
This means noon (12:00:00) in standard time equals 5:00:00 in decimal time.
Installation
dotnet add package Haakjell.DecimalTime
Usage
Basic Usage
using Haakjell.DecimalTime;
// Get current time in decimal format
var now = DecDateTime.Now;
Console.WriteLine($"Current decimal time: {now.ToDecimalTimeString()}");
// Output: Current decimal time: 7:35:42 (example)
// Create from standard DateTime
var noon = new DecDateTime(2024, 6, 15, 12, 0, 0);
Console.WriteLine($"Noon in decimal: {noon.DecimalHour}:{noon.DecimalMinute:D2}:{noon.DecimalSecond:D2}");
// Output: Noon in decimal: 5:00:00
// Create from decimal time components
var decTime = DecDateTime.FromDecimalTime(2024, 6, 15, 5, 0, 0);
Console.WriteLine($"Standard hour: {decTime.Hour}");
// Output: Standard hour: 12
Working with DecTimeSpan
// Create decimal time intervals
var twoAndHalfDecimalHours = DecTimeSpan.FromDecimalHours(2.5);
Console.WriteLine($"In standard hours: {twoAndHalfDecimalHours.ToTimeSpan().TotalHours}");
// Output: In standard hours: 6
// Arithmetic with DecDateTime
var later = DecDateTime.Now + DecTimeSpan.FromDecimalHours(1);
// Difference between times
var diff = later - DecDateTime.Now;
Console.WriteLine($"Difference: {diff.TotalDecimalHours} decimal hours");
Working with DecTimeOnly
// Time-of-day without date component
var lunchTime = new DecTimeOnly(12, 0, 0); // Noon
Console.WriteLine($"Lunch in decimal: {lunchTime.ToDecimalTimeString()}");
// Output: Lunch in decimal: 5:00:00
// Create from decimal time
var meetingTime = DecTimeOnly.FromDecimalTime(7, 50, 0); // 6 PM
Console.WriteLine($"Meeting at: {meetingTime.Hour}:00");
// Output: Meeting at: 18:00
// Use extension method
var now = TimeOnly.FromDateTime(DateTime.Now).ToDecTimeOnly();
Properties
DecDateTime provides both standard and decimal time properties:
var dt = DecDateTime.Now;
// Standard properties (pass-through from DateTime)
int year = dt.Year;
int month = dt.Month;
int day = dt.Day;
int hour = dt.Hour; // 0-23
int minute = dt.Minute; // 0-59
int second = dt.Second; // 0-59
// Decimal time properties
int decHour = dt.DecimalHour; // 0-9
int decMin = dt.DecimalMinute; // 0-99
int decSec = dt.DecimalSecond; // 0-99
double totalDecSec = dt.TotalDecimalSeconds; // 0-99999.999...
Conversion
// Convert to/from DateTime
DateTime standardTime = decDateTime.ToDateTime();
DecDateTime decTime = DecDateTime.FromDateTime(standardTime);
// Implicit/explicit conversion operators
DateTime dt = decDateTime; // implicit to DateTime
DecDateTime ddt = (DecDateTime)dateTime; // explicit from DateTime
API Reference
DecDateTime
| Member | Description |
|---|---|
Now, UtcNow, Today |
Static properties for current time |
MinValue, MaxValue |
Range boundaries |
FromDateTime(DateTime) |
Create from standard DateTime |
FromDecimalTime(...) |
Create from decimal time components |
DecimalHour, DecimalMinute, DecimalSecond |
Decimal time components |
TotalDecimalSeconds |
Total decimal seconds since midnight |
ToDateTime() |
Convert to standard DateTime |
ToDecimalTimeString() |
Format as "h:mm:ss" decimal time |
DecTimeSpan
| Member | Description |
|---|---|
Zero, MinValue, MaxValue |
Static properties |
FromTimeSpan(TimeSpan) |
Create from standard TimeSpan |
FromDecimalHours(double) |
Create from decimal hours |
FromDecimalMinutes(double) |
Create from decimal minutes |
FromDecimalSeconds(double) |
Create from decimal seconds |
TotalDecimalHours, TotalDecimalMinutes, TotalDecimalSeconds |
Total values |
ToTimeSpan() |
Convert to standard TimeSpan |
DecTimeOnly
| Member | Description |
|---|---|
Midnight, Noon, MinValue, MaxValue |
Static properties for common times |
FromTimeOnly(TimeOnly) |
Create from standard TimeOnly |
FromDecimalTime(...) |
Create from decimal time components |
DecimalHour, DecimalMinute, DecimalSecond |
Decimal time components |
TotalDecimalSeconds |
Total decimal seconds since midnight |
ToTimeOnly() |
Convert to standard TimeOnly |
ToDecimalTimeString() |
Format as "h:mm:ss" decimal time |
Future Goals
This library aims to achieve full API parity with System.DateTime and System.TimeSpan. Future versions will include:
- Parsing methods (
Parse,TryParse) - Additional formatting options
- Date arithmetic methods (
AddDays,AddDecimalHours, etc.) - Time zone support
- Calendar integrations
License
MIT
| Product | Versions 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 is compatible. 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.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.