Expand description
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§
- Date
Only - Just a Date.
- Date
Time - A Date and Time.
- Date
Time Offset - A Date and Time with UTC Offset.
- Date
Time SubSecond - A Date and Time with subsecond precision.
- Date
Time SubSecond Offset - A Date and Time with subsecond precision and UTC offset.
- Time
Only - Just a Time.
Enums§
- Creation
Error - Used when creating a struct via
::new(). - Deserialization
Error - Fractional
Second - Offset
Value - Serialization
Error
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.