-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for reverse or negative occurrences? #57
Comments
Negative values are not supported. You can specify constraints using
to signify the last day of a month (which could be the 28th, 29th, 30th, or 31st). Otherwise you need to specify a positive value. If you really need negative values, you could create a custom modifier (see http://bunkat.github.io/later/modifiers.html#custom) that would convert your negative values to positive values and pass those onto the standard constraints. Here is the example of a custom modifier to support a negative values. I haven't tested this at all, but this should give you the basic idea of what you would need to do. This is also assuming that the rest of the code works with negative numbers which it may or may not since I've never tried.
The idea is to modify the extent such that both negative and positive numbers are supported, modify the isValid function to check against the correct value (which is max - supplied), and modify the next and prev functions in the same manner as the isValid function. |
I'm doing some testing with your suggestion, and so far so good, except for a little detail around timezones that I don't understand. Why would the second last day of January be reported as "Thu Jan 30 2014 19:00:00 GMT-0500 (EST)"? If I specify But I don't understand the effect of timezone here - regardless of what timezone I'm in, if I ask for the second last day of January, should it not always be the 30th? |
It should definitely be giving you Jan 30th at midnight in your desired timezone. If it is giving you Jan 31st, then I would guess the value being passed into |
Here's what I've done with the code you provided, followed by a test case:
This test will pass, however the output of
Do I just have a misunderstanding of how a Date with timezone works? For example, if the test is passing, then the correct date is being returned. However, that correct date (Jan. 31st at midnight), displayed in my timezone would be Jan 30th at 7pm. I guess I just don't understand why console.log() is converting to my timezone - just a javascript behaviour? |
Yes, |
Could you suggest a behaviour for next() and prev() if the absolute value of the given value is greater than the number of days in a month? For example, in later.day.next(), if the given value is greater than the number of days in the following month, it returns the first day of the following month. (I'm curious why you chose to do that, too, rather than returning the next month where value is actually valid. Like in January, skipping February for March when val = 31.) I'm still not entirely familiar with the expected behaviour of later - can you give me an idea of what you might expect from the following: I would think return the first day of the following month, it's just that I'm getting failures with this configuration in the test runner:
I realize it may not be possible to use the test runner since this modifier creates some unique scenarios. |
I would suggest that you return the last second of the last day of the previous month if the value provided is greater than the number of days in the month. This is for the same reason that Imagine if you had a constraint that said If we skipped to the next month where 30 was valid, we would end up at March 30th. However, according to the constraints, days 5, 15, 20, and 25 were also valid. We've missed an occurrence on March 5th. To avoid this problem, the constraint returns the first day of the following month (the first time a valid occurrence could possibly occur) and the search for the next valid occurrence begins from there. If your constraint only said |
I have similar question. Is it possible to specify last weekday of each month? Do I have to use modifier as well or is there any easier way? |
Sure, you just need to think at about it a little creatively. If you want the last weekday of each month, it basically means the following:
The second is a little bit tricky to encode, but the way I thought about it was to just always take the last Friday of the month if it occurs at the very end of the month. For example, the last Friday of March 2015 is the 27th, but since Tuesday the 31st is the last weekday, we don't want to include that occurrence. Basically for months with 31 days, we only want Friday if it is either the 30th (the 31st was a Saturday) or the 29th (the 31st was a Sunday). We can then do something similar with months with 30 days (we only want the 28 or 29th). February is similar, but of course, leap year messes everything up so you would need to create additional exceptions to handle the leap years if that's important to you.
There might be a smarter way to do it, this is just the first way that I thought of. |
I am asking since I am new to this very interesting api. I was wondering about the concept of incrementally narrowing data set. Eg
|
That's not how Later works. With Later, all of the constraints are completely independent from one another. The Weekday constraint specifies which day of the week you want. Last in that case means Saturday since that is the last day of the week. It doesn't know anything about days of the month or weeks of the month, so Last can't mean anything else and still make sense. You could create a custom time period that simply calculates the last weekday of a month and uses just a flag (1 for last weekday, 0 for don't care). Assume you create this time period and called it 'lwdm' for last weekday in a month or something, then you could just do:
If you haven't seen it yet, I show an example at http://bunkat.github.io/later/time-periods.html#custom that walks through splitting up a day. The one to calculate the last weekday of the month would be similar. |
Is it possible to specify a recurrence with negative values?
For example, the 2nd last Tuesday of the month?
Or something like recur().on(-5).dayOfMonth() for the fifth last day of the month, recur().on(-101).dayOfYear(), or recur().on(-3).weekOfYear()?
The text was updated successfully, but these errors were encountered: