Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Proposal] Add Date.iso_day_of_week/2

52 views
Skip to first unread message

Kip

unread,
Jan 11, 2025, 3:45:19 AMJan 11
to elixir-lang-core
A recent discussion clarified that the return value from `Date.day_of_week/2` is an ordinal value. That is, when it returns "1" that means "first day of week". It specifically does not mean "1" is Monday.  

That means that it would be useful to have a function that does return the cardinal day of week - that is, a number where 1 == Monday and 7 == Sunday.

The function would look something like:

def iso_day_of_week(%{calendar: Calendar.ISO} = date) do
  day_of_week(date)
end

def iso_day_of_week(date) do
  date
  |> convert!(Calendar.ISO)
  |> day_of_week()
end

This implementation relies on the knowledge that when called with starting_on = :default (which is the default argument value) the returned value is indeed 1 == Monday, 7 == Sunday.

If there is any consensus I'll submit a PR.


José Valim

unread,
Jan 11, 2025, 4:33:39 AMJan 11
to elixir-l...@googlegroups.com
Hi Kip, my concern with this PR is that it opens up the path for "duplicating" several of the functions in the Date module:

Date.iso_beginning_of_month/1
Date.iso_beginning_of_week/2
Date.iso_day_of_era/1
Date.iso_day_of_week/1
Date.iso_day_of_year/1
Date.iso_days_in_month/1
Date.iso_end_of_month/1
Date.iso_end_of_week/2
Date.iso_months_in_year/1
Date.iso_quarter_of_year/1
Date.iso_year_of_era/1

Perhaps not all of the above but at least a few.

Also, in your case, couldn't you support a custom starting_on value called :iso_default or :monday, which will behave as you described?

So your :default can adhere to your custom calendar semantics, and then either :monday or :iso_default returns what the computation above would provide.


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/eeff3e35-448b-4dcf-ad4a-866bac76a819n%40googlegroups.com.

Kip Cole

unread,
Jan 11, 2025, 5:37:17 AMJan 11
to elixir-l...@googlegroups.com
Also, in your case, couldn't you support a custom starting_on value called :iso_default or :monday, which will behave as you described?

In everything I do I try very hard to have a consistent implementation of the Calendar behaviour. I believe that in most cases a user or developer shouldn’t have to think about what calendar is in use - it’s just the right calendar producing the right results.  So while I could implement a custom `starting_on`, that then sets my calendars apart from the default calendar and other peoples calendars. I really really really want to avoid that. Calendaring is hard enough as it is without developers having to differentiate between them in their code.

> my concern with this PR is that it opens up the path for "duplicating" several of the functions in the Date module

I’m just a pragmatist and a bit gun shy to argue any point here. My only intent was to define a standard Elixir API where, for any given date in any complying calendar, I could know upon which day of the week that day falls. 

If I’m the only person who thinks that’s a reasonable expectation then there is no need to consider the proposal further.



José Valim

unread,
Jan 11, 2025, 5:54:36 AMJan 11
to elixir-l...@googlegroups.com
> My only intent was to define a standard Elixir API where, for any given date in any complying calendar, I could know upon which day of the week that day falls.

So I can think of two options:

1. You can use the atom :monday, as that should return the same meaning on all calendars based on our weekdays? In the same way that passing :sunday to Calendar.ISO makes it return the same value as "Calendar.USA". The benefit of this option is that it is supported for quite some time.

2. We introduce a new option, called :iso8601, which returns the default for iso8601, which is therefore :monday.

My preference is 1 but I am open to hearing it is a bad idea. :D


Kip Cole

unread,
Jan 11, 2025, 6:14:26 AMJan 11
to elixir-l...@googlegroups.com
I agree that option (1) is more pragmatic.

I still worry that `Date.day_of_week/2` returns ordinal numbers in all but one very specific (but almost ubiqtuious) case for Calendar.ISO. So documentating that the analogous case for all compliant calendar is a reasonable compromise.

Proposal closed.

José Valim

unread,
Jan 12, 2025, 3:50:07 AMJan 12
to elixir-l...@googlegroups.com
Btw, from an implementation point of view, you can probably implement day_of_week for  most calendars as:

def day_of_week(year, month, day, :default), do: day_of_week(year, month, day, INITIAL_DAY_OF_WEEK_FOR_CALENDAR)
def day_of_week(year, month, day, starting_on), do: Calendar.ISO.day_of_week(year, month, day, starting_on)


Reply all
Reply to author
Forward
0 new messages