Programming Challenge

Quote from nitro:

First, to readers that don't know, in VB the first day of the week is Sunday = 7, Monday = 1, Tuesday = 2, etc.

The first part of the if clearly works. If the first day of the week is Sunday = 7, then add 19 days + 1 day, and you get the third Friday which would always be the 20th. That always works.

The second part of the if is more complex. If the first of the month is any other day than Sunday, add 1 day to 14 + (6 - DayOfWeekOfFirstDayOfMonth), and that in theory is the third Friday. Doesn't works this month, Feb 2007: 14 + (6 - 4) + 1 = 17th (Unless Thursday is the 5th day in VB?)

Interestingly enough, you can turn this calculation to get the weekofmonth using a little modular arithmetic once you get it working...

nitro

Nitro

Actually Sunday = 1, Monday = 2 etc. By default anyway. So I have assumed Friday = 6.

I have tidied my routine up a little bit. If the first day of the month is after Friday (i.. Saturday, because Sunday is considered BEFORE Friday), I add 3 weeks instead of 2.

Dim ExtraDays As Integer

If Weekday(DateSerial(Year(Now), Month(Now), 1)) > 5 Then
ExtraDays = 21
Else
ExtraDays = 14
End If

Debug.Print DateAdd("d", ExtraDays + (6 - Weekday(DateSerial(Year(Now), Month(Now), 1))), DateSerial(Year(Now), Month(Now), 1))
 
On the subject of calculating Easter, the following Excel worksheet formula is published in Walkenbach's Formula book.

Even Walkenbach cannot explain how it works!!

The 2007 is mentioned twice and is the year for which the calculation is required. It gives 8th April 2007 for this year, Easter Sunday, so it appears to work.

=DOLLAR(("4/" & 2007)/7 + MOD(19*MOD(2007,19)-7,30)*14%,0)*7-6
 
Ruby:

Code:
require 'Date'

def thirdFriday( date )
  (date.wday == Date:: DAYS[ 'friday' ]) and
    (date.cweek - Date.new( date.year, date.mon, 1 ).cweek == 2)
end
 
Quote from nitro:

Clearly works, but it is not elegant imo.

nitro

You have misunderstood. To "calculate whether a given day D is the 3rd Friday of the month", employ the single line

return( (dayOfWeek(D) == FRIDAY) && (dayOfMonth(D) >= 15) && (dayOfMonth(D) <= 21) ) ;
 
Quote from MGJ:

You have misunderstood. To "calculate whether a given day D is the 3rd Friday of the month", employ the single line

return( (dayOfWeek(D) == FRIDAY) && (dayOfMonth(D) >= 15) && (dayOfMonth(D) <= 21) ) ;
Ah!!!

Does that really work? I guess so. That would be elegant.

nitro
 
That's nice because it would work under Unix too.

nitro
Quote from rufus_4000:

Something like this would work in C, it returns the 3rd friday for any given month / year. Let me see about a Java equivalent:

#define DEF_FRIDAY 5
#define DEF_DAYINAWEEK 7

int get_3rd_friday (int _mon, int _year) {
struct tm cwtm;
int yr, tmoffset;

yr = _year - 1900;

cwtm.tm_sec = 0 ; cwtm.tm_min = 0 ; cwtm.tm_hour = 1 ;
cwtm.tm_mday = 1 ; cwtm.tm_mon = _mon ; cwtm.tm_year = yr;
mktime (&cwtm);

tmoffset = DEF_FRIDAY - cwtm.tm_wday;
if (tmoffset < 0) tmoffset += DEF_DAYINAWEEK;
tmoffset += 2 * DEF_DAYINAWEEK + 1;

return tmoffset;
}
 
Quote from nitro:

Give the source code of a function that takes in a DateTime, and calculates whether the given day is the 3rd Friday of the month. There is more than one way to do this, but elegance is always preferred.

This is useful for calculating whether the Friday is options expiration Friday or not.

Programming language is up to you, but preferably C or Java or Basic.

nitro

This is what I have been using in VB (returns 3rd friday of the curr month):

debug.Print (7 - (Weekday(Dateserial(Year(Now),Month(Now),1)) Mod 7) + 14)

basically transforms Weekday result to 1=fri, 2=thu, 3=wed ... 7=sat
then simply adds 14 to it
 
Quote from edil:

This is what I have been using in VB (returns 3rd friday of the curr month):

debug.Print (7 - (Weekday(Dateserial(Year(Now),Month(Now),1)) Mod 7) + 14)

basically transforms Weekday result to 1=fri, 2=thu, 3=wed ... 7=sat
then simply adds 14 to it
This wins. No if statements. Pure math. Fits on one line.

Nice!

nitro
 
Back
Top