keithamus / tempus

Tempus - Time for a new Date()
http://tempus-js.com
Other
94 stars 9 forks source link

Tempus cannot parse '%F %T' by default #14

Open tomcooksey opened 12 years ago

tomcooksey commented 12 years ago
var d = new Tempus('2012-04-29 23:06:00');

TypeError: Cannot call method 'getTimezoneOffset' of undefined

Stack Trace:

TypeError: Cannot call method 'getTimezoneOffset' of undefined
    at Tempus.setTimezoneToLocale (http://192.168.1.94/lib/tempus.js:538:45)
    at Tempus.set (http://192.168.1.94/lib/tempus.js:263:18)
    at new Tempus (http://192.168.1.94/lib/tempus.js:34:18)
    at unknown source
    at Object._evaluateOn (unknown source)
    at Object._evaluateAndWrap (unknown source)
    at Object.evaluate (unknown source)

Browser: Google Chrome V 18.0.1025.163 O/S: OSX 10.7.2

tomcooksey commented 12 years ago

Looks like the error is here:

else if ((modules = TempusParsers[aType])) {

                for (var i = 0, module; module = modules[i]; ++i) {
                    var exC = module.length;
                    if (ar.length < module.length) continue;
                    if (exC > 1) {
                        while (exC--) {
                            if (ar[exC] !== undef && realTypeOf(ar[exC]) != module.exp[exC]) {
                                continue;
                            }
                        }
                    }
                    if (module.test.apply(this, ar)) {
                        this._date = new Date();
                        this.setTimezoneToLocale();
                        return module.parse.apply(this, arguments);
                    }
                }

Trying to match a module when no match you're not throwing.

tomcooksey commented 12 years ago

Latest version throws nicer error, but still think that timestamp should be considered valid.

keithamus commented 12 years ago

The date 2012-04-29 23:06:00 is not getting parsed because there are no registered parsers for it.

This date is a flavour of ISO8601, and can currently be parsed by the Date() object in modern browsers.

Tempus should be better at parsing ISO8601 flavours, as it only deals with a limited subset, this is related to issue #13

On 0.1(a) this throws an obscure error, in the upcoming 0.2 this will throw an error like Error: Cannot parse '2012-04-29 23:06:00' with 'ISO8601Date,ISO8601,COOKIE,RFC822,RFC850,RFC1036,RFC1123,RFC2822,RFC3339,RSS,W3C,Locale,GMT,NCC1701' which is more appropriate.

There are two possible quick solutions to this dilema.

  1. If you are in control of the dates you receive, then make some adjustments to the format, a date like 2012-04-29T23:06:00Z will be parsed, as this is the current incarnation of ISO8601 that Tempus passes
  2. Use a custom formatter when newing up the date, this is as simple as changing the arguments to: var d = new Tempus('2012-04-29 23:06:00', '%Y-%m-%d %T');

If you're willing to wait until 0.2 is released then the problem should be resolved by then