OpenHours 1.0.2

dotnet add package OpenHours --version 1.0.2
                    
NuGet\Install-Package OpenHours -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="OpenHours" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenHours" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="OpenHours" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenHours --version 1.0.2
                    
#r "nuget: OpenHours, 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.
#:package OpenHours@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenHours&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=OpenHours&version=1.0.2
                    
Install as a Cake Tool

๐Ÿ•’ OpeningHours

OpeningHours is a lightweight .NET library for evaluating whether a specific DateTime falls within defined business hours. It supports a rich, human-readable pattern syntax for specifying weekly schedules, date-based rules, time ranges, and exclusions.


โœจ Features

  • โœ… Define business hours using intuitive patterns
  • ๐Ÿ“… Support for weekdays, specific dates, and nth weekday of the month (e.g., Fr[2])
  • โฐ Time range support including overnight spans (e.g., 23:00-02:00)
  • ๐Ÿšซ Exclusion rules override inclusions (e.g., We off)
  • ๐Ÿงช Thoroughly tested with edge cases and complex combinations

๐Ÿ“˜ Pattern Syntax

Patterns are composed of inclusions and exclusions, separated by semicolons:

Mo-Th 08:00-20:00;We off;Aug 20;Aug 21 off

๐Ÿงฉ Components

Syntax Description
Mo, Tu, We, Th, Fr, Sa, Su Weekdays
08:00-20:00 Time range (24h format)
Fr[2] Nth weekday of the month (e.g., 2nd Friday)
Aug 20 Specific calendar date
Aug 20 off Exclude a specific date
Mo off Exclude a weekday
00:00-24:00 Full-day availability
23:00-02:00 Overnight span
Jan 01-04 off Exclude a date range

โ„น๏ธ Exclusions always take precedence over inclusions.


๐Ÿ” Usage

bool isOpen = OpeningHours.IsNowWithinBusinessHours(
    "Mo-Fr 09:00-17:00;We off;Dec 25 off",
    DateTime.Parse("2025-12-25 10:00")
);
// Returns false (Christmas is excluded)

โœ… Test Coverage

The library is validated with extensive NUnit test cases, including:

  • โœ”๏ธ Basic weekday and time range checks
  • โŒ Excluded weekdays and dates
  • ๐Ÿ“† Specific dates and date ranges
  • ๐Ÿง  Priority resolution (exclusions override inclusions)
  • ๐Ÿ•› Midnight and overnight spans
  • ๐Ÿ“… Nth weekday logic (e.g., Mo[1], Fr[2])
  • ๐ŸŒ Time zone-sensitive evaluations

Example test case:

[TestCase("Mo-Th 08:00-20:00;We off", "2025-01-15 09:00", false)]
[TestCase("Fr[2] 08:00-10:00;Fr[3] off", "2025-10-10 08:30", true)]
[TestCase("Aug 20;Aug 20 off", "2025-08-20", false)]

๐Ÿงช Running Tests

Tests are written using NUnit. To run them:

dotnet test

๐Ÿง  Inspiration: opening_hours.js

This library was inspired by the excellent opening_hours.js project, which provides a robust parser and evaluator for the opening_hours tag used in OpenStreetMap (OSM). That tag encodes complex business hour patterns for facilities like shops, restaurants, and public services. Also you can evaluate your pattern here


๐Ÿ“„ License

MIT License. See LICENSE for details.


Product 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 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.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.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 263 11/12/2025
1.0.1 265 11/10/2025
1.0.0 259 11/10/2025