From 6ca6037f291e91cb7a694a76c60bffd46532b5e7 Mon Sep 17 00:00:00 2001 From: Jesse Gumm Date: Sat, 7 Nov 2015 11:33:16 -0600 Subject: [PATCH] Add beginning_X functions with documentation * Still need tests --- README.markdown | 12 ++++++++++++ src/qdate.erl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/README.markdown b/README.markdown index 93e7bca..5310710 100644 --- a/README.markdown +++ b/README.markdown @@ -619,6 +619,18 @@ ok %% that timezone to our intended timezone. ``` +## Date Truncation (Beginning of X) + +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. + + + `beginning_minute(Date)` + + `beginning_hour(Date)` + + `beginning_day(Date)` + + `beginning_month(Date)` + + `beginning_year(Date)` + ## Date Arithmetic The current implementation of qdate's date arithmetic returns Unixtimes. diff --git a/src/qdate.erl b/src/qdate.erl index 1d8838a..3311573 100644 --- a/src/qdate.erl +++ b/src/qdate.erl @@ -24,6 +24,15 @@ unixtime/0 ]). +-export([ + beginning_minute/1, + beginning_hour/1, + beginning_day/1, + %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_year/1 +]). + -export([ compare/2, compare/3 @@ -324,6 +333,30 @@ to_now(Disamb, ToParse) -> end. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Beginning/Truncation %%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +beginning_minute(Date) -> + {{Y,M,D},{H,M,_}} = to_date(Date), + {{Y,M,D},{H,M,0}}. + +beginning_hour(Date) -> + {{Y,M,D},{H,_,_}} = to_date(Date), + {{Y,M,D},{H,0,0}}. + +beginning_day(Date) -> + {{Y,M,D},{_,_,_}} = to_date(Date), + {{Y,M,D},{0,0,0}}. + +beginning_month(Date) -> + {{Y,M,_},{_,_,_}} = to_date(Date), + {{Y,M,1},{0,0,0}}. + +beginning_year(Date) -> + {{Y,_,_},{_,_,_}} = to_date(Date), + {{Y,1,1},{0,0,0}}. + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Comparisons %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%