1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-14 02:59:25 +00:00
hls_m3u8/src/tags/media_segment/program_date_time.rs

139 lines
3.7 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
2019-09-22 16:00:38 +00:00
use std::ops::{Deref, DerefMut};
2019-09-06 10:55:00 +00:00
use std::str::FromStr;
2019-09-15 10:51:51 +00:00
use chrono::{DateTime, FixedOffset};
2019-09-22 08:57:28 +00:00
use crate::types::{ProtocolVersion, RequiredVersion};
2019-09-13 14:06:52 +00:00
use crate::utils::tag;
use crate::Error;
2019-09-22 16:00:38 +00:00
/// # [4.3.2.6. EXT-X-PROGRAM-DATE-TIME]
/// The [ExtXProgramDateTime] tag associates the first sample of a
/// [Media Segment] with an absolute date and/or time.
2019-09-06 10:55:00 +00:00
///
2019-09-22 16:00:38 +00:00
/// [Media Segment]: crate::MediaSegment
2019-09-06 10:55:00 +00:00
/// [4.3.2.6. EXT-X-PROGRAM-DATE-TIME]: https://tools.ietf.org/html/rfc8216#section-4.3.2.6
2019-09-15 10:51:51 +00:00
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ExtXProgramDateTime(DateTime<FixedOffset>);
2019-09-06 10:55:00 +00:00
impl ExtXProgramDateTime {
pub(crate) const PREFIX: &'static str = "#EXT-X-PROGRAM-DATE-TIME:";
/// Makes a new `ExtXProgramDateTime` tag.
2019-09-22 16:00:38 +00:00
///
/// # Example
/// ```
/// use hls_m3u8::tags::ExtXProgramDateTime;
/// use chrono::{FixedOffset, TimeZone};
///
/// const HOURS_IN_SECS: i32 = 3600; // 1 hour = 3600 seconds
///
/// let program_date_time = ExtXProgramDateTime::new(
/// FixedOffset::east(8 * HOURS_IN_SECS)
/// .ymd(2010, 2, 19)
/// .and_hms_milli(14, 54, 23, 31)
/// );
/// ```
pub const fn new(date_time: DateTime<FixedOffset>) -> Self {
Self(date_time)
2019-09-06 10:55:00 +00:00
}
/// Returns the date-time of the first sample of the associated media segment.
2019-09-22 16:00:38 +00:00
pub const fn date_time(&self) -> DateTime<FixedOffset> {
self.0
}
/// Sets the date-time of the first sample of the associated media segment.
pub fn set_date_time(&mut self, value: DateTime<FixedOffset>) -> &mut Self {
self.0 = value;
self
2019-09-06 10:55:00 +00:00
}
2019-09-22 08:57:28 +00:00
}
2019-09-06 10:55:00 +00:00
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXProgramDateTime {
fn required_version(&self) -> ProtocolVersion {
2019-09-06 10:55:00 +00:00
ProtocolVersion::V1
}
}
impl fmt::Display for ExtXProgramDateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-09-15 10:51:51 +00:00
let date_time = self.0.to_rfc3339();
write!(f, "{}{}", Self::PREFIX, date_time)
2019-09-06 10:55:00 +00:00
}
}
impl FromStr for ExtXProgramDateTime {
type Err = Error;
2019-09-08 10:23:33 +00:00
2019-09-13 14:06:52 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
let input = tag(input, Self::PREFIX)?;
2019-09-15 10:51:51 +00:00
let date_time = DateTime::parse_from_rfc3339(input)?;
Ok(Self::new(date_time))
2019-09-06 10:55:00 +00:00
}
}
2019-09-22 16:00:38 +00:00
impl Deref for ExtXProgramDateTime {
type Target = DateTime<FixedOffset>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ExtXProgramDateTime {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
2019-09-06 10:55:00 +00:00
#[cfg(test)]
mod test {
use super::*;
2019-09-22 08:57:28 +00:00
use chrono::TimeZone;
const HOURS_IN_SECS: i32 = 3600; // 1 hour = 3600 seconds
2019-09-06 10:55:00 +00:00
#[test]
2019-09-15 10:51:51 +00:00
fn test_display() {
assert_eq!(
2019-09-22 16:00:38 +00:00
ExtXProgramDateTime::new(
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31)
)
.to_string(),
2019-09-15 10:51:51 +00:00
"#EXT-X-PROGRAM-DATE-TIME:2010-02-19T14:54:23.031+08:00".to_string()
);
}
#[test]
fn test_parser() {
2019-09-22 08:57:28 +00:00
assert_eq!(
ExtXProgramDateTime::new(
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31)
),
"#EXT-X-PROGRAM-DATE-TIME:2010-02-19T14:54:23.031+08:00"
.parse::<ExtXProgramDateTime>()
.unwrap()
);
2019-09-15 10:51:51 +00:00
}
#[test]
2019-09-22 08:57:28 +00:00
fn test_required_version() {
2019-09-22 16:00:38 +00:00
assert_eq!(
ExtXProgramDateTime::new(
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31),
)
.required_version(),
ProtocolVersion::V1
2019-09-22 08:57:28 +00:00
);
2019-09-06 10:55:00 +00:00
}
}