mirror of
https://github.com/samsonjs/media.git
synced 2026-03-26 09:35:47 +00:00
Merge pull request #3545 from pstambrecht/feature/iso8601-parser-2
Feature/iso8601 parser 2
This commit is contained in:
commit
de96bb5e4f
1 changed files with 28 additions and 8 deletions
|
|
@ -962,19 +962,39 @@ public final class DashMediaSource implements MediaSource {
|
|||
|
||||
private static final class Iso8601Parser implements ParsingLoadable.Parser<Long> {
|
||||
|
||||
private static final String ISO_8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
||||
private static final String ISO_8601_WITH_OFFSET_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
|
||||
private static final String ISO_8601_WITH_OFFSET_FORMAT_REGEX_PATTERN = ".*[+\\-]\\d{2}:\\d{2}$";
|
||||
private static final String ISO_8601_WITH_OFFSET_FORMAT_REGEX_PATTERN_2 = ".*[+\\-]\\d{4}$";
|
||||
|
||||
@Override
|
||||
public Long parse(Uri uri, InputStream inputStream) throws IOException {
|
||||
String firstLine = new BufferedReader(new InputStreamReader(inputStream)).readLine();
|
||||
try {
|
||||
// TODO: It may be necessary to handle timestamp offsets from UTC.
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
|
||||
format.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
return format.parse(firstLine).getTime();
|
||||
} catch (ParseException e) {
|
||||
throw new ParserException(e);
|
||||
|
||||
if (firstLine != null) {
|
||||
//determine format pattern
|
||||
String formatPattern;
|
||||
if (firstLine.matches(ISO_8601_WITH_OFFSET_FORMAT_REGEX_PATTERN)) {
|
||||
formatPattern = ISO_8601_WITH_OFFSET_FORMAT;
|
||||
} else if (firstLine.matches(ISO_8601_WITH_OFFSET_FORMAT_REGEX_PATTERN_2)) {
|
||||
formatPattern = ISO_8601_WITH_OFFSET_FORMAT;
|
||||
} else {
|
||||
formatPattern = ISO_8601_FORMAT;
|
||||
}
|
||||
//parse
|
||||
try {
|
||||
SimpleDateFormat format = new SimpleDateFormat(formatPattern, Locale.US);
|
||||
format.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
return format.parse(firstLine).getTime();
|
||||
} catch (ParseException e) {
|
||||
throw new ParserException(e);
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new ParserException("Unable to parse ISO 8601. Input value is null");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue