Skip to main content

CdrTimestamp

Struct CdrTimestamp 

Source
pub struct CdrTimestamp { /* private fields */ }
Expand description

A charging-record timestamp: local wall-clock time plus its offset to UTC.

Models the TimeStamp of 3GPP TS 32.298 exactly. The local time is kept as written, never silently normalised, so a record round-trips through this type unchanged and a decoder cannot lose the wall-clock reading an operator will quote back in a billing dispute.

§Ordering is deliberately absent

CdrTimestamp implements neither Ord nor PartialOrd. Two records from different offsets compare correctly only as instants, and a derived ordering would compare the local fields — silently interleaving a UTC+14:00 record ahead of a UTC−12:00 one that actually happened a day earlier. Sort by to_utc, or use cmp_instant:

use mediation::CdrTimestamp;

let mut records: Vec<CdrTimestamp> = vec![
    "2026-07-28T01:00:00+14:00".parse().unwrap(),
    "2026-07-27T23:00:00-12:00".parse().unwrap(),
];
records.sort_by(CdrTimestamp::cmp_instant);

// The +14:00 record is the earlier instant, despite the later local date.
assert_eq!(records[0].to_string(), "2026-07-28T01:00:00+14:00");

Implementations§

Source§

impl CdrTimestamp

Source

pub const MIN_YEAR: i32 = 1

The first UTC year a timestamp may denote.

Source

pub const MAX_YEAR: i32 = 9998

The last UTC year a timestamp may denote.

Bounding the instant to years 1–9998 keeps every re-expression in any encodable offset (±19:45) inside years 0–9999, so to_utc, to_offset and the four-digit rendering are total.

Source

pub const fn new( local: PrimitiveDateTime, offset: QuarterHourOffset, ) -> Option<Self>

A timestamp from a local wall-clock reading and its offset, or None when the instant falls outside UTC years 1–9998.

Source

pub const fn local(self) -> PrimitiveDateTime

The local wall-clock reading, exactly as the record carried it.

Source

pub const fn local_date(self) -> Date

The local calendar date — the correct key for grouping records into the days a subscriber would recognise on an itemised bill.

Source

pub const fn offset(self) -> QuarterHourOffset

The offset to UTC that was in force where the record was written.

Source

pub const fn to_utc(self) -> OffsetDateTime

The instant this timestamp denotes, expressed in UTC.

The returned value carries the UTC offset, not the record’s, so to_utc().date() is the UTC date. That distinction is the whole point: a value that denoted the right instant while still reporting the local date would reintroduce the bug this type exists to prevent, in the one accessor a caller reaches for to escape it.

Never panics: the instant lies in UTC years 1–9998.

Source

pub const fn to_offset(self, offset: QuarterHourOffset) -> Self

The same instant re-expressed in another offset.

The instant is preserved and the wall-clock reading changes — the opposite of new, which preserves the wall clock. Never panics: an instant in years 1–9998 is local years 0–9999 in any encodable offset.

Source

pub const fn from_utc( instant: OffsetDateTime, offset: QuarterHourOffset, ) -> Option<Self>

A timestamp for an instant, expressed in a given offset, or None when the instant falls outside UTC years 1–9998.

Source

pub fn cmp_instant(a: &Self, b: &Self) -> Ordering

Compare two timestamps as instants.

Pass to slice::sort_by to order a series correctly regardless of the offsets its records carry.

Source§

impl CdrTimestamp

Source

pub const BCD_OCTETS: usize = 9

The wire length of the 3GPP TS 32.298 TimeStamp, in octets.

Source

pub fn from_bcd(bytes: &[u8], century: Century) -> Result<Self, DecodeError>

Decode the nine-octet TimeStamp of 3GPP TS 32.298.

The layout is YYMMDDhhmmssShhmm: six octets of plain BCD for the local date and time, one ASCII octet for the sign (+ or -), then two plain-BCD octets for the offset’s hours and minutes.

Note the two conventions in one field: the date-time digits are plain BCD, while the identifier fields of the same record are swapped-nibble TBCD. See crate::digits.

use mediation::{CdrTimestamp, Century};

// 2026-07-28 10:00:00, offset +02:00.
let octets = [0x26, 0x07, 0x28, 0x10, 0x00, 0x00, b'+', 0x02, 0x00];
let ts = CdrTimestamp::from_bcd(&octets, Century::TWENTY_FIRST).unwrap();

assert_eq!(ts.to_string(), "2026-07-28T10:00:00+02:00");
assert_eq!(ts.to_bcd(Century::TWENTY_FIRST).unwrap(), octets);
§Errors

DecodeError for the wrong length, a non-decimal nibble, a sign octet that is neither + nor -, an impossible date or time, or an offset that is not a multiple of 15 minutes.

Source

pub fn to_bcd(self, century: Century) -> Result<[u8; 9], DecodeError>

Encode to the nine-octet TimeStamp of 3GPP TS 32.298.

The century is a parameter, and the year must fall inside it. The field carries YY, so an encoder that simply wrote year % 100 would turn 1999 and 2099 into the same nine octets and hand the reader no way to tell them apart. Naming the century here is the same refusal from_bcd makes on the way in, from the other side: the ambiguity is in the encoding, so it is resolved where the encoding is, not silently downstream.

use mediation::{CdrTimestamp, Century};

let ts: CdrTimestamp = "2026-07-28T10:00:00+02:00".parse().unwrap();
assert!(ts.to_bcd(Century::TWENTY_FIRST).is_ok());

// The same value cannot be written as a 20th-century record.
assert!(ts.to_bcd(Century::new(1900).unwrap()).is_err());
§Errors

DecodeError when the year does not fall in century.

Source§

impl CdrTimestamp

Source

pub const TAP_LOCAL_CHARS: usize = 14

The width of a TAP3 LocalTimeStamp, in characters.

Source

pub const TAP_OFFSET_CHARS: usize = 5

The width of a TAP3 UtcTimeOffset, in characters.

Source

pub fn from_tap(local: &str, offset: &str) -> Result<Self, DecodeError>

Read the two fields a GSMA TD.57 timestamp is split across.

TAP3 carries local time and its offset as two separate fields — LocalTimeStamp, fourteen digits of YYYYMMDDhhmmss, and UtcTimeOffset, five characters of ±hhmm — usually with a UtcTimeOffsetCode indirection so a whole file’s offsets are declared once and referenced by number.

That indirection is where the bug lives. A decoder that reads the local timestamp and forgets to resolve the offset code produces a value that looks complete, parses cleanly, and is wrong by up to fourteen hours for every event in the file. Requiring both fields here means the offset cannot be forgotten: there is no constructor that takes the local half alone.

use mediation::CdrTimestamp;

let ts = CdrTimestamp::from_tap("20260728103045", "+0545").unwrap();
assert_eq!(ts.to_string(), "2026-07-28T10:30:45+05:45");
assert_eq!(ts.to_tap(), ("20260728103045".to_owned(), "+0545".to_owned()));

Note the four-digit year: unlike the 3GPP BCD encoding, TAP carries the century, so no Century is needed and none is accepted.

§Errors

DecodeError for the wrong field widths, a non-digit, an impossible date or time, an offset that is not a multiple of 15 minutes, -0000 (offset unknown), or an instant outside UTC years 1–9998.

Source

pub fn to_tap(self) -> (String, String)

Write the two fields a GSMA TD.57 timestamp is split across.

Returns (LocalTimeStamp, UtcTimeOffset). The offset is the literal five-character form rather than a code, because the code table belongs to the file rather than to the timestamp.

Trait Implementations§

Source§

impl Clone for CdrTimestamp

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for CdrTimestamp

Source§

impl Debug for CdrTimestamp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for CdrTimestamp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Writes the RFC 3339 form with whole seconds: 2026-07-28T06:00:00+05:45.

Whole seconds because the 3GPP encoding has no sub-second field — writing a fractional part would claim precision the record never carried.

Source§

impl Eq for CdrTimestamp

Source§

impl FromStr for CdrTimestamp

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses YYYY-MM-DDThh:mm:ss followed by an offset.

The date-time separator may be T, t or a space. The offset accepts every spelling QuarterHourOffset does; Z and -00:00 are refused (RFC 9557 §2.2: local offset unknown). The instant must fall in UTC years 1–9998.

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

impl Hash for CdrTimestamp

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for CdrTimestamp

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CdrTimestamp

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.