How do I express coming Monday in iso8601? - iso8601

While answering Regexp matching ISO8601 date/time format I wondered how you express first coming Monday or repeat every monday in ISO8601.
My guess would be something like:
W-1
R/W-1
But I cannot find a confirmation of that.

ISO 8601 has the concept of week dates which allow you to specify a week of the year and the day of the year. In the following example 2014-W01-1 means the first week of 2014 on Monday. Then repeat that every week using P1W
R/2014-W01-1T00:00:00/P1W
Seen in this answer

Related

Find last day of the previous week in Teradata?

I want to find out the previous weeks's last day in Teradata SQL using Sunday as the last day of the week. For example, today is Friday 1/27, so the last week ended on Sunday (1/22) and I would want to return 2023-01-22.
Other examples:
If current date is '2023-01-02', then the output I require is '2023-01-01'
If current date is '2023-01-18', then the output I require is '2023-01-15'
With Hive query I would use:
date_sub(current_date, cast(date_format(current_date, 'u') as int));
What would the equivalent be in Teradata? I've tried using the code below but it seems to return the date of the closest Sunday instead of the date of the previous Sunday.
SELECT ROUND(current_date, 'd') (FORMAT 'yyyy-mm-dd');
There are several ways:
Probably the best one is one of the built-in functions to return the previous xxxday <= the input date:
Td_Sunday(Current_Date - 1)
Or the function to return the next xxxday > input date:
Next_Day(Current_Date - 8, 'sun')
Truncating is least understandable:
Trunc(Current_Date, 'IW') -1
TRUNC supports three variations, only IW is usable, but restricted to Monday as week start:
IW: the Monday of the ISO week
WW: the same day of the week as January 1st of the year
W: the same day of the week as the first day of the month
You can use the trunc function to return the first day of the a week, month, ect.
select trunc(current_date -7 ,'IW')
Current date today is 2023-01-27. This will return 2023-01-15, the previous Sunday.
EDIT: Sorry, meant to use the ISO week. As Dnoeth points out, the regular week option doesn't work consistently (which I didn't know, never used it for this before). Anyhoo, his answer is better than mine...

I'm trying to calculate 3 days from today, but not include weekend or holidays, but

The workday function should work for this. I have a Range called "Holidays". The problem is that workdays doesn't count the weekend days. I need to count the weekend days. BUT, if the 3rd day comes on a weekend or a holiday choose the next non-weekend or non-holiday day. A2 is the Effective Date of the contract, which is the start date. I'm trying to calculate the day on which the Earnest Money is due. See the attached chart as to how it should calculate.
=if(OR(A5="",A5>workday(A2,3,Holidays)),workday(A2,3,Holidays),"") is the formula I have but it works for Effective dates that fall on a Sunday, Monday, Tuesday and Wednesday, but not for Effective dates that fall on a Thursday, Friday, Saturday.
Starting from Today
Use this formula to get the dates starting from today
={ArrayFormula(
VLOOKUP(WEEKDAY(SEQUENCE(7,1,TODAY(),1),2),
{SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},2)),
BYROW(SEQUENCE(7,1,TODAY(),1),
LAMBDA(d, VLOOKUP(WEEKDAY(IF(IFNA(MATCH(d+3,F2:F13*1,0),"")="",d+3,d+4),2),
{SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},2)))}
Starting from Monday
={ArrayFormula(
VLOOKUP(WEEKDAY(SEQUENCE(7,1,TODAY()-WEEKDAY(TODAY(),3),1),2),
{SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},2)),
BYROW(SEQUENCE(7,1,TODAY()-WEEKDAY(TODAY(),3),1),
LAMBDA(d, VLOOKUP(WEEKDAY(IF(IFNA(MATCH(d+3,F2:F13*1,0),"")="",d+3,d+4),2),
{SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},2)))}
Starting from Monday using Lambda
Starting from the current week monday.
=ArrayFormula(LAMBDA(vl,wd,
{VLOOKUP(WEEKDAY(wd,2),vl,2),
BYROW(wd,LAMBDA(d, VLOOKUP(WEEKDAY(IF(IFNA(MATCH(d+3,F2:F13*1,0),"")="",d+3,d+4),2),vl,2)))})
({SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},
SEQUENCE(7,1,TODAY()-WEEKDAY(TODAY(),3),1)))
Explanation
You need the Calendar Year 20xx Legal Public Holidays to skip one day if matched the legal holidays date after adding 3 days.
Used formulas help
BYROW - SEQUENCE - TODAY - LAMBDA - VLOOKUP - IF - IFNA - MATCH - ARRAYFORMULA

RRULE for every other week except last week of month

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

What's the name of this week number algorithm?

My customer wants to display week numbers as they show up in his wall calendar:
Week #1 starts on 1st January
Last week of the year (#53 or #54) ends on 31st December
All other weeks start on Monday and end on Sunday
(As a consequence, first and last weeks do not necessarily have 7 days.)
Does this week number algorithm have a name?
Clarification: I already have PHP code to calculate it, I'm just curious about whether this way of identifying weeks has a commonly accepted name.
There isn't one - that is a completely off-the-wall approach to week numbers. Weeks normally start either with Monday or Sunday when using the Gregorian Calendar. They do not start midway. This is not a criticism of your customer, but a comment on the fact that people invent new ways looking at date arithmetic. And get in trouble migrating to new systems.
RFC 3339
http://www.ietf.org/rfc/rfc3339.txt
See also ISO 8601

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.