LightCalendar 1.1.0
dotnet add package LightCalendar --version 1.1.0
NuGet\Install-Package LightCalendar -Version 1.1.0
<PackageReference Include="LightCalendar" Version="1.1.0" />
paket add LightCalendar --version 1.1.0
#r "nuget: LightCalendar, 1.1.0"
// Install LightCalendar as a Cake Addin
#addin nuget:?package=LightCalendar&version=1.1.0
// Install LightCalendar as a Cake Tool
#tool nuget:?package=LightCalendar&version=1.1.0
lightCalendar
Lightweight .Net library for time intervals.
It is a frequent need for projects to have ability to operate time periods that span across several days. This library aims to fix the lack of standard means of doing that.
Period
The main building block of this library is Period struct. It has Start and End:
public readonly struct Period
{
public Period(DateTime begin, DateTime end);
public DateTime Begin { get; }
public DateTime End { get; }
}
You create Periods like this:
var p = new Period(new DateTime(2020, 01, 07), new DateTime(2020, 10, 17));
Or using utility function:
var currentMonth = Period.FromMonth(DateTime.UtcNow);
or
var aYearFromNow = Period.OneYearAhead(DateTime.UtcNow);
It is possible to get Days, Months, Years from a period, as well as overlap, intersect, exclude and combine them.
var now = DateTime.UtcNow;
var todayTomorrow = new Period(now, now.AddDays(1));
var yesterdayTomorrow = new Period(now.AddDays(-1), now.AddDays(1));
var overlap = yesterdayTomorrow.Overlap(todayTomorrow);
Console.WriteLine(overlap.GetDayCount()); // 2
var days = overlap.GetDailySchedule(); // [0] = {DateTime} "2/16/2020 2:34:50 PM"
// [1] = {DateTime} "2/17/2020 2:34:50 PM"
or check if a date falls whitin given period:
overlap.Contains(new DateTime(2006, 1, 1)); //false
or check if a period entirely falls into another period:
yesterdayTomorrow.Contains(todayTomorrow); // true
There are more examples in unit tests, please check them out.
Fiscal year
Another useful abstraction is FiscalYear. A fiscal year is a 12-month period that an organization uses to report its finances. It does not necesserily starts or ends with calendar year. Leap year is taken into account, so if a Fiscal year ends on February there will not be a jump to March on non-leap years. The most important fiscal year for the economy is the federal government's fiscal year. It defines the U.S. government's budget. It runs from October 1 of the budget's prior year through September 30 of the year being described.
// Create currently running Federal Fiscal Year
var federalFiscalYear = FiscalYear.GetFiscalYear(new DateTime(2000,09,30), DateTime.UtcNow);
Console.WriteLine(federalFiscalYear); // 10/1/2019 - 9/30/2020
Because fiscal year is a Period, you can do all usual stuff with it: overlap, intersect, exclude and combine with other Periods. Refer to unit tests in this project for more ideas.
Weeks
Version 1.1.0 adds weeks creator. Weeks can start on different week days depending on standard. You can create several week factories and use them (e.g. via IoC injection) dynamically to create Period for any date's running week.
// Create factories for week period that differ by week beginning day
var weekCreator = Period.FromWeekFactory(DayOfWeek.Monday);
var weekCreator2 = Period.FromWeekFactory(DayOfWeek.Sunday);
var weekCreator3 = Period.FromWeekFactory(DayOfWeek.Saturday);
var currentIsoWeek = weekCreator(DateTime.UtcNow);
var currentAmericanWeek = weekCreator2(DateTime.UtcNow);
var currentMiddleEasternWeek = weekCreator3(DateTime.UtcNow);
Console.WriteLine(currentIsoWeek); // 03/02/2020 - 03/08/2020
Console.WriteLine(currentIsoWeek.Begin.DayOfWeek); // Monday
Console.WriteLine(currentIsoWeek.GetDayCount()); // 7
Console.WriteLine(currentIsoWeek.GetDailySchedule());
//[0] = {DateTime} "03/02/2020 5:12:50 PM"
//[1] = {DateTime} "03/03/2020 5:12:50 PM"
//[2] = {DateTime} "03/04/2020 5:12:50 PM"
//[3] = {DateTime} "03/05/2020 5:12:50 PM"
//[4] = {DateTime} "03/06/2020 5:12:50 PM"
//[5] = {DateTime} "03/07/2020 5:12:50 PM"
//[6] = {DateTime} "03/08/2020 5:12:50 PM"
Console.WriteLine(currentAmericanWeek.GetDailySchedule());
//[0] = {DateTime} "03/01/2020 5:20:38 PM"
//[1] = {DateTime} "03/02/2020 5:20:38 PM"
//[2] = {DateTime} "03/03/2020 5:20:38 PM"
//[3] = {DateTime} "03/04/2020 5:20:38 PM"
//[4] = {DateTime} "03/05/2020 5:20:38 PM"
//[5] = {DateTime} "03/06/2020 5:20:38 PM"
//[6] = {DateTime} "03/07/2020 5:20:38 PM"
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.0
- System.Collections.Immutable (>= 1.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.