Add support for named days of the week in beginning_week

This commit is contained in:
Jesse Gumm 2018-02-04 12:21:47 -06:00
parent 628c87557a
commit 1cccb8392a
2 changed files with 32 additions and 2 deletions

View file

@ -719,12 +719,23 @@ the week" calculation. This has three forms, specifically:
(chosen because Erlang's calendar:day_of_the_week uses 1=Monday and (chosen because Erlang's calendar:day_of_the_week uses 1=Monday and
7=Sunday). 7=Sunday).
+ `beginning_week(DayOfWeek, Date)` - Calculates the beginning of the week + `beginning_week(DayOfWeek, Date)` - Calculates the beginning of the week
based on the provided `DayOfWeek`. Valid values for DayOfWeek are 1-7, based on the provided `DayOfWeek`. Valid values for DayOfWeek are the
where 1=Monday, and 7=Sunday. integers 1-7 or the atom verions of the days of the week. Specifically:
* Monday: `1 | monday | mon`
* Tuesday: `2 | tuesday | tue`
* Wednesday: `3 | wednesday | wed`
* Thursday: `4 | thursday | thu`
* Friday: `5 | friday | fri`
* Saturday: `6 | saturday | sat`
* Sunday: `7 | sunday | sun`
These all return 12am on the day that is the first day of the week of the These all return 12am on the day that is the first day of the week of the
provided date. provided date.
(My apologies to non-English speakers. I'm a lazy American who only speaks
English, hence the Anglocentric day names).
### End of time period ### End of time period
There are also the related `end_X` functions available, using the same There are also the related `end_X` functions available, using the same

View file

@ -407,6 +407,9 @@ beginning_week() ->
beginning_week(Date) -> beginning_week(Date) ->
beginning_week(1, Date). beginning_week(1, Date).
beginning_week(BeginningDayOfWeek, Date) when is_atom(BeginningDayOfWeek) ->
DOW = weekday_map(BeginningDayOfWeek),
beginning_week(DOW, Date);
beginning_week(BeginningDayOfWeek, Date0) when beginning_week(BeginningDayOfWeek, Date0) when
BeginningDayOfWeek >= 1, BeginningDayOfWeek >= 1,
BeginningDayOfWeek =< 7, BeginningDayOfWeek =< 7,
@ -424,6 +427,22 @@ beginning_week(BeginningDayOfWeek, Date0) when
beginning_day(add_days(-Diff, Date)) beginning_day(add_days(-Diff, Date))
end. end.
weekday_map(mon) -> 1;
weekday_map(tue) -> 2;
weekday_map(wed) -> 3;
weekday_map(thu) -> 4;
weekday_map(fri) -> 5;
weekday_map(sat) -> 6;
weekday_map(sun) -> 7;
weekday_map(monday) -> 1;
weekday_map(tuesday) -> 2;
weekday_map(wednesday) -> 3;
weekday_map(thursday) -> 4;
weekday_map(friday) -> 5;
weekday_map(saturday) -> 6;
weekday_map(sunday) -> 7.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% End of Period (day/hour, etc) %%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%% End of Period (day/hour, etc) %%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%