(time_str)
| 13 | } |
| 14 | |
| 15 | export function parseTime(time_str) { |
| 16 | var parsed = { |
| 17 | hour: null, |
| 18 | minute: null, |
| 19 | second: null, |
| 20 | millisecond: null // conform to keys in TLDate |
| 21 | } |
| 22 | var period = null; |
| 23 | var match = time_str.match(/(\s*[AaPp]\.?[Mm]\.?\s*)$/); |
| 24 | if (match) { |
| 25 | period = trim(match[0]); |
| 26 | time_str = trim(time_str.substring(0, time_str.lastIndexOf(period))); |
| 27 | } |
| 28 | |
| 29 | var parts = []; |
| 30 | var no_separators = time_str.match(/^\s*(\d{1,2})(\d{2})\s*$/); |
| 31 | if (no_separators) { |
| 32 | parts = no_separators.slice(1); |
| 33 | } else { |
| 34 | parts = time_str.split(':'); |
| 35 | if (parts.length == 1) { |
| 36 | parts = time_str.split('.'); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if (parts.length > 4) { |
| 41 | throw new TLError("invalid_separator_error"); |
| 42 | } |
| 43 | let hour_part = parts[0] |
| 44 | parsed.hour = parseInt(hour_part); |
| 45 | |
| 46 | if (period && period.toLowerCase()[0] == 'p' && parsed.hour != 12) { |
| 47 | parsed.hour += 12; |
| 48 | } else if (period && period.toLowerCase()[0] == 'a' && parsed.hour == 12) { |
| 49 | parsed.hour = 0; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | if (isNaN(parsed.hour) || parsed.hour < 0 || parsed.hour > 23) { |
| 54 | throw new TLError("invalid_hour_err", hour_part); |
| 55 | } |
| 56 | |
| 57 | if (parts.length > 1) { |
| 58 | let minute_part = parts[1] |
| 59 | parsed.minute = parseInt(minute_part); |
| 60 | if (isNaN(parsed.minute)) { |
| 61 | throw new TLError("invalid_minute_err", minute_part); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (parts.length > 2) { |
| 66 | var sec_parts = parts[2].split(/[\.,]/); |
| 67 | parts = sec_parts.concat(parts.slice(3)) // deal with various methods of specifying fractional seconds |
| 68 | if (parts.length > 2) { |
| 69 | throw new TLError("invalid_second_fractional_err"); |
| 70 | } |
| 71 | parsed.second = parseInt(parts[0]); |
| 72 | if (isNaN(parsed.second)) { |
no test coverage detected