From 1cccb8392abb9c914ad81a6a7cb1072a32d4d83c Mon Sep 17 00:00:00 2001 From: Jesse Gumm Date: Sun, 4 Feb 2018 12:21:47 -0600 Subject: [PATCH] Add support for named days of the week in beginning_week --- README.markdown | 15 +++++++++++++-- src/qdate.erl | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 21f0cf7..25b9e56 100644 --- a/README.markdown +++ b/README.markdown @@ -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 7=Sunday). + `beginning_week(DayOfWeek, Date)` - Calculates the beginning of the week - based on the provided `DayOfWeek`. Valid values for DayOfWeek are 1-7, - where 1=Monday, and 7=Sunday. + based on the provided `DayOfWeek`. Valid values for DayOfWeek are the + 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 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 There are also the related `end_X` functions available, using the same diff --git a/src/qdate.erl b/src/qdate.erl index 39c3967..48e841e 100644 --- a/src/qdate.erl +++ b/src/qdate.erl @@ -407,6 +407,9 @@ beginning_week() -> beginning_week(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 BeginningDayOfWeek >= 1, BeginningDayOfWeek =< 7, @@ -424,6 +427,22 @@ beginning_week(BeginningDayOfWeek, Date0) when beginning_day(add_days(-Diff, Date)) 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) %%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%