Last Friday of each month: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations, moved documentation for the lastDOW function to a separate window.) |
|||
Line 2,007: | Line 2,007: | ||
This REXX program will find the last day-of-week (for any day) of all the months for any year. |
This REXX program will find the last day-of-week (for any day) of all the months for any year. |
||
<br>It wasn't optimized just to find a particular day-of-week. |
<br>It wasn't optimized just to find a particular day-of-week. |
||
⚫ | |||
The documentation for the '''lastDOW''' function (used in the REXX program below): |
|||
<pre> |
|||
╔════════════════════════════════════════════════════════════════════╗ |
|||
⚫ | |||
║ any particular month of any particular year. ║ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
╚════════════════════════════════════════════════════════════════════╝ |
|||
</pre> |
|||
⚫ | |||
parse arg yyyy |
parse arg yyyy |
||
do j=1 for 12 |
|||
say lastDOW('Friday', j, yyyy) /*find last Friday for the Jth month.*/ |
|||
end /*j*/ |
|||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*┌────────────────────────────────────────────────────────────────────┐ |
|||
⚫ | |||
⚫ | |||
if mm=='' | mm=='*' then mm=left( date('U'), 2) /*use default month*/ |
|||
if yy=='' | yy=='*' then yy=left( date('S'), 4) /*use default year */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
└────────────────────────────────────────────────────────────────────┘*/ |
|||
⚫ | |||
parse arg a.1,a.2,a.3 /*orig args, errmsg*/ |
|||
if mm=='' | mm=='*' then mm=left(date('U'),2) /*use default month*/ |
|||
if yy=='' | yy=='*' then yy=left(date('S'),4) /*use default year */ |
|||
⚫ | |||
⚫ | |||
$=" Monday TUesday Wednesday THursday Friday SAturday SUnday" |
$=" Monday TUesday Wednesday THursday Friday SAturday SUnday" |
||
!=" JAnuary February MARch APril MAY JUNe JULy AUgust September" |
!=" JAnuary February MARch APril MAY JUNe JULy AUgust September October November December" |
||
upper $ ! /*uppercase strings*/ |
|||
" October November December" |
|||
⚫ | |||
if dow=='' then call .er "wasn't specified",1 |
if dow=='' then call .er "wasn't specified",1 |
||
if arg()>3 then call .er 'arguments specified',4 |
if arg()>3 then call .er 'arguments specified',4 |
||
do j=1 for 3 /*any plural args ?*/ |
do j=1 for 3 /*any plural args ?*/ |
||
if words(arg(j))>1 then call .er 'is illegal:',j |
if words(arg(j))>1 then call .er 'is illegal:',j |
||
end |
end |
||
dw=pos(' 'dow,$) /*find day-of-week*/ |
dw=pos(' 'dow,$) /*find day-of-week*/ |
||
if dw==0 then call .er 'is invalid:',1 |
if dw==0 then call .er 'is invalid:',1 |
||
if dw\==lastpos(' 'dow,$) then call .er 'is ambigious:',1 |
if dw\==lastpos(' 'dow,$) then call .er 'is ambigious:',1 |
||
if datatype(mm,' |
if datatype(mm,'M') then /*is MM alphabetic?*/ |
||
do |
do |
||
m=pos(' 'mm,!) /*maybe its good...*/ |
m=pos(' 'mm,!) /*maybe its good...*/ |
||
if m==0 then call .er 'is invalid:',1 |
if m==0 then call .er 'is invalid:',1 |
||
if m\==lastpos(' 'mm,!) then call .er 'is ambigious:',2 |
if m\==lastpos(' 'mm,!) then call .er 'is ambigious:',2 |
||
mm=wordpos(word(substr(!,m),1),!)-1 /*now, use true Mon*/ |
mm=wordpos( word( substr(!, m), 1), !) - 1 /*now, use true Mon*/ |
||
end |
end |
||
Line 2,075: | Line 2,078: | ||
if yy>9999 then call .er "can't be > 9999:",3 |
if yy>9999 then call .er "can't be > 9999:",3 |
||
tdow=wordpos(word(substr($,dw),1),$)-1 /*target DOW, 0──►6*/ |
tdow=wordpos(word(substr($,dw),1),$)-1 /*target DOW, 0──►6*/ |
||
/*day# of last dom.*/ |
/*day# of last dom.*/ |
||
_=date('B',right(yy+(mm=12),4)right(mm//12+1,2,0)"01",'S')-1 |
_=date('B',right(yy+(mm=12),4)right(mm//12+1,2,0)"01",'S')-1 |
||
?=_//7 /*calc. DOW, 0──►6*/ |
?=_ // 7 /*calc. DOW, 0──►6*/ |
||
if ?\==tdow then _=_-?-7+tdow+7*(?>tdow) /*not DOW? Adjust.*/ |
if ?\==tdow then _=_ - ? - 7 + tdow + 7 * (?>tdow) /*not DOW? Adjust.*/ |
||
return date('weekday',_,"B") date(,_,'B') /*return the answer*/ |
return date('weekday', _, "B") date(, _, 'B') /*return the answer*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
.er: arg ,_;say; say '***error |
.er: arg ,_; say; say '***error*** (in LASTDOW)'; say /*tell error, and */ |
||
say word('day-of-week month year excess',arg(2)) arg(1) a._ |
say word('day-of-week month year excess',arg(2)) arg(1) a._ |
||
say; exit 13 /*... then exit. */</lang> |
say; exit 13 /*... then exit. */</lang> |
||
'''output''' when using the following input: <tt> 2012 </tt> or <tt> 12 </tt> |
|||
<pre> |
|||
<pre style="overflow:scroll"> |
|||
Friday 27 Jan 2012 |
Friday 27 Jan 2012 |
||
Friday 24 Feb 2012 |
Friday 24 Feb 2012 |