Skip to content

Commit

Permalink
Fixed bug in timezone offest calculation for non-full-hour timezones …
Browse files Browse the repository at this point in the history
…(e.g. Iran) and added a test in local time for toISOString().
  • Loading branch information
botic committed Jun 15, 2011
1 parent 0fe4451 commit ae79b34
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion modules/ringo/utils/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ function toISOString(date, withTime, withTimeZone, withSeconds, withMilliseconds
// Append the timezone offset
if (withTime && withTimeZone) {
var offset = date.getTimezoneOffset(),
inHours = Math.abs(Math.floor(offset / 60)),
inHours = Math.floor(Math.abs(offset / 60)),
inMinutes = Math.abs(offset) - (inHours * 60);

// Write the time zone offset in hours
Expand Down
16 changes: 9 additions & 7 deletions test/ringo/utils/dates_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,13 +723,15 @@ exports.testToISOString = function() {
assert.strictEqual(dates.toISOString(d, true, false, true), "2010-01-02T12:00:00Z");
assert.strictEqual(dates.toISOString(d, true, false, true, true), "2010-01-02T12:00:00.000Z");

var ld = new Date(2011, 0, 2, 12, 0, 0), // Local date
offset = ld.getTimezoneOffset(),
hourOffset = Math.abs(Math.floor(offset / 60)),
minuteOffset = Math.abs(offset) - (hourOffset * 60);

assert.strictEqual(dates.toISOString(ld, true, true, true),
"2011-01-02T12:00:00" + (offset < 0 ? "+" : "-") + (hourOffset < 10 ? "0" : "") + hourOffset + (minuteOffset < 10 ? ":0" : ":") + minuteOffset);
// Test for local time using current time to prevent nasty timzone/dst jumps
d = new Date();
var sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
var formatted = sdf.format(d);
assert.strictEqual(dates.toISOString(d, true, true), formatted.substr(0,22) + ":" + formatted.substr(-2));

sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
formatted = sdf.format(d);
assert.strictEqual(dates.toISOString(d, true, true, true, true), formatted.substr(0,26) + ":" + formatted.substr(-2));;
};

if (require.main == module.id) {
Expand Down

0 comments on commit ae79b34

Please sign in to comment.