z_datetime (zotonic_core v1.0.0-rc.12)

Utility functions for datetime handling and representation.

Link to this section Summary

Functions

Translate a local time date to UNIX timestamp
Return the number of days in a certain year.
Return the absolute difference between two dates. Does not take daylight saving into account.
Format the current date according to the format and the timezone settings in the context.
Format a date according to the format and the timezone settings in the context.
Format the current date in UTC.
Format the date using the UTC timezone.
Shift a date if the date falls outside the valid date or time ranges. Return undefined if the date could not be mapped to some valid date.
Return the month-boundaries of a given date
Return the date one day later.
Return the date one hour later.
Return the date one minute later.
Return the date one month later. Gives unpredictable results if the day doesn't exist in the next month. (eg. feb 30 will become feb 28).
Return the date one second later.
Return the date one week later.
Return the date one year later.
Return the date one day earlier.
Return the date one hour earlier.
Return the date one minute earlier.
Return the date one month earlier.
Return the date one second earlier.
Return the date one week earlier.
Return the date one year earlier.
Show a humanized version of a relative datetime. Like "4 months, 3 days ago".
Show a humanized version of a period between two dates. Like "4 months, 3 days ago".
Show a humanized version of a period between two dates. Like "4 months, 3 days ago". WhenText is a string containing a maximum of three tokens. Example "ago, now, in"
Calculate the current UNIX timestamp (seconds since Jan 1, 1970)
Translate UNIX timestamp to local datetime.
Convert an input to a (universal) datetime, using to_date/1 and to_time/1. When the input is a string, it is expected to be in iso 8601 format, although it can also handle timestamps without time zones. The time component of the datetime is optional.
Convert a time to the local context time using the current timezone.
Convert a time to the local context time using the current timezone.
Return 'undefined' if a given date is invalid
Return the week-boundaries of a given date. WeekStart is optional, and determines on which day a week starts.
Return the date the current week starts (monday)

Link to this section Types

-type date() :: calendar:date() | {integer(), 1..12, 1..31}.
-type datetime() :: calendar:datetime() | {{integer(), 1..12, 1..31}, {0..23, 0..59, 0..59}}.
Link to this type

fixable_datetime/0

-type fixable_datetime() ::
    datetime() |
    date() |
    {integer(), integer(), integer()} |
    {{integer(), integer(), integer()},
     {integer() | undefined, integer() | undefined, integer() | undefined}}.
-type ldom() :: 28 | 29 | 30 | 31.
-type month() :: 1..12.
-type year() :: integer().

Link to this section Functions

Link to this function

datetime_to_timestamp(DT)

Translate a local time date to UNIX timestamp
Link to this function

days_in_year(Y)

Return the number of days in a certain year.
Link to this function

diff(Date1, Date2)

Return the absolute difference between two dates. Does not take daylight saving into account.
Link to this function

format(Format, Context)

Format the current date according to the format and the timezone settings in the context.
Link to this function

format(Date, Format, Context)

Format a date according to the format and the timezone settings in the context.
Link to this function

format_utc(Format, Context)

Format the current date in UTC.
Link to this function

format_utc(Date, Format, Context)

Format the date using the UTC timezone.
Link to this function

is_leap_year(Year)

-spec is_leap_year(Year) -> boolean() when Year :: year().
Link to this function

last_day_of_the_month(Year, Month)

-spec last_day_of_the_month(Year, Month) -> LastDay
                         when Year :: year(), Month :: month(), LastDay :: ldom().
Link to this function

maybe_fix_datetime(Date)

-spec maybe_fix_datetime(fixable_datetime() | undefined) -> datetime() | undefined.
Shift a date if the date falls outside the valid date or time ranges. Return undefined if the date could not be mapped to some valid date.
Link to this function

month_boundaries(_)

Return the month-boundaries of a given date
Return the date one day later.
Link to this function

next_day(Date, N)

Link to this function

next_hour(Date)

Return the date one hour later.
Link to this function

next_hour(Date, N)

Link to this function

next_minute(Date)

Return the date one minute later.
Link to this function

next_minute(Date, N)

Return the date one month later. Gives unpredictable results if the day doesn't exist in the next month. (eg. feb 30 will become feb 28).
Link to this function

next_month(Date, N)

Link to this function

next_second(Date)

Return the date one second later.
Link to this function

next_second(Date, N)

Return the date one week later.
Link to this function

next_week(Date, N)

Link to this function

next_year(Date)

Return the date one year later.
Link to this function

next_year(Date, N)

Return the date one day earlier.
Link to this function

prev_day(Date, N)

Link to this function

prev_hour(Date)

Return the date one hour earlier.
Link to this function

prev_hour(Date, N)

Link to this function

prev_minute(Date)

Return the date one minute earlier.
Link to this function

prev_minute(Date, N)

Return the date one month earlier.
Link to this function

prev_month(DT, N)

Link to this function

prev_second(Date)

Return the date one second earlier.
Link to this function

prev_second(Date, N)

Return the date one week earlier.
Link to this function

prev_week(Date, N)

Link to this function

prev_year(Date)

Return the date one year earlier.
Link to this function

prev_year(Date, N)

Link to this function

timesince(Date, Context)

Show a humanized version of a relative datetime. Like "4 months, 3 days ago".
Link to this function

timesince(Date, Base, Context)

Show a humanized version of a period between two dates. Like "4 months, 3 days ago".
Link to this function

timesince(Date, Base, IndicatorStrings, Context)

Link to this function

timesince(Date, Base, IndicatorStrings, Mode, Context)

Show a humanized version of a period between two dates. Like "4 months, 3 days ago". WhenText is a string containing a maximum of three tokens. Example "ago, now, in"
Calculate the current UNIX timestamp (seconds since Jan 1, 1970)
Link to this function

timestamp_to_datetime(Seconds)

Translate UNIX timestamp to local datetime.
Link to this function

to_datetime(Input)

-spec to_datetime(Input) -> calendar:datetime()
               when
                   Input ::
                       undefined |
                       binary() |
                       string() |
                       integer() |
                       calendar:datetime() |
                       {Y :: integer(), M :: pos_integer(), D :: pos_integer()}.
Convert an input to a (universal) datetime, using to_date/1 and to_time/1. When the input is a string, it is expected to be in iso 8601 format, although it can also handle timestamps without time zones. The time component of the datetime is optional.
Link to this function

to_datetime(DT, Tz)

Link to this function

to_local(Date, Tz)

-spec to_local(calendar:datetime() | undefined | time_not_exists, string() | binary() | z:context()) ->
            calendar:datetime() | undefined.
Convert a time to the local context time using the current timezone.
Link to this function

to_utc(Date, Tz)

-spec to_utc(calendar:datetime() | undefined | time_not_exists, string() | binary() | z:context()) ->
          calendar:datetime() | undefined.
Convert a time to the local context time using the current timezone.
Link to this function

undefined_if_invalid_date(Date)

Return 'undefined' if a given date is invalid
Link to this function

week_boundaries(Date)

Return the week-boundaries of a given date. WeekStart is optional, and determines on which day a week starts.
Link to this function

week_boundaries(Date, WeekStart)

Return the date the current week starts (monday)
Link to this function

week_start(StartDayNr, _)