gavynriebau / dates.recurring Goto Github PK
View Code? Open in Web Editor NEWEasily calculate the next recurrence for recurring dates using a fluent syntax
License: MIT License
Easily calculate the next recurrence for recurring dates using a fluent syntax
License: MIT License
I am trying to get the Last Monday of every month.
IRecurring rule;
rule = Recurs.Starting(new DateTime(2023,1,1))
.Every(1)
.Months()
.OnOrdinalWeek(Ordinal.LAST).OnDay(DayOfWeek.Monday)
.Ending(new DateTime(2023,6,15))
.Build();
DateTime dt = new DateTime(2023, 1, 1);
while (rule.Next(dt).HasValue)
{
dt = rule.Next(dt).Value;
result.Add(dt);
}
the result was supposed to give me 5 different dates. but every time rule.Next() is executed it returns the same date that is provided as the parameter. ends up as an infinite loop.
If I made a NET Core version, would you be happy for a pull request, to make a NET Core branch?
I was wondering if you have a function for finding the last working day of each month.
I was thinking that using the OnDay with a bitwise expression would work but this does not seem to be returning the correct information
nextPaymentRun = Dates.Recurring.Recurs.Starting(FirstDate) .Every(Interval) .Months() .OnOrdinalWeek(Dates.Recurring.Ordinal.FIRST) .OnDay(DayOfWeek.Monday | DayOfWeek.Tuesday | DayOfWeek.Wednesday | DayOfWeek.Thursday | DayOfWeek.Friday) .Build() .Next(NextDate);
Is there any plans to have a new nuget release created from master?
This test (I made) fails, but should pass I think
[Fact]
public void Daily_Every_Saturday_First()
{
// Arrange.
IRecurring daily = Recurs
.Starting(new DateTime(2019, 1, 16)) //Starting on a wednesday
.Every(1)
.Days()
.Including(Day.SATURDAY) //Only on saturdays
.Ending(new DateTime(2019, 5, 15))
.Build();
// Act.
List<DateTime> dates = new List<DateTime>();
DateTime? date = new DateTime(2019, 1, 1); //Date earlier, could be from somewhere else
while (date.HasValue)
{
date = daily.Next(date.Value);
if (!date.HasValue)
{
break;
}
dates.Add(date.Value);
}
Assert.Equal(new DateTime(2019, 1, 19), dates.First()); //First occurrence in a every saturday schedule should be a saturday
}
Hi,
I'm trying get a recurrence rule like "Last day of every month" but couldn't figure out if it's not supported or I'm missing something.
Can you please confirm if it's possible to include last day of every month in recurrence?
Thanks
Are there any plans to continue to develop \accept pull requests on this repository?
Small suggestion to have a method to retrieve all dates,
Currently I'm doing this, not sure if it's the best
`// Weekly recurrences
var weekly = Recurs
.Starting(request.DateStart.DateTime)
.Every(1)
.Weeks()
.FirstDayOfWeek(DayOfWeek.Monday)
.OnDays(Day.WEDNESDAY) //TODO Move to a options object/model
.Ending(request.DateEnd.DateTime)
.Build();
List dates = new();
DateTime? prevData = request.DateStart;
while ((prevData = weekly.Next(prevData.Value)).HasValue)
{
dates.Add(prevData.Value);
}
return dates;`
Currently IRecurring.Previous()
will return null if the result would be before the date passed to Starting()
; would it be possible to add an option for this restriction to be lifted? My use case is that I need a schedule that is every other Thursday, so I use the start date to indicate which Thursdays should be included, but I'll still need Thursdays that are arbitrarily in the past.
Humanizer updated their repository and released version 2.0 which restructured their project. Humanizer.Core now suffices as a library for your project and doesn't include all of the localization .dlls.
I can make a pull request to update this project to reference the updated Humanizr nuget package if you would be willing to update your Nuget package.
Thanks!
Edit --
Here is my pull request #2
Is Next() date the only option, how can we return all dates in the recurrence?
First of all, awesome library. I've been asked to implement Outlook-style recurring calendar events into a project and Dates.Recurring can do all of the permutations required except one. We need events to occur every "x" days (which you support) with the option to include or exclude weekends. I started looking at your "Daily.cs" file with the intent of either modifying it or cloning it and creating a "WeekDay.cs" option. Does this sound like a reasonable approach? Or, do you have another suggestion to support the "exclude weekends" feature that I need?
The DaysBuilder has Including() which takes one or more days, The WeekBuilder has OnDays() taking the same parameter. Both are optional. I like to have something like that for the MonthBuilder as well. Since OnDays better conveys the meaning I suggest doing OnDays() there as well so it aligns with the WeeksBuilder.
Apart from that it would be nice to have OnOrdinalWeek() and OnDay() working the same way as Including/OnDays(), so, optional, and making the parameters flags.
I dont know why this is different than days/months because having a 1st & 2nd week of a month does not equal having a 3rd week like the test has now.
So for non-breaking changes I suggest:
Then a breaking change would be:
Would you accept a PR for this, and if so is it ok to do the breaking changes?
Hey :)
I just noticed that the calculation of the last day differs for each period when the last day matches the endDate. For the daily/weekly period, the last day is included in the calculation if it is the same as the endDate. However, for the monthly/yearly period, it is not included. Is there a reason for this difference, or is it a bug?
In the following example I used the weekly calculation and the endDate is the 29.01.2024 and the last calculated date (Monday) is also the 29.01.2024. In the second example I used the monthly calculation and the last calculated day also matches the endDate but is not included in the result.
I would expect the endDate to always be included in the result or at least the same behaviour for all periods.
Appreciate your feedback.
// First example
var dates = new List<DateTime>();
var startDate = new DateTime(2024, 1, 1);
var endDate = new DateTime(2024, 1, 29);
var weekly = Recurs
.Starting(startDate)
.Every(1)
.Weeks()
.OnDay(DayOfWeek.Monday)
.Ending(endDate)
.Build();
DateTime? nextDate = DateTime.MinValue;
while (nextDate.HasValue && nextDate <= endDate)
{
if (nextDate != DateTime.MinValue) dates.Add(nextDate.Value);
nextDate = weekly.Next(nextDate.Value);
}
// Second example
var dates = new List<DateTime>();
var startDate = new DateTime(2024, 1, 1);
var endDate = new DateTime(2024, 3, 1);
var monthly = Recurs
.Starting(startDate)
.Every(1)
.Months()
.OnDay(1)
.Ending(endDate)
.Build();
DateTime? nextDate = DateTime.MinValue;
while (nextDate.HasValue && nextDate <= endDate)
{
if (nextDate != DateTime.MinValue) dates.Add(nextDate.Value);
nextDate = monthly.Next(nextDate.Value);
}
I dont know if I'm missing something, but if I want to have a list of dates from a pattern I do this:
public List<DateTime> DatesRecurringListContains()
{
var r = Recurs
.Starting(new DateTime(2017, 1, 1))
.Every(2)
.Days()
.Ending(new DateTime(2017, 4, 30))
.Build();
var f = r.Next(new DateTime(2017, 1, 1).AddDays(-1));
if (f.HasValue)
{
var rl = new List<DateTime> { f.Value };
while (f != null)
{
f = r.Next(f.Value);
if (f != null) rl.Add(f.Value);
}
if (r.Next(new DateTime(2017, 4, 28)) == new DateTime(2017, 4, 29))
{
return rl;
}
}
return null;
}
But this is bad for performance, compared to some alternatives:
Host Process Environment Information:
BenchmarkDotNet.Core=v0.9.9.0
OS=Microsoft Windows NT 6.2.9200.0
Processor=Intel(R) Core(TM) i5-4590 CPU 3.30GHz, ProcessorCount=4
Frequency=3215221 ticks, Resolution=311.0206 ns, Timer=TSC
CLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE
GC=Concurrent Workstation
JitModules=clrjit-v4.6.1080.0
Type=Benches Mode=Throughput
Method | Median | StdDev |
---|---|---|
DatesRecurringListContains | 159.6398 us | 1.2346 us |
DarecaliListContains | 3.0112 us | 0.1232 us |
Is there a smarter way to get a list of the recurring dates?
Hi,
At one time this library used multi-targetting to support both .Net standard 2.1 as 2.0
I see that this has been changed so supposedly add support to .Net 5.
I'm no expert on the matter but if I understand correctly there is no reason this codebase can't be compiled against multiple target frameworks.
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
This seems to build just fine.
For the project i'm currently working on we are stuck on .Net 4.7 so targetting netstandard2.0 would mean we could use the lastest version.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.