Why does SYSTEMTIME::wDayOfWeek begin with 0 for sunday? - date

Can anyone here tell me why the week begins at 0 for sunday with SYSTEM_TIME::wDayOfWeek ? Here in Germany the week begins at monday. But maybe I'm missing that for other cultures the week begins at sunday.

The modern calendar was sponsored by Pope Gregory XIII. So regardless of one's personal beliefs about religion, it's necessary to look at what the designers of the Gregorian calendar believed:
In Genesis, the creation takes six days followed by one day of rest. It doesn't actually matter whether those were literal days, because the follow-up commandment to rest on each seventh day was talking about a literal day. That seventh day on which to rest was called "Sabbath" by the Jews.
In the gospels, the time of week isn't left to the imagination. Perfume could not be applied to the body of the Messiah as he was buried (as it was a feast day) or on the next day (as it was a Sabbath), so the women arrived at the tomb on the morning following the Sabbath, and recorded that they found it empty and thereafter spoke to a resurrected Messiah.
In honor of these two events in which the designers of and early adherents to the Gregorian calendar deeply believed, they made a weekend out of (a) the Sabbath (our Saturday), which was by definition the seventh day of one week, and (b) the weekday corresponding to resurrection (our Sunday), which therefore had to be the first day of the next week.
And that's why, in the Gregorian calendar, the week starts on Sunday. This is the system followed in the USA where the majority of OS APIs were designed, including the Windows API.

Related

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

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

Week of the Year

As per ISO 8601, the first week is the week having at least 4 days and week start with Monday. For countries where the week starts with another day, like Sunday, how is the first week defined ?
Weeks can be defined in all sorts of ways.
The ISO 8601 standard way (starts on Monday, week # 1 has first Thursday of year) is sensible and practical, and increasingly common. In Java and its java.time framework (see Tutorial), the standard defined behavior is encapsulated in the IsoFields class. See my Answer to another Question for examples.
The non-standard approaches vary widely. Be very clear in determining such definitions with the stakeholders in your project. Document the definitions in your code base as well.
Often a company uses their own defined fiscal year for other business purposes, while some companies use the fiscal year only for bookkeeping and another calendar system for operations. Often the fiscal year is defined by its last day being a certain day-of-week such as the Friday closest to 31 December. So a year may have 52 or 53 weeks.
Sometimes week number 1 is the week containing January 1. Some people include the prior days of that week (December 31, December 30, etc.) in that new year’s week while some people cut the week into two parts (last year’s part and new year’s part).
Some define week # 1 as the earliest week that contains the first day of the week. Of course that first day of the week is defined by cultural norms such as Sunday in the United States.
Some people define week number 1 as the first complete week of the year, having no days from the previous years. So January 1, January 2, and so on may be counted in the last year.
Some industries and some companies define their own weeks of the year. Some are predictable with a pattern that can deterministically be projected out into the future, while some are arbitrary and defined by humans every year or two or three.
Until the 1990s, the Eastman Kodak Company for decades used and promoted a very different calendar system, the International Fixed Calendar. This system provides for a year of 13 months of 28 days each (exactly 4 weeks), with every date fixed always on the same weekday. So every year has week # 1 starting on Sunday January 1.
The French Republican Calendar is another different calendar in use for over a decade, with a ten-day week.
Keep your mind open, to make sense of date-time matters such as this. Understand that while a day is defined by nature (spinning of the earth) and a year is defined by nature (Earth’s orbit around the Sun), weeks and months and such are inventions of humans. The definition is subject to human creativity and to human folly (ex: Daylight Saving Time nonsense).
Being a standard, it's standardized. Same everywhere.
See Wikipedia page.

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.