[optimization] calendar week ISO 8601 / DIN 1355

Malte Pfaff-Brill revolution at derbrill.de
Sun Mar 28 16:52:59 EDT 2010


Hi all,
I needed to calculate the calendar week from a given date (as we do it in germany and I think also in the rest of europe). 
I quickly hacked together a function implementing some of the info found here: http://en.wikipedia.org/wiki/ISO_8601#Week_dates
If anyone has any pointers on how to make this better, I´d appreciate input. I hope if anyone needs to implement calendar weeks
later this might be a helpful thread.

Cheers,

Malte

/* function calendar week 
@param1 = date in english format
@return = the calendar week in ISO 8601 format
*/

function calendarWeekISO8601 pEnglishdate
   -- Calendar week as described in ISO 8601 / DIN 1355
   local tFirstMonday,tDate,tCurrentday,tLastYear,tLastFirstJan,tLastThrityFirstDec
   put pEnglishDate into tDate
   -- convert to date items, to get  year in question
   convert tDate from english date to dateItems
   -- 01/04 is ALWAYS in 1st week in year   
   put "01/04/"&item 1 of tDate into tFirstMonday
   convert tFirstMonday from english date to dateItems
   -- if item -1 = 1, we got a sunday. We need to find first monday
   if item -1 of tFirstMonday = 1 then 
      put 8 into tCurrentday
   else
      put item -1 of tFirstMonday into tCurrentday
   end if
   put tCurrentday - 1 into tCurrentday
   -- get first monday in year
   if tCurrentday>1 then
      subtract (tCurrentday -1) from item 3 of tFirstMonday
   end if
   convert tFirstMonday from dateItems to seconds
   convert pEnglishDate from english date to seconds
   -- if the passed date is smaller than the first calendar weeks monday, the date 
   -- is within the last week of the previous year
   if tFirstMonday > pEnglishDate then
      -- check for number of weeks in last year
      put item 1 of tDate -1 into tLastYear
      -- get day of last years 1st January
      put "1/1"&tLastYear into tLastFirstJan
      convert tLastFirstJan to dateItems
      put item -1 of tLastFirstJan into tLastFirstJan
      -- get day of last years 1st January
      put "12/31"&tLastYear into tLastThrityFirstDec
      convert tLastThrityFirstDec to dateItems
      put item -1 of tLastFirstJan into tLastThrityFirstDec
      -- check if it is a leap year first
      if (tLastYear mod 400 = 0) or (tLastYear mod 100 <> 0) and (tLastYear mod 4 = 0) then
         -- leap year!
         -- if a leap year starts on a wed and ends on a thurs, the year owns 53 weeks
         if tLastFirstJan=4 and tLastThrityFirstDec=5 then
            return tLastYear&"-W53"
         else
            return tLastYear&"-W52"
         end if
      else
          -- no leap year!
         -- if a regular year starts and ends on a thurs, the year owns 53 weeks
         if tLastFirstJan=5 and tLastThrityFirstDec=5 then
            return tLastYear&"-W53"
         else
            return tLastYear&"-W52"
         end if
      end if
   else
      return item 1 of tDate&"-W"&format("%02s",(pEnglishDate-tFirstMonday) div 604800 + 1)
   end if
end calendarWeekISO8601


More information about the use-livecode mailing list