Skip to content

parseTime not abble to work with timeFormat containing control characters enclosed by quotes #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bernosek opened this issue Oct 3, 2012 · 0 comments

Comments

@bernosek
Copy link

bernosek commented Oct 3, 2012

Master version: 1.0.4

The issue is simple. When you use swedish time format 'kl' h:mm:ss and choose the time for instance 15:30:45, you will get the correct text value kl 15:30:45. Buit when you request a Date object (by calling $("#your_id").datetimepicker("getDate") method), you will get the Date object with time set to midnight (00:00:00). The problem is with the l control character in timeFormat options. Even if it is enclosed by quotes, parsing of such text is failing (and then no time is datermined and applied to the output Date object).

Problem lays in $.datepicker.parseTime function, which is not counting with literars enclosed by quotes. The problem is in regular expression in getFormatPositions inner function and also right after it when the timeFormat is transformed to regstr regular expression variable. Here is the code snippet, which is solving such issue (we're already using this modification for our project, because we need to have this working):

        // figure out position of time elements.. cause js cant do named captures
        var getFormatPositions = function (timeFormat) {
            var finds = timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|l{1}|t{1,2}|z|'.*?')/g),
                orders = {
                    h: -1,
                    m: -1,
                    s: -1,
                    l: -1,
                    t: -1,
                    z: -1
                };

            if (finds) {
                for (var i = 0; i < finds.length; i++) {
                    if (orders[finds[i].toString().charAt(0)] == -1) {
                        orders[finds[i].toString().charAt(0)] = i + 1;
                    }
                }
            }
            return orders;
        };

        var o = extendRemove(extendRemove({}, $.timepicker._defaults), options || {});

        var regstr = '^' + timeFormat.toString()
            .replace(/(hh?|mm?|ss?|[tT]{1,2}|[lz]|'.*?')/g, function (match) {
                switch (match.toLowerCase()) {
                    case 'h':
                    case 'hh':
                        return '(\\d?\\d)';
                    case 'm':
                    case 'mm':
                        return '(\\d?\\d)';
                    case 's':
                    case 'ss':
                        return '(\\d?\\d)';
                    case 'l':
                        return '(\\d?\\d?\\d)';
                    case 'z':
                        return '(z|[-+]\\d\\d:?\\d\\d|\\S+)?';
                    case 't':
                    case 'tt':
                        return getPatternAmpm(o.amNames, o.pmNames);
                    default:    // literar escaped in quotes
                        return '(' + match.replace(/\'/g, "").replace(/(\.|\$|\^|\\|\/|\(|\)|\[|\]|\?|\+|\*)/g, function (m) { return "\\" + m; }) + ')?';
                }
            }).replace(/\s/g, '\\s?') +
                o.timeSuffix + '$',
            order = getFormatPositions(timeFormat),
            ampm = '',
            treg;

        treg = timeString.match(new RegExp(regstr, 'i'));

Note: This solution is working only for literars enclosed by single quotes. It will not work for double quotes, becuse it shouldn't. See #465 issue for more details about quotes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants