BBC BASIC for Windows
Programming >> Libraries >> week of year in Datelib
http://bb4w.conforums.com/index.cgi?board=libraries&action=display&num=1428667742

week of year in Datelib
Post by jeroengroenendaal on Apr 10th, 2015, 12:09pm

Hello,

The DateLib does not have function to get the week of year back (and reverse)

What is the best and most accurate way to return the week number from a Julian Day Number. And reverse? By providing Week of Year number and Day of Week number?

So I can make a function equally like the _mjd(D%,M%,Y%) and _year(J%) in the Datelib ?


Many thanks for any help!

Re: week of year in Datelib
Post by rtr2 on Apr 10th, 2015, 1:25pm

on Apr 10th, 2015, 12:09pm, jeroengroenendaal wrote:
The DateLib does not have function to get the week of year back (and reverse)

That's because there is no universally-agreed definition of 'week number'. There are ISO 8601 weeks (begin on a Monday, and week 1 contains the first Thursday of the year), BBC Programme weeks (begin on a Saturday, and week 1 contains the first Tuesday of the year), HMRC Tax weeks (week 1 begins on 6th April) and others.

Although I might suggest BBC week numbers are most appropriate for BBC BASIC, I'm not sure that everybody would agree!

So it is necessary to know which definition of week number you are using, and once you have that definition it's probably not difficult to work out the appropriate algorithm.

Richard.
Re: week of year in Datelib
Post by jeroengroenendaal on Apr 10th, 2015, 2:34pm

Richard,

I forget to mention.

Currently we use agenda's from Brepols to plan our external fitters which uses the ISO 8601 standard.

Although I am a very proud BBC (Basic) user. Nobody at our Service & Quality department does really care so I would like to keep the ISO 8601 standard.

Your answer points me the direction to write a solution myself:
http://en.wikipedia.org/wiki/ISO_week_date#Calculation

which should be do-able with my skills.

Thanks!
Jeroen


Re: week of year in Datelib
Post by jeroengroenendaal on Apr 14th, 2015, 11:20am

For those who are interested, I have made some functions to get DATELIB working with ISO 8601 week numbers.



Code:
      REM converts Day of Week, Week number + Year to a Julian Day Number.
      DEFFN_wmjd(DD%,W%,Y%)
      LOCAL O%(),M%,E%
      DIM O%(12)
      D%=W%*7+DD%-(FN_dow2(FN_mjd(4,1,Y%))+3)
      IF FN_leap(Y%) THEN O%()=0,31,60,91,121,152,182,213,244,274,305,335,366 ELSE O%()=0,31,59,90,120,151,181,212,243,273,304,334,365
      J%=0:M%=0
      FOR A%=1 TO 12:IF O%(A%)>=D% AND M%=0 THEN D%-=O%(A%-1):M%=A%
      NEXT A%
      IF M%=0 THEN
        M%=1:Y%+=1
        IF FN_leap(Y%-1) D%-=366 ELSE D%-=365
      ENDIF
      = FN_mjd(D%,M%,Y%)

      REM converts Julian Day to Week number ( and Year )
      DEFFN_week(J%, RETURN Y%)
      LOCAL W%
      Y%=FN_year(J%)
      W%=INT((FN_doj(J%)-FN_dow2(J%)+10)/7)
      IF W%<1 THEN Y%-=1:W%=FN_longyear(Y%) ELSE IF W%>FN_longyear(Y%) THEN Y%+=1:W%=1
      =W%

      REM Find the day-of-week (1-7, 1 = Monday) from the MJD
      DEF FN_dow2(J%)
      =((J%+2400001) MOD 7)+1

      REM Find the day-of-year
      DEF FN_doj(J%)
      LOCAL O%(),D%,M%,Y%
      DIM O%(12):D%=FN_day(J%):M%=FN_month(J%):Y%=FN_year(J%)
      IF FN_leap(Y%) THEN O%()=0,31,60,91,121,152,182,213,244,274,305,335,366 ELSE O%()=0,31,59,90,120,151,181,212,243,273,304,334,365
      =O%(M%-1)+D%

      REM Is Y% a leap year or not?
      DEF FN_leap(Y%)
      = (Y% MOD 4 = 0) AND ((Y% MOD 400 = 0) OR (Y% MOD 100 <> 0))

      REM Has Y% 53 weeks or 52 weeks?
      DEF FN_longyear(Y%)
      LOCAL S%,E%
      S%=FN_dow2(FN_mjd(1,1,Y%))
      E%=FN_dow2(FN_mjd(31,12,Y%))
      IF FN_leap(Y%)=FALSE AND S%=4 THEN =53
      IF FN_leap(Y%)=TRUE AND S%=3 THEN =53
      IF FN_leap(Y%)=FALSE AND E%=4 THEN =53
      IF FN_leap(Y%)=TRUE AND E%=5 THEN =53
      IF S%=4 AND E%=4 THEN =53
      =52