pauladaniel / calendardateselect

Automatically exported from code.google.com/p/calendardateselect
Other
0 stars 0 forks source link

Date.parseFormattedString for formate_iso_date.js is broken #174

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Load up CalendarDateSelect with :iso_date as the format
2. Load up a page using the CalendarDateSelect
3. Open up Firebug, and type this in the console:

Date.parseFormattedString('2009-08-26 14:15')

What is the expected output? What do you see instead?

I expect to get a JS Date object.  Instead, I get an Invalid Date.

What version of the product are you using? On what operating system?

Using 1.11.1 on Ubuntu Jaunty, Firefox 3.0.13

Please provide any additional information below.

The Regex expression in the format_iso_date.js is this:

"([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + 
        "( ([0-9]{1,2}):([0-9]{2})?" + "?)?)?)?"; 

parsing '2009-08-26 14:15' with that regex yields:

["2009-08-26 14:", "2009", "-08-26 14:", "08", "-26 14:", "26", " 14:",
"14", undefined]

Note the undefined.  I'm able to get it to be the '15' that I expect it to
be by removing the question mark at the end of the second term of the regex:

"([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + 
        "( ([0-9]{1,2}):([0-9]{2})" + "?)?)?)?"; 

Original issue reported on code.google.com by mike.d.c...@gmail.com on 25 Aug 2009 at 6:43

GoogleCodeExporter commented 8 years ago
I experience the same problem, I was wondering why the popup calendar was not 
showing the value in the input field. I'm using iso_dates as 
well. I am using MacOSX, Safari, calendardateselect 1.15, javascript version.

Started debugging and I also get a "invalid date" after the 
Date.parseFormattedString. The alert I put in the calendardateselect code below 
shows an invalid date. 
...
  parseDate: function()
  {
    var value = $F(this.target_element).strip()
    this.selection_made = (value != "");
    this.date = value=="" ? NaN : Date.parseFormattedString(this.options.get("date") || value);

    alert(this.date);
...

Entering an alert in format_iso_date.js:
...
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + 
        "( ([0-9]{1,2}):([0-9]{2})?" +
        "?)?)?)?"; 

    var d = string.match(new RegExp(regexp, "i"));
    alert(d);
...
gives:
2009-08-21 11:,2009,-08-21 11:,08,-21 11:,21, 11:,11,
for the date 2009-08-21 11:44:13

I found some other regexp here: http://delete.me.uk/2005/03/iso8601.html 
    var regexp = "(\d\d\d\d)(?:-?(\d\d)(?:-?(\d\d)(?:[T ](\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d+))?)?)?(?:Z|(?:([-+])(\d\d)(?::?(\d\d))?)?)?)?)?)?";
that works fine and also recognizes a T between date and time and Z for zones 
etc. 

It seems that in format_iso_date there are also errors int eh milliseconds and 
beyond stuff. There is a 1 missing in the d subindexing. 
It writes now:
...
    if (d[8]) {
        date.setMinutes(d[8]);
    } 
    if (d[0]) {
        date.setSeconds(d[0]);
    } 
    if (d[2]) {
        date.setMilliseconds(Number("0." + d[2]));
    } 
    if (d[4]) {
        offset = (Number(d[6])) + Number(d[8]);
        offset = ((d[5] == '-') ? 1 : -1); 
    } 
...

that should be:
...
    if (d[8]) {
        date.setMinutes(d[8]);
    } 
    if (d[10]) {
        date.setSeconds(d[10]);
    } 
    if (d[12]) {
        date.setMilliseconds(Number("0." + d[12]));
    } 
    if (d[14]) {
        offset = (Number(d[16])) + Number(d[18]);
        offset = ((d[15] == '-') ? 1 : -1); 
    } 
...

With this new regexp and the adjustment in the format_iso_date.js file it works 
fine now.

Original comment by erik.sp...@gmail.com on 6 Jan 2010 at 9:36

GoogleCodeExporter commented 8 years ago
I'm now using the following format_iso_date.js:

Date.prototype.toFormattedString = function(include_time) {
    var hour;
    var str = this.getFullYear() + "-" + Date.padded2(this.getMonth() + 1) + "-" +Date.padded2(this.getDate());
    if (include_time) {
        hour = this.getHours();
        str += " " + this.getHours() + ":" + this.getPaddedMinutes();
    }
    return str;
};

Date.parseFormattedString = function (string) {

// Changed according to 
http://code.google.com/p/calendardateselect/issues/detail?id=174
// Ticket 174: Date.parseFormattedString for formate_iso_date.js is broken
//    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + 
//        "( ([0-9]{1,2}):([0-9]{2})?" +
//        "?)?)?)?"; 
    var regexp = "(\d\d\d\d)(?:-?(\d\d)(?:-?(\d\d)(?:[T ](\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d+))?)?)?(?:Z|(?:([-
+])(\d\d)(?::?(\d\d))?)?)?)?)?)?";
    var d = string.match(new RegExp(regexp, "i"));
    if (d === null) {
        return Date.parse(string); // at least give javascript a crack at it.
    }
    var offset = 0; 
    var date = new Date(d[1], 0, 1);
    if (d[3]) {
        date.setMonth(d[3] - 1);
    } 
    if (d[5]) {
        date.setDate(d[5]);
    } 
    if (d[7]) {
        date.setHours(d[7]);
    } 
    if (d[8]) {
        date.setMinutes(d[8]);
    } 
    if (d[10]) {
        date.setSeconds(d[10]);
    } 
/* milliseconds and timezone not used !
    if (d[12]) {
        date.setMilliseconds(Number("0." + d[12]));
    } 
    if (d[14]) {
        offset = (Number(d[16])) + Number(d[18]);
        offset = ((d[15] == '-') ? 1 : -1); 
    } 
    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    date.setTime(Number(time));
*/
    return date; 
};

Original comment by erik.sp...@gmail.com on 6 Jan 2010 at 9:50

GoogleCodeExporter commented 8 years ago
Your regexp didn't work for me (maybe due to line breaks - break it into 
shorter ""
strings and + them together for the web post), so I used the rest of your 
changes
with this regexp:
   var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + 
       "( ([0-9]{1,2}):([0-9]{2})?" +
       ")?)?)?";   // note extra ? taken out

Original comment by kimti...@gmail.com on 11 Jan 2010 at 8:02

GoogleCodeExporter commented 8 years ago
Hi I must admit that I went back to your original regexp without the extra 
question
mark as noted in the first comment. But if the d[xx] larger than 10 are ok now, 
it
should work fine.

Thanks a lot BTW for the great tool !!

It will be used in the upcoming version of the News modules for the Zikula CMS
(zikula.org).

Original comment by erik.sp...@gmail.com on 11 Jan 2010 at 9:45