IsoWeekNum

From Pickwiki
Revision as of 21:15, 20 October 2006 by SLand (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This function takes an external format date (hence the first, ICONV, line) and then converts it to the week no. It calculates the Thursday by subtracting the days elapsed that week then adding back 4 days. It then finds out what "day of the year" that Thursday is. Dividing by 7 gives the number of weeks, but it needs the "+6" tweak to cope with the fact that some of the arithmetic (div7) starts at 0, while some of it (day, week numbers) start at 1

* Returns the ISO week number for a given date
************************************************************************

      FUNCTION ISOWEEKNUM( USERDATE)

************************************************************************

      INTERNALDATE = ICONV( USERDATE, "D")

      DAYOFWEEK = OCONV( INTERNALDATE, "DW") ;* return day number

      IF DAYOFWEEK = 0 THEN DAYOFWEEK = 7     ;* some variants of Pick return a 0 for Sunday instead of 7

      THURSDAY = INTERNALDATE - DAYOFWEEK + 4 ;* calculate date for that Thursday

      JULIANDAY = OCONV( THURSDAY, "DJ") ;* This is the days so far this year

      WEEKNUM = INT( (JULIANDAY + 6) / 7 ) ;* Add 6 so "Thu 1 Jan" returns 1

      RETURN (WEEKNUM)
   END