Posted: Sun Jul 22, 2007 4:34 am
Impressive! I will do some testing and let you know how it works.
Many thanks!
Many thanks!
The Music Manager for Serious Collectors
http://www.mediamonkey.com/forum/


Yeah, I see. There is a problem when a resulting year is greater then source year. Sorry pal, I already spent too much time calculating this formula. If you want, try for yourself to improve it. Except of approx. two last weeks in year, all results should be correct, Also, you should know that strftime('%w', Date) has totaly different logic for calculating week of day than Jet Format. With Format, every fist January has week of year = 1, no matter which is firstdayofweek. But, with strftime('%w', Date) you could get 0 or 1 depending of year. Here is a code and its result:Bex wrote:ZvezdanD,
I get some strange results with your formula:
Code: Select all
Dim i As Integer, dt As Date
dt = DateSerial(2003, 12, 21)
Debug.Print "Date - FDOW: Sun - FDOW: Mon"
For i = 1 To 21
Debug.Print dt & " - " & Format(dt, "ww", 1) & " - " & Format(dt, "ww", 2)
dt = DateAdd("d", 1, dt)
Next
Maybe, I was not clear. You need to use some complicated formula, even if you decide to limit firstdayofweek to Monday. If you want a script with same behavior within MM2 and MM3 it is not enough to replace Format(Date, "ww", 2) with strftime('%w', Date), because its internal computation is totaly different. Please, take a look on this result:Bex wrote:But I think I'll go with ISO standard and make all the weeks starts on Mondays.
Code: Select all
Date - Jet - SQLite Date - Jet - SQLite
2000-01-01 - 1 - 0 2000-12-01 - 49 - 48
2001-01-01 - 1 - 1 2001-12-01 - 48 - 47
2002-01-01 - 1 - 0 2002-12-01 - 48 - 48
2003-01-01 - 1 - 0 2003-12-01 - 49 - 48
2004-01-01 - 1 - 0 2004-12-01 - 49 - 48
2005-01-01 - 1 - 0 2005-12-01 - 49 - 48
2006-01-01 - 1 - 0 2006-12-01 - 49 - 48
2007-01-01 - 1 - 1 2007-12-01 - 48 - 48
2008-01-01 - 1 - 0 2008-12-01 - 49 - 48
2009-01-01 - 1 - 0 2009-12-01 - 49 - 48
2010-01-01 - 1 - 0 2010-12-01 - 49 - 48
2011-01-01 - 1 - 0 2011-12-01 - 49 - 48
2012-01-01 - 1 - 0 2012-12-01 - 49 - 48
2013-01-01 - 1 - 0 2013-12-01 - 48 - 47
2014-01-01 - 1 - 0 2014-12-01 - 49 - 48
2015-01-01 - 1 - 0 2015-12-01 - 49 - 48
2016-01-01 - 1 - 0 2016-12-01 - 49 - 48
2017-01-01 - 1 - 0 2017-12-01 - 49 - 48
2018-01-01 - 1 - 1 2018-12-01 - 48 - 48
2019-01-01 - 1 - 0 2019-12-01 - 48 - 47
2020-01-01 - 1 - 0 2020-12-01 - 49 - 48
Code: Select all
cast(strftime('%W', Date) as integer) +
case when firstdayofweek = 1
then
case when cast(strftime('%w', Date, 'start of year') as integer) = 1
then
0
else
1
end
else
case when cast(strftime('%w', Date, 'start of year') as integer) > 1
and (firstdayofweek > cast(strftime('%w', Date, 'start of year') as integer)
or firstdayofweek = 0)
then
case when cast(strftime('%w', Date) as integer) = 0
or cast(strftime('%w', Date) as integer) >= firstdayofweek
and firstdayofweek > cast(strftime('%w', Date, 'start of year') as integer)
then
2
else
1
end
else
case when cast(strftime('%w', Date) as integer) = 0
or cast(strftime('%w', Date) as integer) >= firstdayofweek
and firstdayofweek > 0
then
1
else
0
end
end
endBex wrote:But I think I'll go with ISO standard and make all the weeks starts on Mondays. It might disappoint some US users and others who consider Sunday (or any other day) to be the first day of week.
Code: Select all
Function IsoYearWeekSQL(date)
IsoYearWeekSQL = _
"case "&_
" when strftime('%w', "&date&", 'start of year') in ('1','2','3','4') then "&_
" case when strftime('%w', "&date&", '+1 year', 'start of year','-1 day') in ('1','2','3') "&_
" and (((cast(strftime('%j', "&date&") as intiger) + cast(strftime('%w', "&date&", 'start of year') as intiger) - 1-1)/7)+1) = 53 "&_
" then strftime('%Y w ', "&date&",'+1 year')||'01' "&_
" else strftime('%Y w ', "&date&")||substr('0'||(((cast(strftime('%j', "&date&") as intiger) + cast(strftime('%w',"&date&",'start of year') as intiger)-1-1)/7)+1),-2,2) end "&_
" when strftime('%w', "&date&", 'start of year') in ('5','6') then "&_
" case "&_
" when cast(strftime('%w', "&date&", 'start of year') as intiger) + cast(strftime('%j', "&date&") as intiger) <=8 then "&_
" case when strftime('%Y', "&date&", '-1 year') in ('1998','2004','2009','2015') then strftime('%Y w ', "&date&", '-1 year')||'53' "&_
" else strftime('%Y w ', "&date&", '-1 year')||'52' end "&_
" else "&_
" strftime('%Y w ', "&date&")||substr('0'||(((cast(strftime('%j', "&date&") as intiger) + cast(strftime('%w', "&date&", 'start of year') as intiger) - 8-1)/7)+1),-2,2) "&_
" end "&_
" when strftime('%w', "&date&", 'start of year') = '0' then "&_
" case "&_
" when strftime('%j', "&date&") = '001' then "&_
" case when strftime('%Y', "&date&", '-1 year') in ('1998','2004','2009','2015') then strftime('%Y w ', "&date&", '-1 year')||'53' "&_
" else strftime('%Y w ', "&date&", '-1 year')||'52' end "&_
" else "&_
" strftime('%Y w ', "&date&")||substr('0'||(((cast(strftime('%j', "&date&") as intiger) -1-1)/7)+1),-2,2) "&_
" end "&_
"end "
End FunctionQuite a few others countries use Sunday as well. While some countries use Saturday and Hungary uses Wednesday!Teknojnky wrote:Bex wrote:But I think I'll go with ISO standard and make all the weeks starts on Mondays. It might disappoint some US users and others who consider Sunday (or any other day) to be the first day of week.
hrmm, 98273498372 billion americans can't be wrong!
![]()
![]()
http://www.merlyn.demon.co.uk/datefmts.htm#ISO8601The great advantage of having International
Standards is that they make it easy for the
American people to differ from everybody else.
I don't know what your function calculates. If you want a week of year assuming that Monday is a first day of week, your code could be much shorter:Bex wrote:I have actually written a piece of code which gives the ISO year & week.
Code: Select all
cast(strftime('%W', Date) as integer) +
case when cast(strftime('%w', Date, 'start of year') as integer) = 1
then
0
else
1
end