Functie: weekOfYear

Lasă un comentariu

12 Decembrie 2013 de liviustoica55


static int _weekOfYear(date _curDate)

{

int firstDayOfWeek;

int firstWeekOfYear;

int dowJan1st;

int dowCurDate;

int week;

date normDate;

date testDate;

#TimeConstants

;

 

normDate = dateStartYr(_curDate);

 

// Get locale settings.

firstDayOfWeek = firstDayOfWeek();

firstWeekOfYear = firstWeekOfYear();

 

// The dayofwk(…) function returns

// a value between 1 and 7.

// The logic below is expecting 0 – 6 so

// the 1 is subtracted from the result.

dowJan1st = dayofwk(normDate) – 1;

dowCurDate = dayofwk(_curDate) – 1;

 

// The dayofwk function used above returns

// the ISO-8601 day of week, where Monday is 1

// and recall that the 1 was subtracted from the result

// to get a value between (0 – 6).

//

// If the locale settings (firstDayOfWeek) is

// not Monay (i.e. > 0) then the caclulated

// day of week results need to be adjusted

// so they align with the locale setting.

if (firstDayOfWeek > 0)

{

dowJan1st = (dowJan1st + (#DaysPerWeek – firstDayOfWeek)) mod #DaysPerWeek;

dowCurDate = (dowCurDate + (#DaysPerWeek – firstDayOfWeek)) mod #DaysPerWeek;

}

 

// The normDate represents a day in the first week of the year.

// The following will adjust the normDate so the day of week

// will align with the target date.

//

// For example: if normDate is Sunday and target date is Tuesday

// the normDate will be adjusted to Tuesday

// of the 1st week of the year.

normDate = normDate + (dowCurDate – dowJan1st);

 

switch(firstWeekOfYear)

{

case 0: // Week starts on Jan 1

week = ((_curDate – normDate) / #DaysPerWeek) + 1; // calculate week number.

 

if (week > 52)

{

// Look at the last day of the week and compare it

// to the target date. If the year is different

// then the target date is part of week 1 for next year.

testDate = _curDate + ((#DaysPerWeek – 1) – dowCurDate);

if (year(testDate) != year(_curDate))

{

week = 53;

}

}

break;

 

case 1: // First full week.

if (dowJan1st != 0)

{

// If day of week for Jan 1st is not on the

// the 1st day of the week (0), then

// adjust the normDate to next week (the 1st full week).

normDate = normDate + #DaysPerWeek;

}

 

if (_curDate < normDate)

{

// The target date is prior to the 1st week, so

// it considered to be part of last year.

// Get the week for 12/31 of last year.

week = weekOfYear(dateStartYr(_curDate) – 1);

}

else

{

week = ((_curDate – normDate) / #DaysPerWeek) + 1; // calculate the week number.

}

break;

 

case 2: // First 4-day week

{

if (firstDayOfWeek == 0)

{

// Use ISO-8601 when first day of week is Monday.

week = wkofyr(_curdate);

}

else

{

if (dowJan1st > 3)

{

// If 1/1 starts on the 5th day

// Then adust normDate to next week (first 4-day week).

normDate = normDate + #DaysPerWeek;

}

 

if (_curDate < normDate)

{

// The target date is prior to the 1st week, so

// it considered to be part of last year.

// Get the week for 12/31 of last year.

week = weekOfYear(dateStartYr(_curDate) – 1);

}

else

{

week = ((_curDate – normDate) / #DaysPerWeek) + 1; // Calculate the week number.

if (week > 52 && dowCurDate < 3)

{

// Check the 5th day of the target week.

// If the year is different then the target date

// is part of week 1 for next year.

testDate = _curDate + (3 – dowCurDate);

if (year(testDate) != year(_curDate))

{

week = 53;

}

}

}

}

}

}

 

return week;

}

Lasă un răspuns

Completează mai jos detaliile despre tine sau dă clic pe un icon pentru autentificare:

Logo WordPress.com

Comentezi folosind contul tău WordPress.com. Dezautentificare / Schimbă )

Poză Twitter

Comentezi folosind contul tău Twitter. Dezautentificare / Schimbă )

Fotografie Facebook

Comentezi folosind contul tău Facebook. Dezautentificare / Schimbă )

Fotografie Google+

Comentezi folosind contul tău Google+. Dezautentificare / Schimbă )

Conectare la %s

%d blogeri au apreciat asta: