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
-spec epoch_milliseconds() -> erlcron:milliseconds().
Return the current time as milliseconds since the Unix epoch.
-spec epoch_seconds() -> erlcron:seconds().
Return the current time as seconds since the Unix 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.
-spec epoch_to_time_string(erlcron:milliseconds()) -> string().
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.
Returns {ok, Schedule} on success or {error, Reason} on failure.
Cron field order
┌───────── minute (0–59)
│ ┌─────── hour (0–23)
│ │ ┌───── day-of-month (1–31)
│ │ │ ┌─── month (1–12, ignored – erlcron has no month scope)
│ │ │ │ ┌─ day-of-week (0–7, 0/7=Sun; or mon/tue/wed/thu/fri/sat/sun)
│ │ │ │ │
* * * * *Supported field syntax
| Syntax | Meaning |
|---|---|
* | every value |
N | specific value |
*/N | every N steps |
N-M | inclusive range |
a,b,c | list of values |
Supported schedule shapes and the erlcron spec they produce
| Cron | erlcron |
|---|---|
* * * * * | {: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 * *")
-spec localtime_to_epoch(calendar:datetime()) -> erlcron:milliseconds().
Convert a local calendar:datetime() to milliseconds since the Unix epoch.
-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.
-spec universaltime_to_epoch(calendar:datetime()) -> erlcron:milliseconds().
Convert a universal calendar:datetime() to milliseconds since the Unix epoch.