# Overview
Jam (from an Indonesian word for "clock" or "time") is a time parsing
and processing library written to allow Riak's timeseries database to
use fine-grained date and time types.

Jam's initial release includes an ISO 8601 parser/formatter, but the
architecture allows for any syntax to be supported by adding a new
module that understands Jam's date/time structures.

There are several phases of processing available to allow an
application to massage the data for more sophisticated purposes than
merely translating strings to UNIX epoch time.

## Phases

### Parsing

Currently Jam supports (most of) ISO 8601 but other formats are

Yet to be supported:

* Date/time intervals
* Week dates

See `jam_iso8601:parse/{1,2}`.

### Compilation

The compilation step (`jam:compile/{1,2}`) converts the strings
captured by parsing into a (possibly valid) date and/or time. The
resulting tuples will not be the same as Erlang's date/time tuples
because this library permits fractional seconds, "incomplete" dates
and times, and time zones.

### Validation

Validation is entirely optional. It is possible to extensively
manipulate invalid date/time values, and developers are advised to
validate at whatever point in the pipeline seems reasonable (but no
earlier than compilation).

The compilation step only exercises as much knowledge about "real"
times and dates as is necessary (e.g., knowing what years are leap
years to properly interpret ordinal dates). A time such as "25:15" is
a possible outcome of the parsing and compilation steps, so validation
functions `is_valid/{1,2}` are supplied.

Also see **Expansion** below for a discussion of incompleteness. An
incomplete date or time can still be valid.

### Normalization

There are two edge case time values which may arise, and which are
corrected for using `jam:normalize/1`.

The first, permitted by ISO 8601, is "24:00". This is *not* the same
as "00:00", at least when also attached to a date. "2016-06-15 24:00"
is the same as "2016-06-16 00:00" and normalization will convert the
former into the latter.

The second unusual time value: seconds > 59.

Occasionally [leap seconds](
will be added at the end of a day. UNIX/POSIX time will silently
["absorb"]( that
into the first second of the following day.

Thus, a second value of `60` is considered valid by the validation
functions and is converted to `00` seconds in the following minute
during normalization.

### Expansion

ISO 8601 allows for incomplete date/time strings.

We want a complete date/time for some conversions, so expansion of an
incomplete date/time record via `jam:expand/2` is an optional step in
the processing timeline.

The values added to expand an incomplete record are the lower bounds
for the appropriate fields, so 1 for month or day, 0 for any time

`jam:is_complete/1` can be used to test a datetime structure (compiled
or not) for completeness. Use `jam:is_complete_date/1` or
`jam:is_complete_time/1` for dates and times outside a datetime

It may be useful to adjust a compiled, incomplete date/time via
`jam:increment/2`. This identifies the least significant populated
value and adds an integer value. One example is strictly greater-than
comparisons in Riak's timeseries support.

### Translation

Dates and times can be converted to alternative time zones
(`jam:convert_tz/2`), to ISO 8601 (and, in the future, other formats)
(`jam_iso8601:to_string/{1,2}`), to Erlang's date/time tuples (see
`jam_erlang`), or to integers (UNIX epoch with support for sub-second
values via `jam:to_epoch/{1,2}`).

`jam:round_fractional_seconds/1` is recommended before converting to
Erlang tuples.

Jam's internal records are not intended to be manipulated or examined
by code outside the library, thus doing so places your code at risk of
breaking with future releases.

UTC epoch integers can also be used to create jam datetime records:
see `jam:from_epoch/{1,2}`.

## Illustrated usage

1> jam_iso8601:parse("1985-04-23 13:15:57Z").
2> jam:compile(v(-1)).
3> jam:normalize(v(-1)).
4> jam:to_epoch(v(-1)).
5> jam_iso8601:to_string(v(-2)).
6> jam_iso8601:to_string(v(-3), [{format, basic}]).
7> jam_iso8601:to_string(v(-4), [{format, basic}, {z, false}]).

For a more sophisticated example, we handle a leap second with a
fractional component and time zone conversion.

Note that in order to support passing times without dates as
arguments, `jam:offset_normalize/1`,
`jam:offset_round_fractional_seconds/1` and `jam:offset_convert_tz/2`
return a two-tuple, with the first value an integer expressing whether
or not a date adjustment resulted. The non-`offset_` version of each
function drops the date adjustment in favor of a single return value, and we'll use those here.

1> DT1 = jam:normalize(jam:compile(jam_iso8601:parse("20150630T23:59:60.738Z"))).
2> DT2 = jam:round_fractional_seconds(DT1).
3> DT3 = jam:convert_tz(DT2, "-07:30").
4> jam_iso8601:to_string(DT3).

Here's an example of expanding an incomplete date. Note that if we specify a target for our expansion of less than a day, the original date record becomes a datetime so we can expand the time as well.

2> DT = jam:compile(jam_iso8601:parse("2016")).
#date{year = 2016,month = undefined,day = undefined}
3> jam:expand(DT, day).
#date{year = 2016,month = 1,day = 1}
4> jam:expand(DT, minute).
#datetime{date = #date{year = 2016,month = 1,day = 1},
          time = #time{hour = 0,minute = 0,second = undefined,
                       fraction = undefined,subsecond = undefined,
                       timezone = undefined}}

Dates or times can be managed independently. This snippet also
illustrates compiling the ISO 8601 regular expressions for better
parsing performance.

1> REs = jam_iso8601:init().
2> Time = jam_iso8601:parse(REs, "T14:15:16-05").

Jam also allows Erlang datetime tuples to be converted to strings.

The `iso8601_tests` module further illustrates usage of the `jam`

## Joe's Abstract Machine

The term "JAM" in Erlang originally referred to Joe Armstrong's
prolog-based Erlang VM. The name collision is purely coincidental, but
convenient in that there appear to be no existing Erlang libraries
named `jam` (perhaps as a consequence).