Hello, does anyone have a function to calculate ISO week numbers, the xHarbour WEEK() is not returning the correct ISO week number.
Thank you,
FUNCTION FT_WOY(dInDate)
LOCAL nFirstDays, nDayOffset, nWkNumber, cCentury
IF VALTYPE( dInDate) <> "D"
nWkNumber := NIL
ELSE
// resolve century issue
IF LEN(DTOC(dInDate)) > 8 // CENTURY is on
cCentury := SUBSTR( DTOC( dInDate) ,7 ,4)
ELSE
cCentury := SUBSTR( DTOC( dInDate) ,7 ,2)
ENDIF
// find number of days in first week of year
nFirstDays := 8 - (DOW(CTOD("01/01/" + cCentury)))
nWkNumber := 1
// find how many days after first week till dInDate
nDayOffset := (dInDate - CTOD ("01/01/" + cCentury)) - nFirstDays + 1
// count weeks in offset period
DO WHILE nDayOffset > 0
++nWkNumber
nDayOffset -= 7
ENDDO
ENDIF
RETURN(nWkNumber)
nIsoWeek := Week (Date(), .f.)
function IsoWeek (dDate)
LOCAL nWeek := 0, dDate2
dDate2 := dDate + 3 - ( DoW( dDate ) + 5 ) % 7
nWeek := Int (( DoY( dDate2 ) - 1 ) / 7 + 1)
return (nWeek)
ISO_WN( y, m, d )
{
dow = DOW( y, m, d );
dow0101 = DOW( y, 1, 1 );
if ( m == 1 && 3 < dow0101 < 7 - (d-1) )
{
// days before week 1 of the current year have the same week number as
// the last day of the last week of the previous year
dow = dow0101 - 1;
dow0101 = DOW( y-1, 1, 1 );
m = 12;
d = 31;
}
else if ( m == 12 && 30 - (d-1) < DOW( y+1, 1, 1 ) < 4 )
{
// days after the last week of the current year have the same week number as
// the first day of the next year, (i.e. 1)
return 1;
}
return ( DOW( y, 1, 1 ) < 4 ) + 4 * (m-1) + ( 2 * (m-1) + (d-1) + dow0101 - dow + 6 ) * 36 / 256;
}
FUNCTION ISO_WEEKNR( y, m, d )
// Year/month/day
// Sample : ISO_WEEKNR(08,12,29)
y1 := PADL( STR(y), 2, "0")
y2 := PADL( STR(y - 1), 2, "0")
Y3 := PADL( STR(y + 1), 2, "0")
m1 := PADL( STR(m), 2, "0")
d1 := PADL( STR(d), 2, "0")
date1 := CTOD ( '"'+ y1 + "." + m1 + "." + d1 + '"' ) // Test : 08.12.29
date2 := CTOD ( '"'+ y1 + ".01.01" ) // 08.01.01
date3 := CTOD ( '"'+ y2 + ".01.01" ) // 07.01.01 Year before
date4 := CTOD ( '"'+ y3 + ".01.01" ) // 09.01.01 Year after
dow1 := DOW( date1 )
dow0101 := DOW( date2 )
if ( m == 1 && 3 < dow0101 < 7 - (d-1) ) // To be translated !!!
// days before week 1 of the current year have the same week number as
// the last day of the last week of the previous year
dow1:= dow0101 - 1
dow0101 := DOW( date3 )
m := 12
d := 31
ELSE
IF ( m == 12 && 30 - (d-1) < DOW( date4 ) < 4 ) // To be translated !!!!
// days after the last week of the current year have the same week number as
// the first day of the next year, (i.e. 1)
return 1
ENDIF
ENDIF
RETURN ( DOW( date2 ) < 4 ) + 4 * (m-1) + ( 2 * (m-1) + (d-1) + dow0101 - dow1 + 6 ) * 36 / 256
cdmmaui wrote:Sorry, I tested more dates and I found that the week number was not calculating correctly. Here are some examples
12/28/2008 - calculates to week 52, should be week 1
01/11/2009 - calculates to week 2, should be week 3
01/18/2009 - calculates to week 3, should be week 4
It is my understanding that the ISO week starts on a Sunday. Please correct me if I am wrong.
First day of the week
In most of Europe, and some other countries, Monday is considered to be the first day of the week. Monday is literally named as such in Baltic languages, for example Lithuanian (pirmadienis - means First Day); in Estonian (esmaspäev - means First Day); Hungarian (hétfő - means literally Head of The Seven/Week); Basque (astelehen - means Week-First) and Mandarin (pinyin Xīngqí Yī, literally means Weekday One). In Slavic languages, Tuesday, Thursday and Friday were named literally for their number after Monday[5], a meaning still conserved in Russian (for example Tuesday: вторник - vtórnik - means second).
The ISO prescribes Monday as the first day of the week with ISO 8601 for software date formats.
In Jewish, Arab, Western Christian and Greek Orthodox tradition, the first day of the week is Sunday. The Hebrew, Ecclesiastical Latin and Medieval and Modern Greek languages number most of the days of the week. In Hebrew, Sunday through Friday are numbered one through six; in Arabic, Sunday through Thursday are numbered one through five; in Ecclesiastical Latin and modern Portuguese, Monday through Friday are numbered the second through the sixth days of the week (feria in Latin or feira in Portuguese); in Medieval and Modern Greek, Monday through Thursday are numbered the second through fifth. For many Western Christians and Jews, Sunday remains the first day of the week. Most, though not all, business and social calendars in North America mark Sunday as the first day of the week.
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 49 guests