This project is archived and is in readonly mode.

#176 ✓resolved
ksamdev

Date::parse returns wrong Date for 31st of October

Reported by ksamdev | November 5th, 2009 @ 10:13 PM | in 1.2.4.3

Date.parse( '31 Oct') returns 1st of Oct
Date.parse( 'Oct 31') returns 31st of Oct

Example: http://mooshell.net/n6FEN/

Comments and changes to this ticket

  • ksamdev

    ksamdev November 5th, 2009 @ 11:28 PM

    After a short code review I found the source of error. Code:

    return {
        format: format,
        re: new RegExp('^' + re + '$', 'i'),
        handler: function(bits){
            bits = bits.slice(1).associate(parsed);
            var date = new Date().clearTime();
            if ('d' in bits) handle.call(date, 'd', 1);
            if ('m' in bits) handle.call(date, 'm', 1);
            for (var key in bits) handle.call(date, key, bits[key]);
            return date;
        }
    };
    

    };

    var handle = function(key, value){

    if (!value) return this;
    
    switch(key){
        case 'a': case 'A': return this.set('day', Date.parseDay(value, true));
        case 'b': case 'B': return this.set('mo', Date.parseMonth(value, true));
        case 'd': return this.set('date', value);
        case 'H': case 'I': return this.set('hr', value);
        case 'm': return this.set('mo', value - 1);
        case 'M': return this.set('min', value);
        case 'p': return this.set('ampm', value.replace(/\./g, ''));
        case 'S': return this.set('sec', value);
        case 's': return this.set('ms', ('0.' + value) * 1000);
        case 'w': return this.set('day', value);
        case 'Y': return this.set('year', value);
        case 'y':
            value = +value;
    

    Handler line:

    if ('m' in bits) handle.call(date, 'm', 1);

    is not executed because 'm' is not set in bits and current month is automatically used when day is attempted to be set (which is November having 30 days). So, setting DAY 31 results in value of 1 set instead of 31.

  • ksamdev

    ksamdev November 5th, 2009 @ 11:29 PM

    Fix seems to be pretty simple. Replace:

    if ('m' in bits) handle.call(date, 'm', 1);

    with

    if ('m' in bits || 'b' in bits) handle.call(date, 'm', 1);

  • Sebastian Markbåge

    Sebastian Markbåge November 6th, 2009 @ 09:15 PM

    • State changed from “new” to “open”
    • Assigned user changed from “Aaron Newton” to “Scott Kyle”

    You are correct, sir.

    I guess weekdays (%a) could have a similar issue.

  • ksamdev

    ksamdev November 6th, 2009 @ 09:17 PM

    Of course I gave you guys quite particular case. More generally there might something else wrong in logic. You know it better.

    I was using Date and heavily rely in my code on Date::parse() method. That's how this bug was accidentally found.

  • Scott Kyle

    Scott Kyle November 10th, 2009 @ 08:27 AM

    Maybe I'm too tired right now, but I can't think of anything better than changing the line to

    if ('m' in bits || 'b' in bits || 'B' in bits) handle.call(date, 'm', 1);
    

    I'll think it over in the morning :-)

  • Aaron Newton

    Aaron Newton November 19th, 2009 @ 02:09 AM

    • Milestone set to 1.2.4.3
  • Scott Kyle

    Scott Kyle November 23rd, 2009 @ 06:25 AM

    • State changed from “open” to “resolved”

    I decided to go with the method I used in my last comment, at least until I can think of something better.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The MooTools Extensions

Referenced by

Pages