swift-foundation icon indicating copy to clipboard operation
swift-foundation copied to clipboard

[Suggestion] ISO standard weekday indexing

Open BrentMifsud opened this issue 4 months ago • 5 comments

Is your feature request related to a problem? Please describe.

Most languages have dates indexed from Monday to Sunday (following iso standards). Swift's Calendar apis are indexed Sunday to Saturday.

Describe the solution you'd like

Most backwards compatible solution:

  • add an isoWeekday property to DateComponents

Probably the more correct solution:

  • when using an iso8601 calendar, weekdays should be indexed following iso standards

Additional context Add any other context about the feature request here.

BrentMifsud avatar Aug 19 '25 19:08 BrentMifsud

It seems to be working as expected on main (and I believe in the 6.2 branch too since we haven't touched this after branching)

let first = Calendar(identifier: .iso8601).firstWeekday // 2

Which translates to Monday.

Is this not what you're seeing, or are you referring to other API?

itingliu avatar Aug 20 '25 22:08 itingliu

Im referring to the fact that weekday mappings are as follows:

sunday - 1 monday - 2 Tuesday - 3 etc...

the ISO standard is:

monday - 1 Tuesday - 2 ... sunday - 7

BrentMifsud avatar Aug 21 '25 01:08 BrentMifsud

I don't think we can ever change this without breaking existing clients. There are other API that requires this ordering, such as Calendar.weekdaySymbols, so the ordering is basically ABI stable and cannot be broken.

itingliu avatar Aug 21 '25 15:08 itingliu

I don't think we can ever change this without breaking existing clients. There are other API that requires this ordering, such as Calendar.weekdaySymbols, so the ordering is basically ABI stable and cannot be broken.

Can an additional property be added?

For example:

// new
DateComponents(isoWeekday: 1) // would produce a date on a Monday

// existing behavior  
DateComponents(weekday: 1) // would produce a date on a Sunday.

This ensures backwards compatibility.

BrentMifsud avatar Aug 22 '25 16:08 BrentMifsud

DateComponents has a Calendar variable, and the convention is to refer calendar related information to the calendar. Given that this information is available in Calendar, adding this similar property on DateComponents seems redundant

Also

let day = DateComponents(isoWeekday: 1) // would produce a date on a Monday

Can you spell it out for me? Would day be an integer or a enum or something that represents a Monday?

itingliu avatar Aug 22 '25 17:08 itingliu