Module tempo

April 15, 2014 ยท View on GitHub

NIF-based date and time parsing and formatting for Erlang. Copyright (c) 2012 Selectel Ltd.

Authors: Dmitry Groshev (groshev@selectel.ru), Sergey Levedev (lebedev@selectel.ru).

Description

This module implements an interface to strptime/strftime with appropriate handling of Erlang datetime formats.

All exported functions in this module can throw badarg if malformed input is provided.

A Type argument, accepted by some of the exported functions should be one of the following:

   | Type     | Description                                        |
   |----------+----------------------------------------------------|
   | unix     | UNIX timestamp, a positive integer denoting number |
   |          | of seconds since 1 Jan 1970.                       |
   | now      | @see erlang:now/0                                  |
   | datetime | @see calendar:datetime/0                           |

A Format argument to any of the exported functions is either a binary() with strptime/strftime compatible tokens or one of the following atoms: iso8601, rfc1123, rfc2822. In the latter case a predefined format will be used.

A note about 32-bit systems

Functions of "format" family can return "{error, time_overflow}" if the underlying 32-bit value overflows. This is presumably possible only on 32-bit systems. Minimum datetime for such systems is {{1901,12,13},{20,45,52}} and maximum is {{2038,1,19},{3,14,7}}.

Data Types

datetime_type()


datetime_type() = unix | now | datetime

datetime_value()


datetime_value() = unix_timestamp() | erlang:timestamp() | calendar:datetime()

format()


format() = binary() | iso8601 | rfc1123 | rfc2822

unix_timestamp()


unix_timestamp() = float()

Function Index

format/2Formats {Type, Datetime} tuple according to Format.
format/3Formats Datetime according to Format.
format_datetime/2Helper function similar to format/3.
format_now/2Helper function similar to format/3.
format_unix/2Helper function similar to format/3.
parse/2Parses {Type, Binary} tuple according to provided format, returns ok/error tuples with datetime in format that depends on atom Type.
parse/3Parses Binary according to Format and returns ok/error tuple with datetime in format that depends on atom Type.
parse_datetime/2Helper function similar to parse/3.
parse_now/2Helper function similar to parse/3.
parse_unix/2Helper function similar to parse/3.

Function Details

format/2


format(Format::format(), X2::{datetime_type(), datetime_value()}) -> {ok, binary()} | {error, invalid_time} | {error, time_overflow}



Equivalent to format(Format, Datetime, Type).

Formats {Type, Datetime} tuple according to Format. The way in which Datetime will be handled depends on Type.

format/3


format(Format::format(), Datetime::datetime_value(), Type::datetime_type()) -> {ok, binary()} | {error, invalid_time} | {error, time_overflow}



Formats Datetime according to Format. The way in which Datetime will be handled depends on Type.

format_datetime/2


format_datetime(Format::format(), Datetime::calendar:datetime()) -> {ok, binary()} | {error, invalid_time} | {error, time_overflow}



Equivalent to format(Format, Datetime, datetime).

Helper function similar to format/3.

format_now/2


format_now(Format::format(), X2::erlang:timestamp()) -> {ok, binary()} | {error, invalid_time} | {error, time_overflow}



Equivalent to format(Format, Datetime, now).

Helper function similar to format/3.

format_unix/2


format_unix(Format::format(), Timestamp::unix_timestamp()) -> {ok, binary()} | {error, invalid_time} | {error, time_overflow}



Equivalent to format(Format, Datetime, timestamp).

Helper function similar to format/3.

parse/2


parse(Format::format(), X2::{datetime_type(), binary()}) -> {ok, datetime_value()} | {error, format_mismatch}



Equivalent to parse(Format, DatetimeType, Binary).

Parses {Type, Binary} tuple according to provided format, returns ok/error tuples with datetime in format that depends on atom Type.

parse/3


parse(Format::format(), Bin::binary(), Type::datetime_type()) -> {ok, datetime_value()} | {error, format_mismatch}



Parses Binary according to Format and returns ok/error tuple with datetime in format that depends on atom Type.

parse_datetime/2


parse_datetime(Format::format(), Bin::binary()) -> {ok, calendar:datetime()} | {error, format_mismatch}



Equivalent to parse(Format, Binary, datetime).

Helper function similar to parse/3.

parse_now/2


parse_now(Format::format(), Bin::binary()) -> {ok, erlang:timestamp()} | {error, format_mismatch}



Equivalent to parse(Format, Binary, now).

Helper function similar to parse/3.

parse_unix/2


parse_unix(Format::format(), Bin::binary()) -> {ok, unix_timestamp()} | {error, format_mismatch}



Equivalent to parse(Format, Binary, timestamp).

Helper function similar to parse/3.