Page 1 of 1

ISO-8601 WeekNo + variants...

Posted: Thu Jun 05, 2008 3:56 pm
by DoubleDutch
This is some little routines to quickly find the week number to the ISO-8601 standard. I needed this for a little project I'm looking at doing and I couldn't find a week-no routine on the forum that worked to the ISO standard.

I've put in the US and UK variants, but I'm not sure if these are correct. If anyone knows and can verify if they are correct then please reply to this post.

Code: Select all

Procedure CorrectDay(day,first)
	day-first
	If day<0
		day+7
	EndIf
	ProcedureReturn day
EndProcedure

Procedure.s WeekNo(date,first=1,needday=3)
	startday=CorrectDay(DayOfWeek(date),first)
	offset=needday-startday
	needdate=AddDate(date,#PB_Date_Day,offset)
	startofweek=AddDate(needdate,#PB_Date_Day,-needday)
	weekno=(DayOfYear(needdate)/7)+1
	result$=Str(Year(needdate))+"-W"+RSet(Str(weekno),2,"0")+"-"+Str(startday+1)
	ProcedureReturn result$
EndProcedure

Debug("Tests from wikipedia:")
Debug WeekNo(Date(2005,1,1,0,0,0))
Debug WeekNo(Date(2005,1,2,0,0,0))
Debug WeekNo(Date(2005,12,31,0,0,0))
Debug WeekNo(Date(2007,1,1,0,0,0))
Debug WeekNo(Date(2007,12,30,0,0,0))
Debug WeekNo(Date(2007,12,31,0,0,0))
Debug WeekNo(Date(2008,1,1,0,0,0))
Debug WeekNo(Date(2008,12,29,0,0,0))
Debug WeekNo(Date(2008,12,31,0,0,0))
Debug WeekNo(Date(2009,1,1,0,0,0))
Debug WeekNo(Date(2009,12,31,0,0,0))
Debug WeekNo(Date(2010,1,3,0,0,0))
Debug(" ")

Debug("Europe ISO-8601 week format:")
test1= Date(2008,12,29,0,0,0)
Debug WeekNo(test1)  ; adjust days so monday is start, thus thursday is 3

test2= Date(2010,1,3,0,0,0)
Debug WeekNo(test2)  ; adjust days so monday is start, thus thursday is 3

For loop=0 To 380
	Debug(FormatDate("%yyyy/%mm/%dd",test1)+"   "+WeekNo(test1))
	test1=AddDate(test1,#PB_Date_Day,1)
Next

Debug(" ")

Debug("American week format:")
test1= Date(2008,12,28,0,0,0)
Debug WeekNo(test1,0,6)  ; adjust days so sunday is start, thus saturday is 6

test2= Date(2010,1,2,0,0,0)
Debug WeekNo(test2,0,6)  ; adjust days so sunday is start, thus saturday is 6

For loop=0 To 380
	Debug(FormatDate("%yyyy/%mm/%dd",test1)+"   "+WeekNo(test1,0,6))
	test1=AddDate(test1,#PB_Date_Day,1)
Next

Debug(" ")


Debug("UK ISO variation week format:")
test1= Date(2008,12,28,0,0,0)
Debug WeekNo(test1,1,6)  ; adjust days so monday is start, thus sunday is 6

test2= Date(2010,1,3,0,0,0)
Debug WeekNo(test2,1,6)  ; adjust days so monday is start, thus sunday is 6

For loop=0 To 380
	Debug(FormatDate("%yyyy/%mm/%dd",test1)+"   "+WeekNo(test1,0,6))
	test1=AddDate(test1,#PB_Date_Day,1)
Next

Debug(" ")
The "first" parameter is the first day of the week modifier - so for sunday you can set it to 0, monday is 1, etc (ISO standard is 1 - monday).

The "needday" parameter is the day that is needed in the first week of year - count from 0 based on the "first" modifier, so if the "first" is 1 (making monday day 0) then having a needday of 3 means thursday (0=mon, 1=tue, ...)

It would be handy having these types of functions built-in to PureBasic.

Anyone care to write a UK tax week number calcluator and add it to this thread?

Re: ISO-8601 WeekNo + variants...

Posted: Fri Jun 22, 2018 6:35 am
by Seymour Clufley
Here's my own version, which assumes Monday as the start day for each week:

Code: Select all

Procedure.i WeekNumberWithinYear(dl.i)
  
  zerodl = Date(Year(dl),1,1,0,0,0)
  While DayOfWeek(zerodl)<>1
    zerodl = AddDate(zerodl,#PB_Date_Day,1)
  Wend
  
  If dl<zerodl
    ProcedureReturn 53
  EndIf
  
  week=0
  Repeat
    week+1
    zerodl = AddDate(zerodl,#PB_Date_Day,7)
  Until zerodl>dl
  
  ProcedureReturn week
  
EndProcedure

dl = Date()
Debug WeekNumberWithinYear(dl)

Re: ISO-8601 WeekNo + variants...

Posted: Fri Jun 22, 2018 8:33 pm
by Kwai chang caine
The code have really short way of in ten years :shock: :lol:
Thanks for sharing 8)