juniper::integrations::chrono

Trait FromFixedOffset

Source
pub trait FromFixedOffset: TimeZone {
    // Required method
    fn from_fixed_offset(dt: DateTime<FixedOffset>) -> DateTime<Self>;
}
Expand description

Trait allowing to implement a custom TimeZone, which preserves its TimeZone information when parsed in a DateTime GraphQL scalar.

§Example

Creating a custom CET TimeZone using chrono-tz crate. This is required because chrono-tz uses enum to represent all TimeZones, so we have no knowledge of the concrete underlying TimeZone on the type level.

#[derive(Clone, Copy)]
struct CET;

impl TimeZone for CET {
    type Offset = <chrono_tz::Tz as TimeZone>::Offset;

    fn from_offset(_: &Self::Offset) -> Self {
        CET
    }

    fn offset_from_local_date(
        &self,
        local: &chrono::NaiveDate,
    ) -> chrono::LocalResult<Self::Offset> {
        chrono_tz::CET.offset_from_local_date(local)
    }

    fn offset_from_local_datetime(
        &self,
        local: &chrono::NaiveDateTime,
    ) -> chrono::LocalResult<Self::Offset> {
        chrono_tz::CET.offset_from_local_datetime(local)
    }

    fn offset_from_utc_date(&self, utc: &chrono::NaiveDate) -> Self::Offset {
        chrono_tz::CET.offset_from_utc_date(utc)
    }

    fn offset_from_utc_datetime(&self, utc: &chrono::NaiveDateTime) -> Self::Offset {
        chrono_tz::CET.offset_from_utc_datetime(utc)
    }
}

impl FromFixedOffset for CET {
    fn from_fixed_offset(dt: DateTime<FixedOffset>) -> DateTime<Self> {
        dt.with_timezone(&CET)
    }
}

struct Root;

#[graphql_object]
impl Root {
    fn pass_date_time(dt: DateTime<CET>) -> DateTime<CET> {
        dt
    }
}

Required Methods§

Source

fn from_fixed_offset(dt: DateTime<FixedOffset>) -> DateTime<Self>

Converts the given DateTime<FixedOffset> into a DateTime<Self>.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FromFixedOffset for FixedOffset

Source§

impl FromFixedOffset for Utc

Implementors§