Add beginning_X/0 variations

This commit is contained in:
Jesse Gumm 2015-11-07 11:39:24 -06:00
parent 6ca6037f29
commit d73a31e664
2 changed files with 29 additions and 3 deletions

View file

@ -621,9 +621,11 @@ ok
## Date Truncation (Beginning of X) ## Date Truncation (Beginning of X)
Sometimes you need to truncate a time (say, the beginning of the current month). Sometimes you need to truncate a time (say, the beginning of the current
month).
This is abstracted to `beginning_X` functions, which return a date/time format with the dates and times truncated to the specified level. This is abstracted to `beginning_X` functions, which return a date/time format
with the dates and times truncated to the specified level.
+ `beginning_minute(Date)` + `beginning_minute(Date)`
+ `beginning_hour(Date)` + `beginning_hour(Date)`
@ -631,6 +633,10 @@ This is abstracted to `beginning_X` functions, which return a date/time format w
+ `beginning_month(Date)` + `beginning_month(Date)`
+ `beginning_year(Date)` + `beginning_year(Date)`
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.
## 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

@ -26,11 +26,16 @@
-export([ -export([
beginning_minute/1, beginning_minute/1,
beginning_minute/0,
beginning_hour/1, beginning_hour/1,
beginning_hour/0,
beginning_day/1, beginning_day/1,
beginning_day/0,
%beginning_week/2, %% needs to be /2 because we also need to define what day is considered "beginning of week", since some calendars do sunday and some do monday. We'll hold off on implementation here %beginning_week/2, %% needs to be /2 because we also need to define what day is considered "beginning of week", since some calendars do sunday and some do monday. We'll hold off on implementation here
beginning_month/1, beginning_month/1,
beginning_year/1 beginning_month/0,
beginning_year/1,
beginning_year/0
]). ]).
-export([ -export([
@ -337,22 +342,37 @@ to_now(Disamb, ToParse) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Beginning/Truncation %%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Beginning/Truncation %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
beginning_minute() ->
beginning_minute({date(),time()}).
beginning_minute(Date) -> beginning_minute(Date) ->
{{Y,M,D},{H,M,_}} = to_date(Date), {{Y,M,D},{H,M,_}} = to_date(Date),
{{Y,M,D},{H,M,0}}. {{Y,M,D},{H,M,0}}.
beginning_hour() ->
beginning_hour({date(),time()}).
beginning_hour(Date) -> beginning_hour(Date) ->
{{Y,M,D},{H,_,_}} = to_date(Date), {{Y,M,D},{H,_,_}} = to_date(Date),
{{Y,M,D},{H,0,0}}. {{Y,M,D},{H,0,0}}.
beginning_day() ->
beginning_day({date(),time()}).
beginning_day(Date) -> beginning_day(Date) ->
{{Y,M,D},{_,_,_}} = to_date(Date), {{Y,M,D},{_,_,_}} = to_date(Date),
{{Y,M,D},{0,0,0}}. {{Y,M,D},{0,0,0}}.
beginning_month() ->
beginning_month({date(),time()}).
beginning_month(Date) -> beginning_month(Date) ->
{{Y,M,_},{_,_,_}} = to_date(Date), {{Y,M,_},{_,_,_}} = to_date(Date),
{{Y,M,1},{0,0,0}}. {{Y,M,1},{0,0,0}}.
beginning_year() ->
beginning_year({date(),time()}).
beginning_year(Date) -> beginning_year(Date) ->
{{Y,_,_},{_,_,_}} = to_date(Date), {{Y,_,_},{_,_,_}} = to_date(Date),
{{Y,1,1},{0,0,0}}. {{Y,1,1},{0,0,0}}.