aweXpect.Chronology
1.3.0
Prefix Reserved
dotnet add package aweXpect.Chronology --version 1.3.0
NuGet\Install-Package aweXpect.Chronology -Version 1.3.0
<PackageReference Include="aweXpect.Chronology" Version="1.3.0" />
<PackageVersion Include="aweXpect.Chronology" Version="1.3.0" />
<PackageReference Include="aweXpect.Chronology" />
paket add aweXpect.Chronology --version 1.3.0
#r "nuget: aweXpect.Chronology, 1.3.0"
#:package aweXpect.Chronology@1.3.0
#addin nuget:?package=aweXpect.Chronology&version=1.3.0
#tool nuget:?package=aweXpect.Chronology&version=1.3.0
aweXpect.Chronology
Extension methods for creating chronology objects like TimeSpan or DateTime that read more naturally.
The full documentation is available at docs.testably.org.
Installation
dotnet add package aweXpect.Chronology
Supported target frameworks: .NET Standard 2.0, .NET 8, .NET 10. The DateOnly and TimeOnly conversions require .NET 8 or later.
The extension methods are in the aweXpect.Chronology namespace:
using aweXpect.Chronology;
At a glance
Without aweXpect.Chronology:
TimeSpan timeout = TimeSpan.FromMinutes(1) + TimeSpan.FromSeconds(30);
DateTime start = new DateTime(2024, 12, 24, 18, 30, 0, DateTimeKind.Utc);
DateTimeOffset end = new DateTimeOffset(2024, 12, 25, 6, 0, 0, TimeSpan.FromHours(2));
DateTime reminder = start - TimeSpan.FromHours(3);
With aweXpect.Chronology:
TimeSpan timeout = 1.Minutes(30.Seconds());
DateTime start = 24.December(2024).At(18, 30).AsUtc();
DateTimeOffset end = 25.December(2024).At(6, 0).WithOffset(2.Hours());
DateTime reminder = 3.Hours().Before(start);
The methods return lightweight builders that convert implicitly to the .NET types, so you can pass them wherever a TimeSpan, DateTime or DateTimeOffset is expected. ToString() returns the value as text, so string interpolation and the debugger show e.g. 00:05:00 for 5.Minutes().
Overview
| Feature | Methods | Example |
|---|---|---|
| Units | Ticks … Weeks |
5.Minutes() |
| Combine values | the same methods with a TimeSpan argument |
1.Minutes(30.Seconds()) |
| Arithmetic | +, -, * (by double), / (by double), unary - |
5.Minutes() * 2 |
| Comparison | ==, !=, <, <=, >, >= |
5.Minutes() > 30.Seconds() |
| Dates | January … December |
24.December(2024) |
| Time of day | At(hours, minutes, [seconds], [milliseconds]), At(TimeSpan) |
24.December(2024).At(18, 30) |
| Kind | AsUtc, AsLocal, ToLocalTime, ToUniversalTime |
24.December(2024).At(18, 30).AsUtc() |
| Relative dates | Before, After |
3.Hours().Before(25.December(2024)) |
| Add and subtract | +, - with a TimeSpan |
24.December(2024) + 18.Hours() |
| Compare dates | ==, !=, <, <=, >, >= |
24.December(2024) < 25.December(2024) |
| DateTimeOffset | WithOffset(TimeSpan) |
24.December(2024).WithOffset(2.Hours()) |
| DateOnly and TimeOnly | implicit conversions | DateOnly date = 24.December(2024); |
TimeSpan
Units
The package adds the extension methods Ticks(), Microseconds(), Milliseconds(), Seconds(), Minutes(), Hours(), Days() and Weeks() on int and double (Ticks() on int and long):
TimeSpan timeout = 10.Seconds();
TimeSpan duration = 1.5.Hours();
TimeSpan retention = 2.Weeks();
TimeSpan latency = 250.Microseconds();
Combine values
Each method has an overload that adds another TimeSpan, so you can write a value with several units:
TimeSpan timeout = 1.Minutes(30.Seconds()); // 00:01:30
TimeSpan period = 2.Days(12.Hours()); // 2.12:00:00
Arithmetic
The result supports the operators +, -, * (by double, in either order), / (by double) and unary negation:
TimeSpan doubled = 5.Minutes() * 2;
TimeSpan tripled = 3 * 5.Minutes();
TimeSpan half = 5.Minutes() / 2;
TimeSpan combined = 1.Minutes() + 30.Seconds();
TimeSpan negative = -10.Seconds();
Comparison
Two durations can be compared with ==, !=, <, <=, > and >=:
bool isLonger = 5.Minutes() > 30.Seconds(); // true
Dates and times
Dates
The package adds an extension method on int for each month, which takes the day and the year:
DateTime christmasEve = 24.December(2024);
The date has the time 00:00 and the DateTimeKind Unspecified. Set the kind explicitly when the value is compared with UTC or local times.
Time of day
At sets the time of day, either with explicit components or with a TimeSpan:
DateTime time = 24.December(2024).At(18, 30);
DateTime preciseTime = 24.December(2024).At(18, 30, 45, 500);
DateTime fromTimeSpan = 24.December(2024).At(18.Hours(30.Minutes()));
At also works on an existing DateTime or DateOnly and replaces its time of day.
A component outside its range, e.g. At(24, 0) or At(18, 60), and a TimeSpan outside a single day, e.g. At(24.Hours()) or At(-1.Hours()), throw an ArgumentOutOfRangeException.
Kind
AsUtc() and AsLocal() set the DateTimeKind of a date with a time of day or of an existing DateTime:
DateTime utc = 24.December(2024).At(18, 30).AsUtc();
DateTime local = 24.December(2024).At(18, 30).AsLocal();
A date without a time has no AsUtc() or AsLocal(). Add the time explicitly, e.g. 24.December(2024).At(0, 0).AsUtc() for midnight UTC.
ToLocalTime() and ToUniversalTime() convert a date with a time of day to the local time zone or to UTC, like the DateTime methods with the same name. Use them when the system under test converts the value the same way:
DateTime expected = 1.January(1601).At(0, 0).AsUtc().ToLocalTime();
Caution: AsUtc() and AsLocal() keep the clock time and only set the kind, while ToLocalTime() and ToUniversalTime() change the clock time, so the result depends on the time zone of the machine. Set the kind first: like DateTime, ToLocalTime() treats an Unspecified value as UTC and ToUniversalTime() treats it as local time.
Relative dates
Before and After on a TimeSpan express a date relative to another one:
DateTime earlier = 3.Hours().Before(25.December(2024));
DateTime later = 3.Hours().After(25.December(2024));
With a DateTimeOffset, the result keeps its offset:
DateTimeOffset earlier = 3.Hours().Before(25.December(2024).At(12, 0).WithOffset(2.Hours())); // 09:00 +02:00
Add and subtract
You can add a TimeSpan to a date or subtract one from it:
DateTime evening = 24.December(2024) + 18.Hours();
DateTime dayBefore = 24.December(2024) - 1.Days();
Compare dates
Two dates, or two dates with a time of day, can be compared with ==, !=, <, <=, > and >=. Like DateTime, the comparison ignores the kind:
bool isEarlier = 24.December(2024) < 25.December(2024); // true
DateTimeOffset
WithOffset attaches a UTC offset to a date, a date with a time of day or an existing DateTime:
DateTimeOffset date = 24.December(2024).WithOffset(1.Hours());
DateTimeOffset time = 24.December(2024).At(14, 30).WithOffset(2.Hours());
Caution: For a DateTime with the kind Utc or Local, the offset has to match: 24.December(2024).At(14, 30).AsUtc().WithOffset(2.Hours()) throws an ArgumentException. Use an Unspecified value, as in the examples above, or the matching offset.
The result also supports + and - with a TimeSpan, and ==, !=, <, <=, > and >= with another result. Like DateTimeOffset, the comparison uses the point in time, so 24.December(2024).At(14, 30).WithOffset(2.Hours()) == 24.December(2024).At(12, 30).WithOffset(0.Hours()) is true. When you assign it to a DateTime, it keeps the clock time and drops the offset.
DateOnly and TimeOnly
On .NET 8 and later, the results convert implicitly to DateOnly and TimeOnly:
DateOnly date = 24.December(2024);
TimeOnly time = 18.Hours(30.Minutes());
TimeOnly timeOfDay = 24.December(2024).At(18, 30);
A date converts to DateOnly, a duration to TimeOnly, and a date with a time of day or an offset to both.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net10.0
- No dependencies.
-
net8.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.