/*_____________________________________________________________________________ |/ / Week number add-on by David "Saturn" van Moolenbroek | Version 0.1, released 11-01-2005 -- support on #help.script/Quakenet | Use/modify however you want, but please keep my name in it. Thank you! | | Keywords: date/time, tokens | | This add-on provides an identifier that returns the weeknumber for a given | UNIX time. The first week is defined to start at the first monday of the | year, and new weeks start on monday. Days before the first monday of the | year are considered to be part of the last week of the previous year. | | The script defines the following commands/identifiers: | | $week(ctime) | | Properties: year | | Returns the week number for the given UNIX ctime. If the year property | is specified, the year (in yyyy format) to which the week belongs is | returned; this may or may not be the same year as the supplied date. | If the supplied input is invalid, $null is returned. | | Simple example | | echo -a $week($ctime) \ _\_____________________________________________________________________________ */ alias -l leapyear return $iif($ctime(29/2/ $+ $$1),1,0) alias -l yearstart { var %daynames = Tue Wed Thu Fri Sat Sun Mon return $calc(8 - $findtok(%daynames,$asctime($ctime(01/01/ $+ $$1),ddd),32)) } alias week { var %daycount = 0 31 59 90 120 151 181 212 243 273 304 334 ; get some data from the supplied unix time tokenize 32 $asctime($$1,d m yyyy) ; make sure the input was valid if ($0 < 3) return ; get the day-of-year for the supplied date var %day = $calc($1 + $gettok(%daycount,$2,32)) if ($2 > 2) && ($leapyear($3)) inc %day ; get the day-of-year where the first week starts var %start = $yearstart($3) ; if the day-of-year is before the start-of-year, it's in one of last years' weeks if (%day < %start) { tokenize 32 $1-2 $calc($3 - 1) inc %day $calc(365 + $leapyear($3)) var %start = $yearstart($3) } ; subtract the start-of-year from the day-of-year dec %day %start ; if the year property was specified, return the resulting year if ($prop == year) return $3 ; otherwise, get the week number from the result return $int($calc(%day / 7 + 1)) }