diff --git a/README.markdown b/README.markdown index 4b3f03a..21f0cf7 100644 --- a/README.markdown +++ b/README.markdown @@ -692,10 +692,9 @@ ok %% that timezone to our intended timezone. ``` -## Date Truncation (Beginning of X) +## Beginning or Ending of time periods (hours, days, years, weeks, etc) -Sometimes you need to truncate a time (say, the beginning of the current -month). +qdate can determine beginnings and endings of time periods, like "beginning of the month" This is abstracted to `beginning_X` functions, which return a date/time format with the dates and times truncated to the specified level. @@ -710,11 +709,12 @@ 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 +#### Beginning of Week qdate can also do a special "beginning" case, particularly the "beginning of -the week" calculation. This has two forms, specifically: - +the week" calculation. This has three forms, specifically: + + + `beginning_week()` - Returns first day of the current week. + `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). @@ -725,6 +725,15 @@ the week" calculation. This has two forms, specifically: These all return 12am on the day that is the first day of the week of the provided date. +### End of time period + +There are also the related `end_X` functions available, using the same +conventions, except return the last second of that time period. + +So `end_month("2016-01-05")` will return the unix timestamp representing +"2016-01-31 11:59:59pm" + + ## Date Arithmetic The current implementation of qdate's date arithmetic returns Unixtimes. diff --git a/src/qdate.erl b/src/qdate.erl index 767b1c0..0970c18 100644 --- a/src/qdate.erl +++ b/src/qdate.erl @@ -31,6 +31,7 @@ beginning_hour/0, beginning_day/1, beginning_day/0, + beginning_week/0, beginning_week/1, beginning_week/2, beginning_month/1, @@ -46,6 +47,9 @@ end_hour/0, end_day/1, end_day/0, + end_week/0, + end_week/1, + end_week/2, end_month/1, end_month/0, end_year/1, @@ -396,6 +400,9 @@ beginning_year(Date) -> {{Y,_,_},{_,_,_}} = to_date(Date), {{Y,1,1},{0,0,0}}. +beginning_week() -> + beginning_week({date(), time()}). + %% 1 = Monday, 7 = Sunday beginning_week(Date) -> beginning_week(1, Date). @@ -457,6 +464,16 @@ end_year(Date) -> {{Y,_,_},_} = to_date(Date), {{Y,12,31},{23,59,59}}. +end_week() -> + end_week({date(), time()}). + +end_week(Date) -> + end_week(1, Date). + +end_week(BeginningDayOfWeek, Date) -> + Beginning = beginning_week(BeginningDayOfWeek, Date), + PlusWeek = add_weeks(1, Beginning), + add_seconds(-1, PlusWeek). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Comparisons %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%