Opened 16 years ago

Last modified 15 years ago

#2024 closed defect

timezone offset error in getWeekOfYear v0.3.1 — at Version 3

Reported by: [email protected] Owned by: psowden
Priority: high Milestone: 0.9
Component: Date Version: 0.4
Keywords: Cc: [email protected]
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 (3)

comment:1 Changed 16 years ago by [email protected]

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

contact info:

Brec Carson [email protected]

comment:2 Changed 16 years ago by Adam Peller

Cc: [email protected] added
Component: GeneralDate
Owner: changed from anonymous to psowden

comment:3 Changed 16 years ago by Adam Peller

Description: modified (diff)
Note: See TracTickets for help on using tickets.