pub struct DateTime { /* private fields */ }Expand description
An OCPI timestamp: an instant in UTC, serialised as RFC 3339 with a Z designator.
All timestamps are formatted as string(25) following RFC 3339, with some additional limitations. All timestamps SHALL be in UTC. The absence of the timezone designator implies a UTC timestamp. Fractional seconds MAY be used.
The six forms the spec lists as the only allowed ones are, by example:
2015-06-29T20:39:09Z 2015-06-29T20:39:09
2016-12-29T17:45:09.2Z 2016-12-29T17:45:09.2
2018-01-01T01:08:01.123Z 2018-01-01T01:08:01.123§+00:00 is not Z
The spec is explicit: “NOTE: +00:00 is not the same as UTC.” A DateTime parsed from a
timestamp carrying an explicit offset — even +00:00 — is converted to the correct UTC
instant so no data is lost, but is flagged: DateTime::is_canonical returns false and
Validate::validate reports it. Serialising always emits the canonical Z form, so a
value that passes through this crate comes out conformant.
§Fractional seconds are preserved
A timestamp read as …09.2Z is written back as …09.2Z, not …09.200Z. The number of
fractional digits is formatting metadata: it takes no part in PartialEq, Ord or
std::hash::Hash, which compare the instant alone.
use ocpi_kit::types::DateTime;
let t: DateTime = "2016-12-29T17:45:09.2Z".parse().unwrap();
assert_eq!(t.to_string(), "2016-12-29T17:45:09.2Z");
assert_eq!(t, "2016-12-29T17:45:09.200Z".parse::<DateTime>().unwrap());Spec: 2.3.0 §types_datetime_type
Implementations§
Source§impl DateTime
impl DateTime
Sourcepub fn local_parts(self, offset_seconds: i32) -> LocalParts
pub fn local_parts(self, offset_seconds: i32) -> LocalParts
The wall clock this instant shows in a zone offset_seconds east of UTC.
Every time-of-day and day-of-week rule in OCPI is written in a Location’s local time —
tariff restrictions, opening hours — so reading one means converting first. The result is
in this crate’s own LocalParts, so no date-time library
reaches the API.
The offset has to come from somewhere: a Location carries an IANA time_zone, and
TimeZone resolves it against the zone database, which is
what a rule spanning a daylight-saving change needs.
use ocpi_kit::types::DateTime;
let instant: DateTime = "2024-01-15T23:30:00Z".parse()?;
let local = instant.local_parts(3600); // CET, UTC+1
assert_eq!(local.time.hour(), 0);
assert_eq!(local.date.day(), 16);
assert_eq!(local.iso_weekday, 2); // TuesdaySourcepub const fn unix_timestamp(self) -> i64
pub const fn unix_timestamp(self) -> i64
Seconds since the Unix epoch.
Sourcepub fn from_unix_timestamp(secs: i64) -> Result<Self, InvalidDateTime>
pub fn from_unix_timestamp(secs: i64) -> Result<Self, InvalidDateTime>
Builds a timestamp from seconds since the Unix epoch.
§Errors
Returns InvalidDateTime if the value is outside the supported range.
Sourcepub const fn is_canonical(self) -> bool
pub const fn is_canonical(self) -> bool
Whether the value was written in one of the six forms the spec allows.
false means the source used an explicit UTC offset (including +00:00), a lower-case
z, or a space instead of T. The instant itself is still correct.
Sourcepub const fn with_fractional_digits(self, digits: u8) -> Self
pub const fn with_fractional_digits(self, digits: u8) -> Self
Returns a copy that will be written with exactly digits fractional-second digits.
digits is clamped to 9.
Sourcepub const fn fractional_digits(self) -> u8
pub const fn fractional_digits(self) -> u8
How many fractional-second digits this value will be written with.
Sourcepub fn parse(text: &str) -> Result<Self, InvalidDateTime>
pub fn parse(text: &str) -> Result<Self, InvalidDateTime>
Parses one of the RFC 3339 forms OCPI allows, plus the tolerated deviations described on the type.
§Errors
Returns InvalidDateTime when the text is not a date-time at all, or carries an
offset that would place it outside the representable range.
Trait Implementations§
impl Copy for DateTime
Source§impl<'de> Deserialize<'de> for DateTime
impl<'de> Deserialize<'de> for DateTime
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
impl Eq for DateTime
Source§impl From<DateTime> for OffsetDateTime
impl From<DateTime> for OffsetDateTime
Source§impl From<OffsetDateTime> for DateTime
impl From<OffsetDateTime> for DateTime
Source§fn from(value: OffsetDateTime) -> Self
fn from(value: OffsetDateTime) -> Self
Source§impl JsonSchema for DateTime
Available on crate feature schema only.
impl JsonSchema for DateTime
schema only.Source§fn json_schema(_g: &mut SchemaGenerator) -> Schema
fn json_schema(_g: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreSource§impl Ord for DateTime
impl Ord for DateTime
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for DateTime
impl PartialOrd for DateTime
Auto Trait Implementations§
impl Freeze for DateTime
impl RefUnwindSafe for DateTime
impl Send for DateTime
impl Sync for DateTime
impl Unpin for DateTime
impl UnsafeUnpin for DateTime
impl UnwindSafe for DateTime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.