1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-26 16:30:34 +00:00
hls_m3u8/src/tags/media_segment/program_date_time.rs

204 lines
5.9 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
use std::str::FromStr;
2019-10-06 14:39:18 +00:00
use chrono::{DateTime, FixedOffset, SecondsFormat};
2020-02-02 14:23:47 +00:00
use derive_more::{Deref, DerefMut};
2019-09-15 10:51:51 +00:00
2019-10-04 09:02:21 +00:00
use crate::types::ProtocolVersion;
2019-09-13 14:06:52 +00:00
use crate::utils::tag;
2019-10-04 09:02:21 +00:00
use crate::{Error, RequiredVersion};
2019-09-13 14:06:52 +00:00
2019-09-22 16:00:38 +00:00
/// # [4.3.2.6. EXT-X-PROGRAM-DATE-TIME]
2020-02-02 12:38:11 +00:00
///
2019-10-03 14:23:27 +00:00
/// 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-10-03 14:23:27 +00:00
/// [`Media Segment`]: crate::MediaSegment
2020-02-02 12:38:11 +00:00
/// [4.3.2.6. EXT-X-PROGRAM-DATE-TIME]:
/// https://tools.ietf.org/html/rfc8216#section-4.3.2.6
2020-02-02 14:23:47 +00:00
#[derive(Deref, DerefMut, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2019-09-15 10:51:51 +00:00
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:";
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXProgramDateTime`] tag.
2019-09-22 16:00:38 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-09-22 16:00:38 +00:00
/// ```
2019-10-06 14:39:18 +00:00
/// # use hls_m3u8::tags::ExtXProgramDateTime;
2019-09-22 16:00:38 +00:00
/// 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)
2019-10-03 15:01:15 +00:00
/// .and_hms_milli(14, 54, 23, 31),
2019-09-22 16:00:38 +00:00
/// );
/// ```
2019-10-03 15:01:15 +00:00
pub const fn new(date_time: DateTime<FixedOffset>) -> Self { Self(date_time) }
2019-09-06 10:55:00 +00:00
2019-10-03 15:01:15 +00:00
/// Returns the date-time of the first sample of the associated media
/// segment.
2019-10-06 14:39:18 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-10-06 14:39:18 +00:00
/// ```
/// # 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),
/// );
///
/// assert_eq!(
/// program_date_time.date_time(),
/// FixedOffset::east(8 * HOURS_IN_SECS)
/// .ymd(2010, 2, 19)
/// .and_hms_milli(14, 54, 23, 31)
/// );
/// ```
2019-10-03 15:01:15 +00:00
pub const fn date_time(&self) -> DateTime<FixedOffset> { self.0 }
2019-09-22 16:00:38 +00:00
/// Sets the date-time of the first sample of the associated media segment.
2019-10-06 14:39:18 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-10-06 14:39:18 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXProgramDateTime;
/// use chrono::{FixedOffset, TimeZone};
///
/// const HOURS_IN_SECS: i32 = 3600; // 1 hour = 3600 seconds
///
/// let mut program_date_time = ExtXProgramDateTime::new(
/// FixedOffset::east(8 * HOURS_IN_SECS)
/// .ymd(2010, 2, 19)
/// .and_hms_milli(14, 54, 23, 31),
/// );
///
/// program_date_time.set_date_time(
/// FixedOffset::east(8 * HOURS_IN_SECS)
/// .ymd(2010, 10, 10)
/// .and_hms_milli(10, 10, 10, 10),
/// );
///
/// assert_eq!(
/// program_date_time.date_time(),
/// FixedOffset::east(8 * HOURS_IN_SECS)
/// .ymd(2010, 10, 10)
/// .and_hms_milli(10, 10, 10, 10)
/// );
/// ```
2019-09-22 16:00:38 +00:00
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-10-06 14:39:18 +00:00
/// This tag requires [`ProtocolVersion::V1`].
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXProgramDateTime {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 }
2019-09-06 10:55:00 +00:00
}
impl fmt::Display for ExtXProgramDateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-10-06 14:39:18 +00:00
let date_time = self.0.to_rfc3339_opts(SecondsFormat::Millis, true);
2019-09-15 10:51:51 +00:00
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)?;
let date_time = DateTime::parse_from_rfc3339(input).map_err(Error::chrono)?;
2019-09-15 10:51:51 +00:00
Ok(Self::new(date_time))
2019-09-06 10:55:00 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
2019-10-06 14:39:18 +00:00
use chrono::{Datelike, TimeZone};
2020-02-02 14:23:47 +00:00
use core::ops::DerefMut;
use pretty_assertions::assert_eq;
2019-09-22 08:57:28 +00:00
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
}
2019-10-06 14:39:18 +00:00
#[test]
fn test_deref() {
assert_eq!(
ExtXProgramDateTime::new(
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31),
)
.year(),
2010
);
}
#[test]
fn test_deref_mut() {
assert_eq!(
ExtXProgramDateTime::new(
FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31),
)
.deref_mut(),
&mut FixedOffset::east(8 * HOURS_IN_SECS)
.ymd(2010, 2, 19)
.and_hms_milli(14, 54, 23, 31),
);
}
2019-09-06 10:55:00 +00:00
}