swift 4 weekday in iso8601 - swift

It's 8th of October, 2017, Sunday.
var weekday = Calendar(identifier: .iso8601).component(.weekday, from: Date())
weekday is 1, but should be 7. What's the problem?

First of all: This has nothing to do with Swift. NSCalendar is a part of Cocoa. (And written in Objective-C, by the way.)
In general:
Even in most western countries the "business week" starts with monday (aka 1st day of week), traditionally in hebrew and christian calendar the sunday is the first day of week. (Beside this, for christians it is the day of god. Of course, this is the first day. Can you give god another place?)
Therefore it is quite usual to get 1 or 0 (aka the lowest valid index) for sunday on western calendars.
ISO8601
However, ISO8601 positively defines the monday as the first day of a week. But this has nothing to do with indexing the weekdays, but with calculating with weeks, i. e. for "first week of a year". weekday is a simple index, nothing else.

Related

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

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.

How do I express coming Monday in 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

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

What is the difference between kCFCalendarUnitWeekday and kCFCalendarUnitDay?

From the documentation:
kCFCalendarUnitDay
Specifies the day unit.
and
kCFCalendarUnitWeekday
Specifies the weekday unit. The
weekday units are the numbers 1-N
(where for the Gregorian calendar N=7
and 1 is Sunday).
I wanted an alarm to go off from Monday to Friday and I thought that kCFCalendarUnitWeekday was what I needed, until I woke up this Saturday :) Can someone explain what is the difference between these two, as the documentation doesn't seem very helpful to me.
kCFCalendarUnitDay specifies the day of the month, i.e. a number between 1 and 31 in the Gregorian Calendar.
kCFCalendarUnitWeekday specifies the day of the week, i.e. a number between 1 (Sunday) and 7 (Saturday) in the Gregorian Calendar.
until I woke up this Saturday
Can you show us some code?

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.