Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#2024 closed defect (fixed)

timezone offset error in getWeekOfYear v0.3.1

Reported by: brec@… Owned by: Adam Peller
Priority: high Milestone: 0.9
Component: Date Version: 0.4
Keywords: Cc: mde@…
Blocked By: Blocking:

Description (last modified by Adam Peller)

There's an issue with the current implementation that fails when the first week of the year and the current week of the year aren't in the same timezone (which happens often due to daylight savings time).

Found this in v 0.3.1 but checked and v.4.x isn't different.

This is how we fixed it. Hope it helps.

dojo.date.getWeekOfYear = function (dateObject, firstDay) {
	if (arguments.length == 1) { firstDay = 0; } // Sunday

	// work out the first day of the year corresponding to the week
	var firstDayOfYear = new Date(dateObject.getFullYear(), 0, 1);
	var day = firstDayOfYear.getDay();
	
	// XXX fix for timezone differences
		var dateOffset = dateObject.getTimezoneOffset();
		var fdateOffset = firstDayOfYear.getTimezoneOffset();
		//dojo.debug("-- input tz offset: "+dateOffset+" vs firstday tz offset: "+fdateOffset);	
		var diffOffset = fdateOffset - dateOffset;
		dateObject.setMinutes( dateObject.getMinutes() + diffOffset );
	
	// XXX fixing an order of operations bug
	firstDayOfYear.setDate(firstDayOfYear.getDate() - (day + firstDay - (day > firstDay ? 7 : 0)));

	return Math.floor( (dateObject.getTime() - firstDayOfYear.getTime()) / 604800000 );
}

Change History (10)

comment:1 Changed 13 years ago by brec@…

yikes, formatting not preserved. sorry about that, not as readable as it looked when creating it.

contact info:

Brec Carson brec@…

comment:2 Changed 13 years ago by Adam Peller

Cc: mde@… added
Component: GeneralDate
Owner: changed from anonymous to psowden

comment:3 Changed 13 years ago by Adam Peller

Description: modified (diff)

comment:4 Changed 13 years ago by dylan

Owner: changed from psowden to tk

comment:5 Changed 13 years ago by dylan

Milestone: 0.9

comment:6 Changed 13 years ago by tk

Can you provide a test case for this? And also, do you have a CLA submitted?

comment:7 Changed 13 years ago by Adam Peller

xref #2504

comment:8 Changed 12 years ago by Adam Peller

Owner: changed from tk to Adam Peller

comment:9 Changed 12 years ago by Adam Peller

Resolution: fixed
Status: newclosed

(In [9674]) Go back to iterative approach to calculate day/week of year, due to DST bug. Fixes #2587, #2024

comment:10 Changed 12 years ago by Adam Peller

Resolution: fixed

(In [9676]) dojo.date.difference also fixes #2587, #2024

Note: See TracTickets for help on using tickets.