Skip to content

Commit

Permalink
Add end_week/[0-2] and beginning_week/0
Browse files Browse the repository at this point in the history
  • Loading branch information
choptastic committed Jan 5, 2017
1 parent c42272a commit aa114b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
21 changes: 15 additions & 6 deletions README.markdown
Expand Up @@ -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.
Expand All @@ -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).
Expand All @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions src/qdate.erl
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down

0 comments on commit aa114b1

Please sign in to comment.