Crate temporenc [] [src]

An implementation of Temporenc v1, a compact binary format for temporal data. This is (intentionally) not a full-featured time library; it is focused just on handling the Temporenc format.

Temporenc has the concept of "components" like date or time, and "types", which are a composition of one ore more components like "date + time" or just "time". Components are repesented in this library by traits (Time, Date, etc) and types by structs (TimeOnly which only implements Time, DateTime which implements Date and Time, etc).

All of the fields in a component ("day" in Date, "minute" in Time, etc) are optional, so the accessors expose Option<T> or an enum with a None variant.

All of the structs implement Serializable and Deserializable which, surprisingly enough, provide methods related to serialization and deserialization.

use temporenc::*;
use std::io::Cursor;

let mut vec = Vec::new();

// Serialize a date
// 2017-01-15
let date = DateOnly::new(Some(2017), Some(01), Some(15)).unwrap();

let date_bytes_written = date.serialize(&mut vec).unwrap();
assert_eq!(date_bytes_written, date.serialized_size());
// Date is not variable precision, so the serialized size is always the max size.
assert_eq!(DateOnly::max_serialized_size(), date.serialized_size());

// Serialize a date + time + subsecond precision + offset
// 2017-01-15T18:45:30.123456+02:15
let dtso = DateTimeSubSecondOffset::new(Some(2017), Some(01), Some(15),
    Some(18), Some(45), Some(30), FractionalSecond::Microseconds(123456),
    OffsetValue::UtcOffset(135)).unwrap();

let dtso_bytes_written = dtso.serialize(&mut vec).unwrap();
assert_eq!(dtso.serialized_size(), dtso_bytes_written);
// This one is only microseconds, not nanoseconds, so it doesn't have the full size
assert_eq!(DateTimeSubSecondOffset::max_serialized_size() - 1,
    dtso.serialized_size());

// Deserialize the two items
assert_eq!(date.serialized_size() + dtso.serialized_size(), vec.len());
let mut cursor = Cursor::new(vec.as_slice());

let deser_date = DateOnly::deserialize(&mut cursor).unwrap();
assert_eq!(date, deser_date);

let deser_dtso =
    DateTimeSubSecondOffset::deserialize(&mut cursor).unwrap();
assert_eq!(dtso, deser_dtso);

Structs

DateOnly

Just a Date.

DateTime

A Date and Time.

DateTimeOffset

A Date and Time with UTC Offset.

DateTimeSubSecond

A Date and Time with subsecond precision.

DateTimeSubSecondOffset

A Date and Time with subsecond precision and UTC offset.

TimeOnly

Just a Time.

Enums

CreationError

Used when creating a struct via ::new().

DeserializationError
FractionalSecond
OffsetValue
SerializationError

Constants

DAY_MAX
DAY_MIN
HOUR_MAX
HOUR_MIN
MICROS_MAX
MICROS_MIN
MILLIS_MAX
MILLIS_MIN
MINUTE_MAX
MINUTE_MIN
MONTH_MAX
MONTH_MIN
NANOS_MAX
NANOS_MIN
OFFSET_MAX
OFFSET_MIN
SECOND_MAX
SECOND_MIN
YEAR_MAX
YEAR_MIN

Traits

Date

Represents the Temporenc "Date" component.

Deserializable

Deserialize from the Temporenc binary format.

Offset

Represents the Temporenc "UTC Offset" component.

Serializable

Serialize into the Temporenc binary format.

SubSecond

Represents the Temporenc "Sub-second precision" component.

Time

Represents the Temporenc "Time" component.