trentrichardson / jQuery-Timepicker-Addon

Adds a timepicker to jQueryUI Datepicker
http://trentrichardson.com/examples/timepicker/
MIT License
2.66k stars 1.05k forks source link

"Now" button click sets a wrong Value for the default Timezone. #843

Open andriiash opened 8 years ago

andriiash commented 8 years ago

Use the next to reproduce:

$(function () {
        $('#MyTime').timepicker({
            timeFormat: 'TT hh:mm:ss',
            timeOnly: true
        });
    });

To reproduce:

  1. Click on Now button.
  2. Remember the Time value. 3.Wait a few seconds.
  3. Click on Now button again.
  4. Compare a new value with the previous one.

For example, after the button click the "now" variable has value eqals "Mon Nov 16 2015 15:05:28 GMT+0200". The result of "now.getMinutes() + now.getTimezoneOffset() + tzoffset" will be [5(int) + -120(int) + "120"(string)] equls "-115120" instead of 5 as expected, since "tzoffset" will have a string value for the default Timezone. Thus, after the now.setMinutes(-115120) call the date value will be elqual: "Fri Aug 28 2015 16:20:28 GMT+0300".

Fix: The jquery-ui-timepicker-addon.js code:

$.timepicker.timezoneOffsetNumber = function (tzString) {
...
        if (!/^(\-|\+)\d{4}$/.test(normalized)) { // possibly a user defined tz, so just give it back
            return tzString;
        }
...
}

should be replaced by:

$.timepicker.timezoneOffsetNumber = function (tzString) {
...
        if (!/^(\-|\+)\d{4}$/.test(normalized)) { // possibly a user defined tz, so just give it back
            return parseInt(tzString);
        }
...
}
trentrichardson commented 8 years ago

I updated dev branch with this fix. Note that this completely kills the partial support that existed for custom alpha timezones (it was a use at your own risk anyway).

andriiash commented 8 years ago

Thanks a lot!

MrPetovan commented 8 years ago

I found that this issue is actually coming from _onTimeChange which sets the timepicker timezone as a string: Line 828 of jquery-ui-timepicker-addon.js version v1.6.1

if (timezone !== false) {
    timezone = timezone.toString();
}

I would suggest to change to:

if (timezone !== false) {
    timezone = parseInt(timezone);
}

In order to keep the user-defined timezones.