readium / readium-js

EPUB processing engine written in Javascript
BSD 3-Clause "New" or "Revised" License
367 stars 107 forks source link

SMIL Clock value parser #59

Open danielweck opened 10 years ago

danielweck commented 10 years ago

Originally posted by @shunito https://github.com/readium/readium-chrome-extension/pull/318

Specification: http://idpf.org/epub/30/spec/epub30-mediaoverlays.html#app-clock-examples

Current implementation: https://github.com/readium/readium-js/blob/develop/epub-modules/epub/src/models/smil_document_parser.js#L212

Proposed implementation + Jasmine unit tests: https://github.com/shunito/parseTime

danielweck commented 8 years ago

Updated link: https://github.com/readium/readium-js/blob/develop/js/epub-model/smil_document_parser.js#L218

    // parse the timestamp and return the value in seconds
    // supports this syntax:
    // http://idpf.org/epub/30/spec/epub30-mediaoverlays.html#app-clock-examples
    SmilDocumentParser.resolveClockValue = function(value) {
        if (!value) return 0;

        var hours = 0;
        var mins = 0;
        var secs = 0;

        if (value.indexOf("min") != -1) {
            mins = parseFloat(value.substr(0, value.indexOf("min")));
        } else if (value.indexOf("ms") != -1) {
            var ms = parseFloat(value.substr(0, value.indexOf("ms")));
            secs = ms / 1000;
        } else if (value.indexOf("s") != -1) {
            secs = parseFloat(value.substr(0, value.indexOf("s")));
        } else if (value.indexOf("h") != -1) {
            hours = parseFloat(value.substr(0, value.indexOf("h")));
        } else {
            // parse as hh:mm:ss.fraction
            // this also works for seconds-only, e.g. 12.345
            var arr = value.split(":");
            secs = parseFloat(arr.pop());
            if (arr.length > 0) {
                mins = parseFloat(arr.pop());
                if (arr.length > 0) {
                    hours = parseFloat(arr.pop());
                }
            }
        }
        var total = hours * 3600 + mins * 60 + secs;
        return total;
    }
danielweck commented 8 years ago

Updated link: http://www.idpf.org/epub/31/spec/epub-mediaoverlays.html#app-clock-examples

The following are examples of allowed clock values:

5:34:31.396 = 5 hours, 34 minutes, 31 seconds and 396 milliseconds

124:59:36 = 124 hours, 59 minutes and 36 seconds

0:05:01.2 = 5 minutes, 1 second and 200 milliseconds

0:00:04 = 4 seconds

09:58 = 9 minutes and 58 seconds

00:56.78 = 56 seconds and 780 milliseconds

76.2s = 76.2 seconds = 76 seconds and 200 milliseconds

7.75h = 7.75 hours = 7 hours and 45 minutes

13min = 13 minutes

2345ms = 2345 milliseconds

12.345 = 12 seconds and 345 milliseconds