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
impl CdrTimestamp
Sourcepub const fn new(
local: PrimitiveDateTime,
offset: QuarterHourOffset,
) -> Option<Self>
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.
Sourcepub const fn local(self) -> PrimitiveDateTime
pub const fn local(self) -> PrimitiveDateTime
The local wall-clock reading, exactly as the record carried it.
Sourcepub const fn local_date(self) -> Date
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.
Sourcepub const fn offset(self) -> QuarterHourOffset
pub const fn offset(self) -> QuarterHourOffset
The offset to UTC that was in force where the record was written.
Sourcepub const fn to_utc(self) -> OffsetDateTime
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.
Sourcepub const fn to_offset(self, offset: QuarterHourOffset) -> Self
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.
Sourcepub const fn from_utc(
instant: OffsetDateTime,
offset: QuarterHourOffset,
) -> Option<Self>
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.
Sourcepub fn cmp_instant(a: &Self, b: &Self) -> Ordering
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
impl CdrTimestamp
Sourcepub const BCD_OCTETS: usize = 9
pub const BCD_OCTETS: usize = 9
The wire length of the 3GPP TS 32.298 TimeStamp, in octets.
Sourcepub fn from_bcd(bytes: &[u8], century: Century) -> Result<Self, DecodeError>
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.
Sourcepub fn to_bcd(self, century: Century) -> Result<[u8; 9], DecodeError>
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
impl CdrTimestamp
Sourcepub const TAP_LOCAL_CHARS: usize = 14
pub const TAP_LOCAL_CHARS: usize = 14
The width of a TAP3 LocalTimeStamp, in characters.
Sourcepub const TAP_OFFSET_CHARS: usize = 5
pub const TAP_OFFSET_CHARS: usize = 5
The width of a TAP3 UtcTimeOffset, in characters.
Sourcepub fn from_tap(local: &str, offset: &str) -> Result<Self, DecodeError>
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.
Trait Implementations§
Source§impl Clone for CdrTimestamp
impl Clone for CdrTimestamp
impl Copy for CdrTimestamp
Source§impl Debug for CdrTimestamp
impl Debug for CdrTimestamp
Source§impl Display for CdrTimestamp
impl Display for CdrTimestamp
impl Eq for CdrTimestamp
Source§impl FromStr for CdrTimestamp
impl FromStr for CdrTimestamp
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
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.