davedelong / time

Robust and type-safe date and time calculations for Swift
MIT License
2.32k stars 77 forks source link

How does one set the clock so that Monday is the first dayu of the week? #54

Closed Rillieux closed 3 years ago

Rillieux commented 3 years ago

Is this possible? If so, where in the documentation is this referenced?

theablemo commented 3 years ago

Hi Rillieux, So first of all, this API is designed to make developers use time much more easily. But keep in mind that almost everything is based on the system's current time.

So as the documentation says:

Humans use calendars because talking about time and duration in terms of SI Seconds is incomprehensible. Thus, it's appropriate to think about calendars as an abstraction that exists on top of physical time.

So to get the current time you should use: let clock = Clocks.system

Now to put a layer of "abstraction" on this, you can get the current date by using: let today = clock.today()

Now as your question said, you want to get the day of the week. You can get today's day of the week by: let dayOfTheWeekForToday = today.dayOfWeek

In the Absolute+Day.swift file in the source code, it mentions that dayOfWeek property return as follows:

For the Gregorian calendar, 1 = Sunday, 2 = Monday, ... 7 = Saturday

And if you want to know why, you can refer to here. So there is no possible way for now to set Monday as the first day of the week. Good luck.

davedelong commented 3 years ago

Hi @Rillieux,

"The first day of the week" is a concept that's tied to the calendaring system. It's used for calculations such as "get all the days in this calendar week in order", and then getting an array back where the first element corresponds to Sunday or Monday, depending on one's locale.

Time accounts for this by keying off the firstWeekday property on the Calendar value in the Region used for calculating values. By default, this uses whatever the user has specified as the preferences on their device.

So, if you're interested in doing calculations that override the user's preferred notion of "what's the first day of the week", then you can create a custom Calendar value, set its firstWeekday property, use that to create a Region value, and then use that Region as the basis for your calculations in Time.