// Comparable, Equatable, Debug-able RFC 3339 DateTime wrapper over
// `@time.ZonedDateTime`. Enables generated models containing `date-time`
// fields to derive `Eq`, `Compare`, and `Debug` cleanly.
///|
/// RFC 3339 UTC timestamp wrapper over `@time.ZonedDateTime` implementing
/// `Eq`, `Compare`, `Debug`, `FromJson`, and `ToJson`.
///
/// Equality and ordering compare the underlying UTC instant
/// `(to_unix_second(), nanosecond())`.
pub struct DateTime {
priv inner : @time.ZonedDateTime
}
///|
/// Wrap a `@time.ZonedDateTime`, normalizing it to UTC so component accessors
/// (`year`, `month`, `day`, `hour`, `minute`, `second`) are always consistent
/// with UTC-instant `Eq`, `Compare`, and `to_rfc3339`.
pub fn DateTime::new(dt : @time.ZonedDateTime) -> DateTime {
let utc = @time.ZonedDateTime::from_unix_second(
dt.to_unix_second(),
nanosecond=dt.nanosecond(),
zone=@time.utc_zone,
) catch {
_ => dt
}
{ inner: utc, }
}
///|
/// Access the underlying `@time.ZonedDateTime`.
pub fn DateTime::inner(self : DateTime) -> @time.ZonedDateTime {
self.inner
}
///|
/// Parse an RFC 3339 date-time string into a `DateTime`.
pub fn DateTime::from_rfc3339(
text : String,
ctx? : @cjson.DecodeContext,
) -> DateTime raise @cjson.JsonDecodeError {
{ inner: rfc3339_parse(text, ctx?), }
}
///|
/// Format as a canonical UTC RFC 3339 string.
pub fn DateTime::to_rfc3339(self : DateTime) -> String {
rfc3339_format_utc(self.inner)
}
///|
/// Unix timestamp in whole seconds since `1970-01-01T00:00:00Z`.
pub fn DateTime::to_unix_second(self : DateTime) -> Int64 {
self.inner.to_unix_second()
}
///|
/// Sub-second nanoseconds (`0..=999_999_999`).
pub fn DateTime::nanosecond(self : DateTime) -> Int32 {
self.inner.nanosecond()
}
///|
/// UTC calendar year.
pub fn DateTime::year(self : DateTime) -> Int32 {
self.inner.year()
}
///|
/// UTC calendar month (`1..=12`).
pub fn DateTime::month(self : DateTime) -> Int32 {
self.inner.month()
}
///|
/// UTC day of month (`1..=31`).
pub fn DateTime::day(self : DateTime) -> Int32 {
self.inner.day()
}
///|
/// UTC hour (`0..=23`).
pub fn DateTime::hour(self : DateTime) -> Int32 {
self.inner.hour()
}
///|
/// UTC minute (`0..=59`).
pub fn DateTime::minute(self : DateTime) -> Int32 {
self.inner.minute()
}
///|
/// UTC second (`0..=59`).
pub fn DateTime::second(self : DateTime) -> Int32 {
self.inner.second()
}
///|
pub impl Eq for DateTime with fn equal(self, other) {
self.inner.to_unix_second() == other.inner.to_unix_second() &&
self.inner.nanosecond() == other.inner.nanosecond()
}
///|
pub impl Compare for DateTime with fn compare(self, other) {
let s = self.inner.to_unix_second().compare(other.inner.to_unix_second())
if s != 0 {
s
} else {
self.inner.nanosecond().compare(other.inner.nanosecond())
}
}
///|
pub impl @debug.Debug for DateTime with fn to_repr(self) {
@debug.Repr(self.to_rfc3339())
}
///|
pub impl @cjson.FromJson for DateTime with fn from_json(json, path) {
let r = JsonReader::new(json, path)
{ inner: r.read_datetime(), }
}
///|
pub impl @cjson.ToJson for DateTime with fn to_json(self) {
write_datetime(self.inner)
}
///|
pub extend DateTime with @cjson.ToJson::{to_json}