selectel / tempo

NIF-based date and time parsing and formatting for Erlang
MIT License
67 stars 10 forks source link
community datetime erlang

tempo Build Status

Authors: Dmitry Groshev (lambdadmitry@gmail.com), Sergei Levedev (superbobry@gmail.com).

tempo is a library for parsing and formatting dates in Erlang. It provides a clean and nice interface to libc's strptime and strftime functions, which are unfortunately missing from Erlang's standard library.

Is it any good?

Yes.

How can I use it?

The only two functions you have to remember are tempo:parse/2 and tempo:format/2. Here are some examples:

1> {ok, Bin} = tempo:format(iso8601, {now, now()}).
{ok,<<"2016-01-11T19:25:26Z">>}
2> tempo:parse(iso8601, {datetime, Bin}).
{ok,{{2016,1,11},{19,25,26}}}

As you might have noticed, both of the functions follow a common pattern -- Format first, then a Value, tagged by its actual or expected type. Predefined formats include: iso8601, rfc1123, and rfc2822, but in fact, you can use any format, as long as it follows libc conventions:

(tempo_dev@localhost)1> {ok, Bin} = tempo:format(<<"%A, %Y-%d-%m">>, {now, now()}).
{ok,<<"Thursday, 2012-07-06">>}

Limitations

Unfortunately, dealing with time on various platforms is messy, so limitations are unavoidable. Here's a shortlist of those we know of:

Modules

tempo