0

I have a Date object from which I need to get a weekOfYear and my code works great on a real device, but it's one weekOfYear ahead on the simulator if I select Sunday. All other days of the week work fine.

I know the simulator uses the mac's date and time, but both the mac and the real device are set to auto, same time zone, same settings.

let wednesday: Date? = calendar.date(from: DateComponents(year: 2020, month: 10, day: 14))
let sunday: Date? = calendar.date(from: DateComponents(year: 2020, month: 10, day: 18))
    
print("\(wednesday?.weekOfYear) vs \(sunday?.weekOfYear)")
// In the simulator this prints Optional(42) vs Optional(43)
// On a real device this prints Optional(42) vs Optional(42)

Any idea what I'm doing wrong? Does it have anything to do with daylight savings time? The date objects themselves are printed as 2020-10-13 23:00:00 +0000 and 2020-10-17 23:00:00 +0000, but nonetheless, adding 1h for DST should mean it's still Sunday the 18th.

Thanks in advance for any tips.

Update

Thanks for the comments everyone, they helped me find the issue.

The calendar I am using is this:

var calendar: Calendar {
    var calendar = Calendar(identifier: .iso8601)
    calendar.firstWeekday = 2
    return calendar
}

And I have a Date extension to get .weekday which looks like this:

extension Date {
    var weekday: Int { return Calendar.current.component(.weekday, from: self) }
}

So the problem was that I was not using my iso8601 calendar, which starts the week on Monday. I changed it to my calendar and it now works.

CristianMoisei
  • 2,071
  • 2
  • 22
  • 28
  • 1
    I ran this code in a playground on my Mac and on my iPhone 11 pro and got 42 & 43 both times. Have you implemented an extension to add `weekOfYear` to `Date`? Show that code. – Paulw11 Oct 12 '20 at 22:28
  • 1
    First don't print the date. It will give you UTC timezone date representation. Use `date.description(with: .current)` I got 43 for Sunday as well which is correct. The week starts at Sunday for all calendars except for ISO8601 calendar which starts on Monday. Can you check which calendar is being used in your code. I am using `.gregorian` here. – Leo Dabus Oct 12 '20 at 22:30
  • 1
    Resuming you are omitting the most important part of your code. Without knowing the calendar used we can't really help. – Leo Dabus Oct 12 '20 at 22:37
  • 1
    "I know the simulator uses the mac's date and time". It really doesn't matter. The date will always be the start of that day for your current timezone. – Leo Dabus Oct 12 '20 at 22:46
  • Thanks @Paulw11, your hunch was correct, it was the extension that was causing the issue. I updated the question with all these details, and if you'd like to post this as an answer, I can accept it. – CristianMoisei Oct 13 '20 at 00:56
  • @LeoDabus thanks for explaining, this is helpful. I will use .description in the future, but what exactly do you mean when you say it will print the UTC date representation? So it will ignore the locale and always print the UTC date, even if the device is in a completely different timezone? I live in London so to me it always looked like it's off by 1h during daylight savings time, but nothing else. – CristianMoisei Oct 13 '20 at 00:59
  • 1
    @CristianMoisei When you print a date it will always print the corresponding time at UTC. A date has no timezone. It is just a point in time. The date representation (string) is how you display the date localized and with the current timezone. – Leo Dabus Oct 13 '20 at 01:07
  • 1
    @CristianMoisei note that calendar ISO8601 the first weekday is already Monday by default. No need to set it again. `let calendar = Calendar(identifier: .iso8601)` – Leo Dabus Oct 13 '20 at 01:09
  • 1
    https://stackoverflow.com/a/28347285/2303865 – Leo Dabus Oct 13 '20 at 01:12

2 Answers2

1

As I already mentioned in my comment the problem is the calendar ISO8601 you were using. The first weekday of that calendar is Monday while the current calendar (probably Gregorian) the first weekday is Sunday.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
1

Perhaps there is a difference between the simulator and the real device. Becausue it has a different locale timeZone, please ensure they are same.

Marc Steven
  • 477
  • 4
  • 16
  • Thanks Marc. The issue was as Leo suggested that I was using the wrong calendar in my extension, so it was defaulting to a calendar where the week starts on Sunday. – CristianMoisei Oct 13 '20 at 12:01