Add end_week/[0-2] and beginning_week/0

This commit is contained in:
Jesse Gumm 2017-01-04 19:22:21 -06:00
parent c42272a9eb
commit aa114b1ef8
2 changed files with 32 additions and 6 deletions

View file

@ -692,10 +692,9 @@ ok
%% that timezone to our intended timezone. %% 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 qdate can determine beginnings and endings of time periods, like "beginning of the month"
month).
This is abstracted to `beginning_X` functions, which return a date/time format This is abstracted to `beginning_X` functions, which return a date/time format
with the dates and times truncated to the specified level. 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 "right now". For example, calling `qdate:beginning_month()` would return
midnight on the first day of the current month. 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 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 + `beginning_week(Date)` - Assumes the beginning of the week is Monday
(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).
@ -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 These all return 12am on the day that is the first day of the week of the
provided date. 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 ## Date Arithmetic
The current implementation of qdate's date arithmetic returns Unixtimes. The current implementation of qdate's date arithmetic returns Unixtimes.

View file

@ -31,6 +31,7 @@
beginning_hour/0, beginning_hour/0,
beginning_day/1, beginning_day/1,
beginning_day/0, beginning_day/0,
beginning_week/0,
beginning_week/1, beginning_week/1,
beginning_week/2, beginning_week/2,
beginning_month/1, beginning_month/1,
@ -46,6 +47,9 @@
end_hour/0, end_hour/0,
end_day/1, end_day/1,
end_day/0, end_day/0,
end_week/0,
end_week/1,
end_week/2,
end_month/1, end_month/1,
end_month/0, end_month/0,
end_year/1, end_year/1,
@ -396,6 +400,9 @@ beginning_year(Date) ->
{{Y,_,_},{_,_,_}} = to_date(Date), {{Y,_,_},{_,_,_}} = to_date(Date),
{{Y,1,1},{0,0,0}}. {{Y,1,1},{0,0,0}}.
beginning_week() ->
beginning_week({date(), time()}).
%% 1 = Monday, 7 = Sunday %% 1 = Monday, 7 = Sunday
beginning_week(Date) -> beginning_week(Date) ->
beginning_week(1, Date). beginning_week(1, Date).
@ -457,6 +464,16 @@ end_year(Date) ->
{{Y,_,_},_} = to_date(Date), {{Y,_,_},_} = to_date(Date),
{{Y,12,31},{23,59,59}}. {{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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Comparisons %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%