ISO-8601 WeekNo + variants...

Share your advanced PureBasic knowledge/code with the community.
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

ISO-8601 WeekNo + variants...

Post 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?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: ISO-8601 WeekNo + variants...

Post 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)
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: ISO-8601 WeekNo + variants...

Post by Kwai chang caine »

The code have really short way of in ten years :shock: :lol:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply