PaulTechGuy.NetCore.TimeFilters 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package PaulTechGuy.NetCore.TimeFilters --version 1.0.1                
NuGet\Install-Package PaulTechGuy.NetCore.TimeFilters -Version 1.0.1                
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.1" />                
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.1                
#r "nuget: PaulTechGuy.NetCore.TimeFilters, 1.0.1"                
#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.1

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

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)) ...

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.0 - Initial version
  • 1.0.1
    • Allow 3-character abbreviations for weekdays
    • Change Within() method to virtual to allow derviced classes to override
    • Update unit test nuget packages
    • Update unit tests
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 335 12/10/2022
1.0.1 314 12/9/2022
1.0.0 357 5/15/2022