whatwg_datetime

Function parse_time

Source
pub fn parse_time(s: &str) -> Option<NaiveTime>
Expand description

Parse a specific time containing an hour, minute, and optionally a second, and a fraction of a second

This follows the rules for parsing a time string per WHATWG HTML Standard § 2.3.5.4 Times.

§Examples

use chrono::NaiveTime;
use whatwg_datetime::parse_time;

// parse a local datetime with hours and minutes
assert_eq!(parse_time("14:59"), NaiveTime::from_hms_opt(14, 59, 0));

// parse a local datetime with hours, minutes, and seconds
assert_eq!(parse_time("14:59:39"), NaiveTime::from_hms_opt(14, 59, 39));

// parse a local datetime with hours, minutes, seconds, and milliseconds
assert_eq!(parse_time("14:59:39.929"), NaiveTime::from_hms_milli_opt(14, 59, 39, 929));