RRULE for every other week except last week of month - icalendar

I need an RRULE for every other Saturday except the last week of the month. I tried to create one with a weekly frequency, but didn't know how to apply an exception for the last week of the month:
RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=SA
I also tried creating a rule with a monthly frequency, which allowed me to skip the last week of the month, but I didn't know how to make it every other week:
RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=SA;BYSETPOS=-2,-3,-4,-5
I looked through the specification, but I'm not seeing anything that makes this possible.

The secret to the solution here is that BYMONTHDAY can be negative like BYSETPOS. This allows one to exclude the last 7 days of each month even though the number of days in the month varies. Your DTSTART should be on a SATURDAY, so one doesn't really need the BYDAY=SA
This rrule works in google calendar (if DTSTART is on Saturday and generally for any every 2nd week but not the last week rule for any day of week specified by the DTSTART):
RRULE:FREQ=WEEKLY;INTERVAL=2;BYMONTHDAY=-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20,-21,-22,-23,-24,-25,-26,-27,-28,-29,-30,-31
See test calendar:
https://calendar.google.com/calendar/u/0?cid=ZXBwdWE4N2RwZm5xODVic3JydDJzaXFsY3NAZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ
Spec: https://datatracker.ietf.org/doc/html/rfc2445#page-43
and
https://icalevents.com/2447-need-to-know-the-possible-combinations-for-repeating-dates-an-ical-cheatsheet/ for the expansion & contraction (limiting) rules & combinations

Related

iCalendar repeat rule for x days before the 2nd Tuesday in the month

I have a meeting on every 2nd Tuesday of the month.
I need 10 days to prepare that meeting - this means I would need a recurring event "10 days before the 2nd Tuesday of the month"
(added via ical file)
I found iCalendar repeat rule for "the day after the second monday of the month" but I'm not sure how/if this applies to my situation/how to remodel it to fit my needs.
The issue is that since it's 10 days prior it may even be in the previous month in some cases (e.g. if the first day of the month is a Tuesday)
What is the correct command for this?
EDIT: the time is always from 8-10am just so it can be included.

RRULE for the weekend including the first sunday of a month

We have a event every year on the weekend (Fr-Su) that includes the first Sunday of June. How would I create a iCalendar Event that expresses these three days (whole day events)?
Creating a rule for the first Sunday is easy. But for the Saturday and Friday, I did not succeed to create a rule that counts backwards (RFC 5545 says INTERVAL, COUNT must be positive]. Moreover I could not think of a different expression that would start from the Friday - it could be the last Friday of May, but also the first in June.
The RRRULE specification in RFC 5545 is lacking in this regard. The INTERVAL and COUNT values are for the repeating events, not the event itself. I've come across a similar issue when trying to define the USA day "Black Friday", the day after the 4th Thursday in November (Friday after Thanksgiving). The 4th Friday in November could occur the day after the 4th Thursday, or the prior week. There is not a way that I have found to make a RRULE for this situation.
I believe you will need to code the events individually instead of using a recurring rule.
RRULE:FREQ=YEARLY;BYDAY=FR;BYMONTH=5,6;BYSETPOS=2;BYMONTHDAY=-2,-1,1,2,3,4,5,6,7' seems to do the trick.
How can I write an ICS file for the Friday before the first Saturday of the month? led me to the right track: with 'BYMONTHDAY' I can count backwards from the end of the month.
The Friday before the first Sunday of the next month can be the last or the second to last day of the previous month, or up to the 5th day of the month. If I include May and June, I will get a set that includes the day. 'BYSETPOS' allows me to choose the second of the found Fridays. To always have the second in the set being my desired day, I include the 6th and 7th day of the month, which gives me a stable first Friday in May. Possibly matched additional Fridays in June are discarded by 'BYSETPOS' anyway.
Extending this to Saturday is simple, and the first Sunday of June is trivial.
I developed the rule with rrule.js
https://jakubroztocil.github.io/rrule/#/rfc/RRULE:BYDAY=FR;BYMONTH=5,6;BYSETPOS=2;BYMONTHDAY=-2,-1,1,2,3,4,5,6,7

Recurring calendar event on First of the month

I have a recurring calendar event on 1st of each month, when 1st falls on Sat/Sun is it possible for iCal to schedule it for last working day Fri?
If I understood you correctly the following RRULE should do the trick:
FREQ=MONTHLY;BYDAY=1MO,1TU,1WE,1TH,1FR,-1FR;BYMONTHDAY=1,-1,-2
See the next 100 instances
It basically does two things:
Iterate the first weekday that falls on the 1st day of the month
Iterate the last Friday of each month that falls on the last or 2nd last day of the month (in which cases the 1st of the next month falls on Saturday or Sunday).
A slightly shorter version that should yield the same results is
FREQ=MONTHLY;BYDAY=1MO,1TU,1WE,1TH,FR;BYMONTHDAY=1,-1,-2
This just iterates every Friday, but only keeps the ones that fall on the first, last and 2nd last day of each month. Though it looks like the recurrence expansion service above disagrees. That's probably a bug.
I'm assuming you mean "the last working day Friday of the previous month", in which case I don't think it possible. It would mean that some months have 2 occurrences when others have 0, which doesn't really work.
You could easily do the first working day of the month (the 1st, or the first Monday):
FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1
Or, you could do always the last working day of the month:
FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1

ical RRULE for "second week of January"?

What ical RRULE lets me choose the second (not necessarily full) week of January (for example)?
More specifically, the Sunday that starts the second week of January?
Usually, "1SU" would work, but if the month starts with Sunday itself, it would be "2SU", so I don't see an obvious solution here?
If you want the Sunday that starts the second week in January, you have to say that that Sunday can be any month day except the first (and obviously, only can be below the 8th day of the month).
The way to do this is to specify a RRULE with FREQ=YEARLY;BYMONTH=1 to specify January, BYDAY=SU to specify Sundays, BYMONTHDAY=2,3,4,5,6,7,8 to specify that your Sunday cannot be the 1st of the month and that you are only interested in the one starting the 2nd week of the month.
To summarize:
RRULE:FREQ=YEARLY;BYMONTH=1;BYDAY=SU;BYMONTHDAY=2,3,4,5,6,7,8

Is there a Haskell library for dates?

Is there a function in Haskell that will allow me to enter component of a date (like a string representation or day month year components) that I can get information from (like day of week, days in a month, etc.)?
I've looked online and it looks like there are a lot of custom libraries, but I'm hoping there's one in the standard prelude library of ghci 10.6.4 that's just not well documented?
Are Data.Time.Calendar and Data.Time.Format in the time library sufficient?
You can parse a string representation of a date and get the length of a month using gregorianMonthLength. Not sure about day of the week, though you could format the date as a string using a format that just displays the week day.
A quick Google search turns up this, which may be what you want. It lets you parse strings representing dates and extract information from them.
You can find the day of the week with mondayStartWeek or sundayStartWeek, depending on whether you think a week starts on Monday, or on Sunday. Both functions are in Data.Time.Calendar.OrdinalDate.
λ> snd $ mondayStartWeek $ fromGregorian 2017 10 3
2
In the above example, the return value is 2, which indicates the second day of the week. Since the function is called mondayStartWeek, Monday is the first day, so 2 corresponds to Tuesday. This is true of October 3, 2017.
A warning regarding week numbers
Both functions return a tuple, where the second element is the week day. As far as I can tell, that should be trustworthy.
The first element, however, is the week number of the year. Be careful with that, because the rules for week numbering are political. If I remember correctly, in USA, week 1 is the week that contains January 1. That's not the case in Denmark, where I live. Here, week 1 is the first week where Thursday falls in the new year. This can mean that December 31 can fall in week 1 of the next year. IIRC, this is the rule for many other European countries. Some years, the American and the European week numbers align, but some years, they don't.