LfrlAnvil.Chrono 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package LfrlAnvil.Chrono --version 0.2.0                
NuGet\Install-Package LfrlAnvil.Chrono -Version 0.2.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="LfrlAnvil.Chrono" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LfrlAnvil.Chrono --version 0.2.0                
#r "nuget: LfrlAnvil.Chrono, 0.2.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.
// Install LfrlAnvil.Chrono as a Cake Addin
#addin nuget:?package=LfrlAnvil.Chrono&version=0.2.0

// Install LfrlAnvil.Chrono as a Cake Tool
#tool nuget:?package=LfrlAnvil.Chrono&version=0.2.0                

(root) NuGet Badge

LfrlAnvil.Chrono

This project contains functionalities related to date & time, as well as timezones. Most of them work directly on dotnet's DateTime and TimeZoneInfo types.

Documentation

Technical documentation can be found here.

Examples

Following are a few examples of date & time structures:

// creates a new timestamp provider
var timestamps = new TimestampProvider();

// returns the current timestamp, which represents the number of ticks elapsed since unix epoch
var timestamp = timestamps.GetNow();

// target timezone
var timezone = TimeZoneInfo.FindSystemTimeZoneById( ... );

// creates a new zoned date time provider, with the desired timezone
var clock = new ZonedClock( timezone );

// returns the current date & time, in the clock's timezone
var datetime = clock.GetNow();

// adds a duration to datetime
// duration represents elapsed time in ticks
var a = datetime + Duration.FromHours( 2 );

// adds a period to datetime
// period represents elapsed time in calendar-related chronological units, such as years, months etc.
var b = datetime + Period.FromMonths( 1 ).AddWeeks( 2 ).AddMinutes( 420 );

// sets a time of day of datetime to 12:00:00
var c = datetime.SetTimeOfDay( TimeOfDay.Mid );

// extracts a zoned day instance from datetime
// there are also other zoned objects, such as year, month and week
var day = datetime.GetDay();

This project also includes implementations of caches, whose entries have a limited lifetime:

// creates a new timestamp provider
var timestamps = new TimestampProvider();

// timestamp of the creation of cache
var start = timestamps.GetNow();

// creates a new empty cache with 'string' key and 'int' value, with entry lifetime equal to '2 minutes'
var cache = new LifetimeCache<string, int>( startTimestamp: start, lifetime: Duration.FromMinutes( 2 ) );

// adds an entry to the cache, whose lifetime ends at 'start + 2 minutes'
cache.AddOrUpdate( "foo", 42 );

// moves the cache's current timestamp forward by '30 seconds', to 'start + 30 seconds' timestamp
// this means that the 'foo' entry has '1 minute and 30 seconds' left
cache.Move( Duration.FromSeconds( 30 ) );

// gets the value associated with 'foo' key, which should return 42
// this also resets entry's lifetime back to '2 minutes',
// which means that it will now be removed at 'start + 2 minutes and 30 seconds' timestamp
var foo = cache["foo"];

// moves the cache's current timestamp forward by '2 minutes', to 'start + 2 minutes and 30 seconds' timestamp
// this will also remove the 'foo' entry from cache
cache.Move( Duration.FromMinutes( 2 ) );

// checks whether or not the 'foo' entry exits, should return false
var exists = cache.ContainsKey( "foo" );

There also exists a version of lifetime cache that allows to set each entry's lifetime individually.

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on LfrlAnvil.Chrono:

Package Downloads
LfrlAnvil.Reactive.Chrono

This project contains a few functionalities related to timers and schedulers.

LfrlAnvil.Identifiers

This project contains a generator of sequential 64-bit identifiers based on a timestamp.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.1 136 6/16/2024
0.2.0 117 6/16/2024
0.1.1 123 5/29/2024
0.1.0 128 5/26/2024