ecrn_util (erlcron v1.3.6)

Utility functions for epoch time conversion used internally by erlcron.

All epoch values are in milliseconds unless the function name contains _seconds.

Summary

Functions

Return the current time as milliseconds since the Unix epoch.

Return the current time as seconds since the Unix epoch.

Format an epoch millisecond timestamp as a "YYYY-MM-DD HH:MM:SS.mmm" string in local time.

Format an epoch millisecond timestamp as a "HH:MM:SS.mmm" string in local time.

Converts standard 5-field Unix cron expressions to erlcron schedule specs.

Convert a local calendar:datetime() to milliseconds since the Unix epoch.

Parse an erlcron schedule specification from a cron expression string or a tuple representing an erlcron schedule. Returns {ok, Schedule} on success or {error, Reason} on failure. The supported cron syntax is a subset of standard Unix cron expressions, with some extensions and restrictions. See from_cron/1 for details.

Convert a universal calendar:datetime() to milliseconds since the Unix epoch.

Functions

epoch_milliseconds()

-spec epoch_milliseconds() -> erlcron:milliseconds().

Return the current time as milliseconds since the Unix epoch.

epoch_seconds()

-spec epoch_seconds() -> erlcron:seconds().

Return the current time as seconds since the Unix epoch.

epoch_to_datetime_string(Epoch)

-spec epoch_to_datetime_string(erlcron:milliseconds()) -> string().

Format an epoch millisecond timestamp as a "YYYY-MM-DD HH:MM:SS.mmm" string in local time.

epoch_to_time_string(Epoch)

-spec epoch_to_time_string(erlcron:milliseconds()) -> string().

Format an epoch millisecond timestamp as a "HH:MM:SS.mmm" string in local time.

from_cron/1

-spec from_cron(string() | binary()) -> {ok, tuple()} | {error, string()}.

Converts standard 5-field Unix cron expressions to erlcron schedule specs.

Returns {ok, Schedule} on success or {error, Reason} on failure.

Cron field order

 minute      (059)
  hour        (023)
   day-of-month (131)
    month       (112, ignored  erlcron has no month scope)
     day-of-week  (07, 0/7=Sun; or mon/tue/wed/thu/fri/sat/sun)
    
* * * * *

Supported field syntax

SyntaxMeaning
*every value
Nspecific value
*/Nevery N steps
N-Minclusive range
a,b,clist of values

Supported schedule shapes and the erlcron spec they produce

Cronerlcron
* * * * *{:daily, {:every, {1, :min}}}
*/5 * * * *{:daily, {:every, {5, :min}}}
0 * * * *{:daily, {:every, {1, :hr}}}
0 */2 * * *{:daily, {:every, {2, :hr}}}
30 9 * * *{:daily, {9, 30, 0}}
0,30 9 * * *{:daily, [{9, 0, 0}, {9, 30, 0}]}
0 9 * * 1{:weekly, :mon, {9, 0, 0}}
0 9 * * 1,3{:weekly, [:mon, :wed], {9, 0, 0}}
0 9 1 * *{:monthly, 1, {9, 0, 0}}
0 9 1,15 * *{:monthly, [1, 15], {9, 0, 0}}

Note: the month field is accepted but ignored (erlcron has no month-level scope). Expressions specifying both DOM and DOW simultaneously are rejected.

The month field is accepted but ignored (erlcron has no month-level scope). Expressions specifying both DOM and DOW simultaneously are rejected.

Examples

{ok,{daily,{every,{5,min}}}} = ecrn_util:from_cron("*/5 * * * *")
{ok,{daily,{9,30,0}}}        = ecrn_util:from_cron("30 9 * * *")
{ok,{weekly,mon,{9,0,0}}}    = ecrn_util:from_cron("0 9 * * 1")
{ok,{monthly,1,{9,0,0}}}     = ecrn_util:from_cron("0 9 1 * *")

localtime_to_epoch(DT)

-spec localtime_to_epoch(calendar:datetime()) -> erlcron:milliseconds().

Convert a local calendar:datetime() to milliseconds since the Unix epoch.

parse_schedule/1

-spec parse_schedule(erlcron:schedule()) -> {ok, erlcron:schedule()} | {error, string()}.

Parse an erlcron schedule specification from a cron expression string or a tuple representing an erlcron schedule. Returns {ok, Schedule} on success or {error, Reason} on failure. The supported cron syntax is a subset of standard Unix cron expressions, with some extensions and restrictions. See from_cron/1 for details.

universaltime_to_epoch(DT)

-spec universaltime_to_epoch(calendar:datetime()) -> erlcron:milliseconds().

Convert a universal calendar:datetime() to milliseconds since the Unix epoch.