PaulTechGuy.NetCore.TimeFilters 1.0.2

dotnet add package PaulTechGuy.NetCore.TimeFilters --version 1.0.2
NuGet\Install-Package PaulTechGuy.NetCore.TimeFilters -Version 1.0.2
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="PaulTechGuy.NetCore.TimeFilters" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PaulTechGuy.NetCore.TimeFilters --version 1.0.2
#r "nuget: PaulTechGuy.NetCore.TimeFilters, 1.0.2"
#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 PaulTechGuy.NetCore.TimeFilters as a Cake Addin
#addin nuget:?package=PaulTechGuy.NetCore.TimeFilters&version=1.0.2

// Install PaulTechGuy.NetCore.TimeFilters as a Cake Tool
#tool nuget:?package=PaulTechGuy.NetCore.TimeFilters&version=1.0.2

PaulTechGuy.NetCore.TimeFilters

GitHub: https://github.com/paultechguy/NetCore.TimeFilters

Requirements

  • Windows 10 or higher
  • Visual Studio 2022 with .NET Core

WeekdayTimeRange

This .NET Core library provides the WeekdayTimeRange which provides support for filtering a DateTime object against one more day of week patterns. A day of week pattern contains a weekday or start/end time, or both. For example, if you want to create a day of week filter for Sunday, you could use the following source code:

WeekdayTimeRange range = new(DayOfWeek.Sunday);

There are several more ways to create a WeekdayTimeRange:

WeekdayTimeRange range = new(DayOfWeek.Sunday, new TimeSpan(1, 2, 3), new TimeSpan(13, 14, 15)); // Sunday 01:02:03 to 13:14:15
WeekdayTimeRange range = new("Sunday", "01:02:03", "13:14:15");

WeekdayTimeRange range = new(DayOfWeek.Sunday);
WeekdayTimeRange range = new("Sunday");
WeekdayTimeRange range = new("Sun"); // using 3-character abbreviations

WeekdayTimeRange range = new(new TimeSpan(1, 2, 3), new TimeSpan(13, 14, 15)); // 01:02:03 to 13:14:15
WeekdayTimeRange range = new("01:02:03", "13:14:15");

TryParse

You can use several TryParse method overides to verify if a text string is a valid WeekdayTimeRange:

bool isValid = WeekdayTimeRange.TryParse("Sunday", out var range) ...
bool isValid = WeekdayTimeRange.TryParse("Sun 01:02:03-13:14:15", out var range) ...
bool isValid = WeekdayTimeRange.TryParse("01:02:03-13:14:15", out var range) ...

Within

Once you have a filter, you can determine if a DateTime object is within the filter range:

if (range.Within(DateTime.Now)) ...

You can also determine if a collection of DateTime objects is within a filter range, and have the DateTime matches returned as part of the same method call:

// determine if a Friday WeekdayTimeRange matches a
// collection of DateTime objects
var range = new WeekdayTimeRange(DayOfWeek.Friday);
var dates = new List<DateTime>()
{
    // Friday matche(s)
    DateTime.Parse("12/9/2022"),
    DateTime.Parse("1/7/2022"),
    DateTime.Parse("8/11/1911"),

    // miss(es)
    DateTime.Parse("11/13/1915"), // saturday
    DateTime.Parse("7/8/1649"), // sunday
};

// returns true with matches count = 3
bool isValid = range.Within(dates, out var matches);

WeekdayTimeRangeCollection

You can determine if a DateTime object is within a collection of day of week filters using:

string[] patterns = new string[] {"Sunday","Monday 01:02:03-13:14:15", "23:00:00-23:15:00"};
if (WeekayTimeRange.TryParse(patterns, out WeekdayTimeRangeCollection rangeCollection))
{
    if (rangeCollection.Within(DateTime.Now)) ...
}

.editorConfig

The code has its own set of formatting and language rules. If you don't like these, feel free to modify the .editorConfig file, or remove it entirely from the project.

License

MIT

Inspiration

This class library was a valuable resource for a Windows Service that periodically monitors the computer CPU level. The service needs to bypass the CPU check during certain weekday and/or times of the day.

Version History

  • 1.0.2
    • Add additional Within() to match on a collection of DateTime and return matches
    • Added WeekdayTimeRangeCollection.WithinAll method along with unit tests.
    • Renamed WeekdayTimeRangeCollection.Within method to WithinAny
    • Added intellisense for public methods.
  • 1.0.1
    • Allow 3-character abbreviations for weekdays
    • Change Within() method to virtual to allow derived classes to override
    • Update unit test nuget packages
    • Update unit tests
  • 1.0.0 - Initial version
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 was computed.  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.
  • net6.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.

Version Downloads Last updated
1.0.2 290 12/10/2022
1.0.1 271 12/9/2022
1.0.0 323 5/15/2022