Crystal: DayOfWeek should be compatible to ordinal day numbers from ISO 8601

Created on 16 Aug 2018  路  1Comment  路  Source: crystal-lang/crystal

There exist two common schemes for numbering week days: either starting with sunday = 0 or monday = 1. The enum values of Time::DayOfWeek are Sunday = 0, Monday = 1, ..., Saturday = 6. The ordinal day numbers in ISO 8601 however are Monday = 1, ..., Saturday = 6, Sunday = 7.
This conversion is pretty simple and besides sunday all numerical representations are the same in both numberings.

However, there should be a direct way to use ISO numbers without having to implement any conversion manually.

I'd even favour the internal representation to follow ISO Monday = 1 because this is the most commonly used standard. I propose to change the enum values accordingly. Obviously, this is a breaking change.

enum DayOfWeek
  Monday    = 1
  Tuesday   = 2
  Wednesday = 3
  Thursday  = 4
  Friday    = 5
  Saturday  = 6
  Sunday    = 7
end

In order to also support 0-based numbering, additional methods should be added to the enum. For example #value_sun0 (I don't have any more poetic idea) which returns value % 7. There should also be an equivalent constructor for 0-based numbers. Instead of an additional one, it might be better to simply alter from_value (and maybe new) to map 0 to Sunday. This is still unambiguous and the breaking change is reduced.

The alternative - if we don't want to change the internal values of DayOfWeek - would be to add #to_iso_week_day and .from_iso_week_day providing the required conversions. This avoids a breaking change.

I hope this doesn't spark a discussion about which day is the first day of the week. There are different views and all are right. We just need to assign numbers to represent week days and besides technical reasons it shouldn't matter how.

draft stdlib

Most helpful comment

I agree to follow the ISO standard by default. Modifying the enum looks fine.

>All comments

I agree to follow the ISO standard by default. Modifying the enum looks fine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

will picture will  路  3Comments

ArthurZ picture ArthurZ  路  3Comments

lbguilherme picture lbguilherme  路  3Comments

RX14 picture RX14  路  3Comments

Sija picture Sija  路  3Comments