2

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?

Anton
  • 339
  • 5
  • 15
  • ISO8601 is merely a formatting standard. The weekday will be taken IIRC from your locale. – Fogmeister Oct 08 '17 at 17:54
  • @rmaddy Today October, 8th is Sunday, not Monday. – Ramy Al Zuhouri Oct 08 '17 at 18:05
  • 1
    @RamyAlZuhouri Yeah, I don't know what I was thinking. So in other words, the code is working just fine. – rmaddy Oct 08 '17 at 18:13
  • @RamyAlZuhouri Yes, and 1 is sunday. – Amin Negm-Awad Oct 08 '17 at 18:14
  • @Anton Why do you think `weekday` should have a value of 7? 7 represents Saturday. – rmaddy Oct 08 '17 at 18:16
  • Well, for me monday is 1st, ..., sunday is 7th day of week – Anton Oct 08 '17 at 18:18
  • 1
    @Anton I explained in may answer the "reasons" (of course, there is no scientific reason) for it: Hebrew and Christian tradition. Having monday as the first day is a secularized (commercial) view on the week. – Amin Negm-Awad Oct 08 '17 at 18:21
  • 1
    @Anton The result of `weekday` has nothing to do with what your locale treats as the first day of the week. For a Gregorian calendar, 1 is Sunday and 7 is Saturday. This applies no matter whether your locale treats Sunday, Monday, or any other day of the week as the traditional 1st day of the week. – rmaddy Oct 08 '17 at 18:23

1 Answers1

0

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.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • NSCalendar is part of Foundation not Cocoa. Secondly, NSCalendar and Calendar are not exactly the same. See https://developer.apple.com/documentation/foundation/nscalendar – idmean Oct 08 '17 at 18:14
  • 2
    This answer would be a lot clearer if you simply stated that a value of `1` represents Sunday and since October 8, 2017 is a Sunday, the code is working just fine. – rmaddy Oct 08 '17 at 18:15
  • @idmean Foundation is a part of Cocoa, therefore every part of Foundation is a part of Cocoa. – Amin Negm-Awad Oct 08 '17 at 18:16
  • @rmaddy Some calendar implementations starts counting with 0 for whatever reasons, I saw implementations, which had an option, whether you want to start with 1 or 0. The problem is generic (and its solution and the answer) and does not depend on the indexing the calendar uses. The point is, that sunday is traditionally the first day of the week. It does not matter, whether you start counting with 0, 1 or whatever. – Amin Negm-Awad Oct 08 '17 at 18:18
  • @AminNegm-Awad I think you should avoid the discussion what is traditionally the first week because that differs a lot among countries and in most modern calendars Sunday is the last day of the week. The fact that constant `1` was chosen for Sunday was probably merely because it was implemented in USA. That's all there is to it. No need to study calendar history. – Sulthan Oct 08 '17 at 18:24
  • @AminNegm-Awad I spent three hours today, on mapping a date to an index. I couldn't wrap my head around this, but now I see it isn't so simple as iso80601. Thanks. – Martin Muldoon Oct 08 '17 at 18:24
  • @AminNegm-Awad Of course. But Foundation is also available in Swift *without* Cocoa, since the question doesn’t mention Cocoa or macOS it doesn’t make sense to mention it, particularly since the OP could be using Swift on Linux where Swift uses its own Foundation implementation. – idmean Oct 08 '17 at 18:26
  • Foundation is available in Objective-C without Cocoa, too. And exactly this is what I wanted to say: There can be a different implementation using "the same" Swift. – Amin Negm-Awad Oct 08 '17 at 18:30
  • @Sulthan I think, that *in most western* and *traditionally in hebrew and christian calendar* emphasizes, that there is no mathematical background for this counting , but simple definition. (As it would for monday = 1. There is no reason at all, to give a specific day of the week the lowest index. It's definition and nothing else.) Having replaceable definitions with no scientific reason for one definition a explanation why a specific one is chosen, helps understanding. Esp. the OP obviously thought that monday is the first day of the week. – Amin Negm-Awad Oct 08 '17 at 18:35
  • I. e. fr wiki: *Pour les religions bibliques, le dimanche est le premier jour de la semaine musulmane2, juive, chrétienne3*, de wiki: *der Sonntag […] ist heute im deutschsprachigen Raum[1] der siebte Wochentag, kulturhistorisch aber der erste.* The italian wiki is more specific. I do not understand more languages. – Amin Negm-Awad Oct 08 '17 at 18:42
  • @AminNegm-Awad People in most european countries would actually suppose Monday the first day of the week. This comes from the christian tradition, Sunday being God's day in Christianity. The history of calendars is quite complex but it actually has nothing to do with the question. – Sulthan Oct 08 '17 at 19:02
  • @Sulthan Your comment simple repeats my answer … – Amin Negm-Awad Oct 08 '17 at 19:23