From 6fadc548d299f5e5b627c959333060314cc48907 Mon Sep 17 00:00:00 2001 From: Jesse Gumm Date: Wed, 4 Jan 2017 15:58:27 -0600 Subject: [PATCH] Update readme. Add some sanity guards on beginning_week --- README.markdown | 15 +++++++++++++++ src/qdate.erl | 5 ++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 96958e0..4b3f03a 100644 --- a/README.markdown +++ b/README.markdown @@ -710,6 +710,21 @@ There are also 0-arity versions of the above, in which `Date` is assumed to be "right now". For example, calling `qdate:beginning_month()` would return midnight on the first day of the current month. +### Beginning of Week + +qdate can also do a special "beginning" case, particularly the "beginning of +the week" calculation. This has two forms, specifically: + + + `beginning_week(Date)` - Assumes the beginning of the week is Monday + (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. + +These all return 12am on the day that is the first day of the week of the +provided date. + ## Date Arithmetic The current implementation of qdate's date arithmetic returns Unixtimes. diff --git a/src/qdate.erl b/src/qdate.erl index 6e93230..767b1c0 100644 --- a/src/qdate.erl +++ b/src/qdate.erl @@ -400,7 +400,10 @@ beginning_year(Date) -> beginning_week(Date) -> beginning_week(1, Date). -beginning_week(BeginningDayOfWeek, Date0) -> +beginning_week(BeginningDayOfWeek, Date0) when + BeginningDayOfWeek >= 1, + BeginningDayOfWeek =< 7, + is_integer(BeginningDayOfWeek) -> {DateOnly, _} = Date = to_date(Date0), CurDOW = calendar:day_of_the_week(DateOnly), if