Skip to main content

generalized_time_value

Function generalized_time_value 

Source
pub open spec fn generalized_time_value(bytes: Seq<u8>) -> Option<GeneralizedTimeSpec>
Expand description
{
    let zone_start = generalized_zone_start(bytes);
    let main_end = generalized_main_end(bytes, zone_start);
    let local = DateTime {
        year: decimal4(bytes, 0),
        month: decimal2(bytes, 4),
        day: decimal2(bytes, 6),
        hour: decimal2(bytes, 8),
        minute: if main_end >= 12 { decimal2(bytes, 10) } else { 0 },
        second: if main_end == 14 { decimal2(bytes, 12) } else { 0 },
    };
    let precision = if main_end == 10 {
        TimePrecision::Hour
    } else if main_end == 12 {
        TimePrecision::Minute
    } else {
        TimePrecision::Second
    };
    let fraction = if main_end == zone_start {
        Seq::empty()
    } else {
        bytes.subrange(main_end as int + 1, zone_start as int)
    };
    if zone_start == bytes.len() || bytes[zone_start as int] == 0x5a {
        Some(GeneralizedTimeSpec {
            datetime: local,
            precision,
            fraction,
            zone: if zone_start == bytes.len() { TimeZone::Local } else { TimeZone::Utc },
        })
    } else {
        let offset_minute = if bytes.len() - zone_start == 5 {
            decimal2(bytes, (zone_start as int + 3) as usize)
        } else {
            0
        };
        match normalize_offset(
            local,
            bytes[zone_start as int] == 0x2b,
            decimal2(bytes, (zone_start as int + 1) as usize),
            offset_minute,
        ) {
            Some(datetime) => {
                Some(GeneralizedTimeSpec {
                    datetime,
                    precision,
                    fraction,
                    zone: TimeZone::Utc,
                })
            }
            None => None,
        }
    }
}

Spec function parsing the parsed GeneralizedTime bytes into a structured GeneralizedTimeSpec. Handles timezone offset adjustment (UTC = local - offset) if present.