Evaluate complex time patterns

2020-04-26 13:56发布

问题:

I would like to define and evaluate ocurrences of some very complex time patterns that cannot be handled by CRON expressions easily. Is there any library that help me to do that?

For example:

  1. I would like it to occur every 25 seconds.
  2. I would like to occur only first and last day of month. But first day of month should gets me resolution of 5 minutes betwen 9:00AM and 11:00AM. The last day of month should evaluate to 5:00AM.
  3. I would like to create very complex time pattern that do something like this:

    On monday of first and third week of month gets time between 8:30AM and 11:30AM On tuesday and sunday of second and fourth week time at 12:00PM

Is it possible to express such requirements in some form of expression and evaluate what dates it fits? What should I use, where to find it?

回答1:

I am author of library that evaluates such expressions. It's domain specific language that looks a bit like SQL so lots of user should be familiar with it's syntax. For the asked expressions:

I would like it to occur every 25 seconds.

repeat every 25 seconds start at '29.10.2017 01:55:00'

I would like to occur only first and last day of month. But first day of month should gets me resolution of 5 minutes betwen 9:00AM and 11:00AM. The last day of month should evaluate to 5:00AM.

repeat every minutes where 1 = 
    (case 
        when GetDay() = 1 and GetHour() between 9 and 11
        then GetMinute() % 5 = 0
        when IsLastDayOfMonth()
        then GetHour() = 5 and GetMinute() = 0 and GetSecond() = 0 
        else 0
    esac) start at '01.01.2017'

On monday of first and third week of month gets time between 8:30AM and 11:30AM On tuesday and sunday of second and fourth week time at 12:00PM

repeat every minutes where 1 = 
   (case 
       when GetWeekOfMonth() in (1,3) and GetDayOfWeek() = monday
       then GetTime() between Time(8, 30, 0) and Time(11, 30, 0)
       when GetWeekOfMonth() in (2,4) and GetDayOfWeek() in (tuesday, sunday)
       then GetTime() = Time(12, 0, 0)
       else 0
    esac) start at '01.04.2017'

As worth to point here is that generally week of months can be calucated differently, there is no standard way of doing this so results may varing dependly on what strategy was chosen. GetWeekOfMonth(string type) have optional type parameter that changes strategy.

As it can be noted, queries that contains where part allows to apply sophisticated filters on current timeline. You simply write your filters like you would do in SQL but you will filter timeline, not datas.

There are few build-in functions to help faster designing new queries. It's also possible to develop other filter functions. See defaultly available in wiki. All those functions were written in pure C# and it should be easy to add custom functions. It's available on nuget. I would like this library to be usefull for community.

I made also cron evaluator that share abstractions so it is possible to use it interchangeably becouse sometimes it's much better to use cron instead.

https://github.com/Puchaczov/TQL.RDL