GoodTimes

Convenient and expressive functions dealing with dates and times.

This is the core module of the GoodTimes library. For other modules and their functionality, see Associated modules below.

Unless explicitly stated, all functions operate on and return an Erlang datetime based on the Coordinated Universal Time (UTC).

Functions overview

The following functions are available for each time unit (second, minute, hour, day, week, month, or year); all return a datetime that is offset the specified amount of time units.

You can use the following functions when only adjusting the datetime by a single time unit.

In addition, now/0 and at/2 are included for convenience.

Examples

iex> now
{{2015, 2, 27}, {18, 30, 45}}
iex> an_hour_ago
{{2015, 2, 27}, {17, 30, 45}}
iex> a_month_before {{2016, 3, 31}, {9, 30, 0}}
{{2016, 2, 29}, {9, 30, 0}}
iex> 2 |> weeks_from_now |> at {12, 15, 0}
{{2015, 3, 13}, {12, 15, 0}}

Known limitations

As the entire library builds upon Erlang’s calendar module, dates before year 0 are not supported.

Associated modules

Aside from the core module, the GoodTimes library consistes of these modules.

Source

Summary

a_day_after(datetime)

Returns the UTC date and time a day after the given datetime

a_day_ago()

Returns the UTC date and time a day ago

a_day_before(datetime)

Returns the UTC date and time a day before the given datetime

a_day_from_now()

Returns the UTC date and time a day from now

a_minute_after(datetime)

Returns the UTC date and time a minute after the given datetime

a_minute_ago()

Returns the UTC date and time a minute ago

a_minute_before(datetime)

Returns the UTC date and time a minute before the given datetime

a_minute_from_now()

Returns the UTC date and time a minute from now

a_month_after(datetime)

Returns the UTC date and time a month after the given datetime

a_month_ago()

Returns the UTC date and time a month ago

a_month_before(datetime)

Returns the UTC date and time a month before the given datetime

a_month_from_now()

Returns the UTC date and time a month from now

a_second_after(datetime)

Returns the UTC date and time a second after the given datetime

a_second_ago()

Returns the UTC date and time a second ago

a_second_before(datetime)

Returns the UTC date and time a second before the given datetime

a_second_from_now()

Returns the UTC date and time a second from now

a_week_after(datetime)

Returns the UTC date and time a week after the given datetime

a_week_ago()

Returns the UTC date and time a week ago

a_week_before(datetime)

Returns the UTC date and time a week before the given datetime

a_week_from_now()

Returns the UTC date and time a week from now

a_year_after(datetime)

Returns the UTC date and time a year after the given datetime

a_year_ago()

Returns the UTC date and time a year ago

a_year_before(datetime)

Returns the UTC date and time a year before the given datetime

a_year_from_now()

Returns the UTC date and time a year from now

an_hour_after(datetime)

Returns the UTC date and time an hour after the given datetime

an_hour_ago()

Returns the UTC date and time an hour ago

an_hour_before(datetime)

Returns the UTC date and time an hour before the given datetime

an_hour_from_now()

Returns the UTC date and time an hour from now

at(date_or_datetime, time)

Merges the date from the given date or datetime with the given time

days_after(days, datetime)

Returns the UTC date and time the specified days after the given datetime

days_ago(days)

Returns the UTC date and time the specified days ago

days_before(days, datetime)

Returns the UTC date and time the specified days before the given datetime

days_from_now(days)

Returns the UTC date and time the specified days from now

hours_after(hours, datetime)

Returns the UTC date and time the specified hours after the given datetime

hours_ago(hours)

Returns the UTC date and time the specified hours ago

hours_before(hours, datetime)

Returns the UTC date and time the specified hours before the given datetime

hours_from_now(hours)

Returns the UTC date and time the specified hours from now

minutes_after(minutes, datetime)

Returns the UTC date and time the specified minutes after the given datetime

minutes_ago(minutes)

Returns the UTC date and time the specified minutes ago

minutes_before(minutes, datetime)

Returns the UTC date and time the specified minutes before the given datetime

minutes_from_now(minutes)

Returns the UTC date and time the specified minutes from now

months_after(months, arg2)

Returns the UTC date and time the specified months after the given datetime

months_ago(months)

Returns the UTC date and time the specified months ago

months_before(months, datetime)

Returns the UTC date and time the specified months before the given datetime

months_from_now(months)

Returns the UTC date and time the specified months from now

now()

Returns the current UTC time as a datetime

seconds_after(seconds, datetime)

Returns the UTC date and time the specified seconds after the given datetime

seconds_ago(seconds)

Returns the UTC date and time the specified seconds ago

seconds_before(seconds, datetime)

Returns the UTC date and time the specified seconds before the given datetime

seconds_from_now(seconds)

Returns the UTC date and time the specified seconds from now

weeks_after(weeks, datetime)

Returns the UTC date and time the specified weeks after the given datetime

weeks_ago(weeks)

Returns the UTC date and time the specified weeks ago

weeks_before(weeks, datetime)

Returns the UTC date and time the specified weeks before the given datetime

weeks_from_now(weeks)

Returns the UTC date and time the specified weeks from now

years_after(years, datetime)

Returns the UTC date and time the specified years after the given datetime

years_ago(years)

Returns the UTC date and time the specified years ago

years_before(years, datetime)

Returns the UTC date and time the specified years before the given datetime

years_from_now(years)

Returns the UTC date and time the specified years from now

Types

year :: non_neg_integer

month :: 1 .. 12

day :: 1 .. 31

hour :: 0 .. 23

minute :: 0 .. 59

second :: 0 .. 59

date :: {year, month, day}

Functions

a_day_after(datetime)

Specs:

Returns the UTC date and time a day after the given datetime.

Examples

iex> a_day_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 28}, {18, 30, 45}}
Source
a_day_ago()

Specs:

Returns the UTC date and time a day ago.

Examples

iex> a_day_ago
{{2015, 2, 26}, {18, 30, 45}}
Source
a_day_before(datetime)

Specs:

Returns the UTC date and time a day before the given datetime.

Examples

iex> a_day_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 26}, {18, 30, 45}}
Source
a_day_from_now()

Specs:

Returns the UTC date and time a day from now.

Examples

iex> a_day_from_now
{{2015, 2, 28}, {18, 30, 45}}
Source
a_minute_after(datetime)

Specs:

Returns the UTC date and time a minute after the given datetime.

Examples

iex> a_minute_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {18, 31, 45}}
Source
a_minute_ago()

Specs:

Returns the UTC date and time a minute ago.

Examples

iex> a_minute_ago
{{2015, 2, 27}, {18, 29, 45}}
Source
a_minute_before(datetime)

Specs:

Returns the UTC date and time a minute before the given datetime.

Examples

iex> a_minute_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {18, 29, 45}}
Source
a_minute_from_now()

Specs:

Returns the UTC date and time a minute from now.

Examples

iex> a_minute_from_now
{{2015, 2, 27}, {18, 31, 45}}
Source
a_month_after(datetime)

Specs:

Returns the UTC date and time a month after the given datetime.

Examples

iex> a_month_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 3, 27}, {18, 30, 45}}
Source
a_month_ago()

Specs:

Returns the UTC date and time a month ago.

Examples

iex> a_month_ago
{{2015, 1, 27}, {18, 30, 45}}
Source
a_month_before(datetime)

Specs:

Returns the UTC date and time a month before the given datetime.

Examples

iex> a_month_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 1, 27}, {18, 30, 45}}
Source
a_month_from_now()

Specs:

Returns the UTC date and time a month from now.

Examples

iex> a_month_from_now
{{2015, 3, 27}, {18, 30, 45}}
Source
a_second_after(datetime)

Specs:

Returns the UTC date and time a second after the given datetime.

Examples

iex> a_second_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {18, 30, 46}}
Source
a_second_ago()

Specs:

Returns the UTC date and time a second ago.

iex> a_second_ago
{{2015, 2, 27}, {18, 30, 44}}
Source
a_second_before(datetime)

Specs:

Returns the UTC date and time a second before the given datetime.

Examples

iex> a_second_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {18, 30, 44}}
Source
a_second_from_now()

Specs:

Returns the UTC date and time a second from now.

Examples

iex> a_second_from_now
{{2015, 2, 27}, {18, 30, 46}}
Source
a_week_after(datetime)

Specs:

Returns the UTC date and time a week after the given datetime.

Examples

iex> a_week_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 3, 6}, {18, 30, 45}}
Source
a_week_ago()

Specs:

Returns the UTC date and time a week ago.

Examples

iex> a_week_ago
{{2015, 2, 20}, {18, 30, 45}}
Source
a_week_before(datetime)

Specs:

Returns the UTC date and time a week before the given datetime.

Examples

iex> a_week_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 20}, {18, 30, 45}}
Source
a_week_from_now()

Specs:

Returns the UTC date and time a week from now.

Examples

iex> a_week_from_now
{{2015, 3, 6}, {18, 30, 45}}
Source
a_year_after(datetime)

Specs:

Returns the UTC date and time a year after the given datetime.

Examples

iex> a_year_after {{2015, 2, 27}, {18, 30, 45}}
{{2016, 2, 27}, {18, 30, 45}}
Source
a_year_ago()

Specs:

Returns the UTC date and time a year ago.

Examples

iex> a_year_ago
{{2014, 2, 27}, {18, 30, 45}}
Source
a_year_before(datetime)

Specs:

Returns the UTC date and time a year before the given datetime.

Examples

iex> a_year_before {{2015, 2, 27}, {18, 30, 45}}
{{2014, 2, 27}, {18, 30, 45}}
Source
a_year_from_now()

Specs:

Returns the UTC date and time a year from now.

Examples

iex> a_year_from_now
{{2016, 2, 27}, {18, 30, 45}}
Source
an_hour_after(datetime)

Specs:

Returns the UTC date and time an hour after the given datetime.

Examples

iex> an_hour_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {19, 30, 45}}
Source
an_hour_ago()

Specs:

Returns the UTC date and time an hour ago.

Examples

iex> an_hour_ago
{{2015, 2, 27}, {17, 30, 45}}
Source
an_hour_before(datetime)

Specs:

Returns the UTC date and time an hour before the given datetime.

Examples

iex> an_hour_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {17, 30, 45}}
Source
an_hour_from_now()

Specs:

Returns the UTC date and time an hour from now.

Examples

iex> an_hour_from_now
{{2015, 2, 27}, {19, 30, 45}}
Source
at(date_or_datetime, time)

Specs:

Merges the date from the given date or datetime with the given time.

Examples

iex> now |> at {10, 30, 15}
{{2015, 2, 27}, {10, 30, 15}}
iex> {2015, 2, 27} |> at {10, 30, 15}
{{2015, 2, 27}, {10, 30, 15}}
Source
days_after(days, datetime)

Specs:

Returns the UTC date and time the specified days after the given datetime.

Examples

iex> 3 |> days_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 3, 2}, {18, 30, 45}}
Source
days_ago(days)

Specs:

Returns the UTC date and time the specified days ago.

Examples

iex> 7 |> days_ago
{{2015, 2, 20}, {18, 30, 45}}
Source
days_before(days, datetime)

Specs:

Returns the UTC date and time the specified days before the given datetime.

Examples

iex> 3 |> days_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 24}, {18, 30, 45}}
Source
days_from_now(days)

Specs:

Returns the UTC date and time the specified days from now.

Examples

iex> 2 |> days_from_now
{{2015, 3, 1}, {18, 30, 45}}
Source
hours_after(hours, datetime)

Specs:

Returns the UTC date and time the specified hours after the given datetime.

Examples

iex> 15 |> hours_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 28}, {9, 30, 45}}
Source
hours_ago(hours)

Specs:

Returns the UTC date and time the specified hours ago.

Examples

iex> 2 |> hours_ago
{{2015, 2, 27}, {16, 30, 45}}
Source
hours_before(hours, datetime)

Specs:

Returns the UTC date and time the specified hours before the given datetime.

Examples

iex> 15 |> hours_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {3, 30, 45}}
Source
hours_from_now(hours)

Specs:

Returns the UTC date and time the specified hours from now.

Examples

iex> 6 |> hours_from_now
{{2015, 2, 28}, {0, 30, 45}}
Source
minutes_after(minutes, datetime)

Specs:

Returns the UTC date and time the specified minutes after the given datetime.

Examples

iex> 15 |> minutes_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {18, 45, 45}}
Source
minutes_ago(minutes)

Specs:

Returns the UTC date and time the specified minutes ago.

Examples

iex> 5 |> minutes_ago
{{2015, 2, 27}, {18, 25, 45}}
Source
minutes_before(minutes, datetime)

Specs:

Returns the UTC date and time the specified minutes before the given datetime.

Examples

iex> 15 |> minutes_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {18, 15, 45}}
Source
minutes_from_now(minutes)

Specs:

Returns the UTC date and time the specified minutes from now.

Examples

iex> 10 |> minutes_from_now
{{2015, 2, 27}, {18, 40, 45}}
Source
months_after(months, arg2)

Specs:

Returns the UTC date and time the specified months after the given datetime.

Examples

iex> 3 |> months_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 5, 27}, {18, 30, 45}}
Source
months_ago(months)

Specs:

Returns the UTC date and time the specified months ago.

Examples

iex> 2 |> months_ago
{{2014, 12, 27}, {18, 30, 45}}
Source
months_before(months, datetime)

Specs:

Returns the UTC date and time the specified months before the given datetime.

Examples

iex> 3 |> months_before {{2015, 2, 27}, {18, 30, 45}}
{{2014, 11, 27}, {18, 30, 45}}
Source
months_from_now(months)

Specs:

Returns the UTC date and time the specified months from now.

Examples

iex> 2 |> months_from_now
{{2015, 4, 27}, {18, 30, 45}}
Source
now()

Specs:

Returns the current UTC time as a datetime.

Examples

iex> now
{{2015, 2, 27}, {18, 30, 45}}
Source
seconds_after(seconds, datetime)

Specs:

Returns the UTC date and time the specified seconds after the given datetime.

Examples

iex> 15 |> seconds_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {18, 31, 0}}
Source
seconds_ago(seconds)

Specs:

Returns the UTC date and time the specified seconds ago.

Examples

iex> 20 |> seconds_ago
{{2015, 2, 27}, {18, 30, 25}}
Source
seconds_before(seconds, datetime)

Specs:

Returns the UTC date and time the specified seconds before the given datetime.

Examples

iex> 15 |> seconds_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 27}, {18, 30, 30}}
Source
seconds_from_now(seconds)

Specs:

Returns the UTC date and time the specified seconds from now.

Examples

iex> 15 |> seconds_from_now
{{2015, 2, 27}, {18, 31, 0}}
Source
weeks_after(weeks, datetime)

Specs:

Returns the UTC date and time the specified weeks after the given datetime.

Examples

iex> 3 |> weeks_after {{2015, 2, 27}, {18, 30, 45}}
{{2015, 3, 20}, {18, 30, 45}}
Source
weeks_ago(weeks)

Specs:

Returns the UTC date and time the specified weeks ago.

Examples

iex> 2 |> weeks_ago
{{2015, 2, 13}, {18, 30, 45}}
Source
weeks_before(weeks, datetime)

Specs:

Returns the UTC date and time the specified weeks before the given datetime.

Examples

iex> 3 |> weeks_before {{2015, 2, 27}, {18, 30, 45}}
{{2015, 2, 6}, {18, 30, 45}}
Source
weeks_from_now(weeks)

Specs:

Returns the UTC date and time the specified weeks from now.

Examples

iex> 2 |> weeks_from_now
{{2015, 3, 13}, {18, 30, 45}}
Source
years_after(years, datetime)

Specs:

Returns the UTC date and time the specified years after the given datetime.

Examples

iex> 3 |> years_after {{2015, 2, 27}, {18, 30, 45}}
{{2018, 2, 27}, {18, 30, 45}}
Source
years_ago(years)

Specs:

Returns the UTC date and time the specified years ago.

Examples

iex> 2 |> years_ago
{{2013, 2, 27}, {18, 30, 45}}
Source
years_before(years, datetime)

Specs:

Returns the UTC date and time the specified years before the given datetime.

Examples

iex> 3 |> years_before {{2015, 2, 27}, {18, 30, 45}}
{{2012, 2, 27}, {18, 30, 45}}
Source
years_from_now(years)

Specs:

Returns the UTC date and time the specified years from now.

Examples

iex> 2 |> years_from_now
{{2017, 2, 27}, {18, 30, 45}}
Source