Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Next Friday Function 4

Status
Not open for further replies.

GriffMG

Programmer
Mar 4, 2002
6,288
1
38
FR
Just a bit of old code... to give you the date of the next Friday.

Code:
FUNCTION NEXTFRIDAY
	PARAMETERS m.MYDATE
	PRIVATE m.MYDATE
	IF PCOUNT() < 1
		m.MYDATE = DATE()
	ENDIF
	m.MYDATE = m.MYDATE + (6-(DOW(m.MYDATE,1)-(INT(DOW(m.MYDATE,1)/6)*7)))
	RETURN(m.MYDATE)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Works for me, Griff.

One tiny improvement:

Instead of this:
Code:
PARAMETERS m.MYDATE
PRIVATE m.MYDATE

you could do this:

Code:
LPARAMETERS m.MYDATE

saving a whole line of code.

I would also prefer to lose the mdots on the left of the assignment statements, but I won't open that can of worms again.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Mike L,

Yes, I have never really adopted the lparameters approach, and here it is more appropriate than my 'long hand' B-)

The m.dots on the left side may not be necessary, because you can't assign to a field, obviously enough, but I'm happy
with that omission to improve clarity (in my opinion) and the habit of using them reduces the chances of a typing error
creeping in.

Hi Mike Y

I do use Hungarian notation, but only where I am maintaining code developed by someone else - for example I sometimes
customise WestWind utilities and I use the same notation Rick does, again not necessary - but I think it is better
to use 'the same pen' as the originator and not faff about with chalk on his finely crafted calligraphy!

Edit
Of course, these days I'm pretty confident the only person maintaining my code is likely myself... so it's only
public if I present it here.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Use the right value for DOW and you can reduce the math very much.

Chriss
 
Hi Chriss

Are you thinking if I used the DOW pointing to a Friday base?

Code:
DOW(m.MYDATE,6)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Maybe that or 7. I'm just sure, but there's a way the 1...7 range of DOW is exactly opposite of the 0...6 sequence you actually need, 0 for when you're at Friday already. Well, unless you would add 7 for next Friday on a Friday. Anyway you like it, you can get date+7-dow(date,n) or date+8-dow(date,n) to work as you need. No need for a portion INT(DOW(m.MYDATE,1)/6)*7 as a correction term.


Chriss
 
I can see how using Friday in DOW() as a base for calculating the next Friday ought to simplify the math, but the clue is in the name.

Next Friday is not equal to today, it has to be at least a day away, otherwise it would be nearest Friday B-)

I started this thread for two reasons, firstly as MY kinda alluded to, in most cases iterating through a loop is probably
less efficient to 'doing the math' - I was reading something recently where even branching on the ELSE was considered
'inferior' - maybe, maybe not. Secondly I wanted to demonstrate that a discussion often generates something actually better
which might be unexpected. NEXTFRIDAY() is nearly forty years old, it isn't pretty, perhaps not as efficient as three people
can make it, but it is - this is key - an example of the shoulders on which we all stand.

We stand on trillions of lines of other peoples code, good, bad or WTF.

I'm pretty happy with that... but I am most certainly going to try and get my grey cells to follow Chris's advice.
Trouble is, they are ... sorry, who are you?

Thank you gentlemen, ahem, people, have a nice evening

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
There's a much better way to do this, because not only the number of function calls matters, also their cost. I already have seen mathematiicians argue with just the number of operatons for algortihms and fail in their estimation because they know very little about operator costs. And they should know once they enter the realm of computer science.

Anyway, it was never fruitful to show you something, I'll just leave that here and don't bother.

Chriss
 
Taking the various comments on board, save the unnecessary assignment to an m.dot variable, this would be
the likely simplification of NextFriday()

Code:
FUNCTION NEXTFRIDAY
	LPARAMETERS m.MYDATE
	IF PCOUNT() < 1
		m.MYDATE = DATE()
	ENDIF
	** use DOW(x,6) for Friday based return value
	m.MYDATE = m.MYDATE + (8-DOW(m.MYDATE,6))
	
	RETURN(m.MYDATE)

Faster, more readable, easily modified for NextMonday() etc.



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
For purists perhaps B-)

Code:
FUNCTION NEXTFRIDAY
	LPARAMETERS m.MYDATE
	m.MYDATE = IIF(PCOUNT() < 1, DATE(), m.MYDATE)
	** use DOW(x,6) for Friday based return value
	RETURN(m.MYDATE + (8-DOW(m.MYDATE,6)))
ENDFUNC

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
EVL is also helpful. Often makes unnecessary to look into PCOUNT anymore, as VFP sets not passed-in parameters to .F. and .F. is empty.
I guess I'm alone in thinking the initial value of a LOCAL var or a parameter should be .NULL., anyway.
I get your point about next, Griff. Just even VFP itself is using REPLACE NEXT 1 for the record itself and not really the next one. I don't assume that was programmed by a non-american. If it comes to language, there's a lot to say about English and Americans about their own language usage, too, dialects and other quirks. Anyway, just spare any more comments on improvements.

Ah, yes, there is a nice read for the weekend, perhaps, by Tamar, about many Foxpro quirks, as well.
Chriss
 
Hi Chris

So test for Empty() instead of pcount()?

Code:
m.MYDATE = IIF(EMPTY(m.MYDATE), DATE(), m.MYDATE)

With a local parameter (lParameter) that would work, would it with a private? Not that it is relevant in this example!



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Chriss said:
there is a nice read for the weekend, perhaps, by Tamar, about many Foxpro quirks, as well.

Thanks for posting that link, Chris. It's something we should all take the time to read.

For the same article in a possibly more user-friendly format, I suggest The text is identical, but it doesn't have the formatting issues that I see on the PDFSlide site (which might be specific to my system / browser).

While you are at take a look at some of Tamar's other articles and conference papers. There's a lot of good stuff there.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Griff, not IIF with EMPTY(), EVL(). m.MyDate= EVL(m.MyDate,DATE()). Unless you absolutely want to be backward compatible, I don't see a benefit of that when I have my project being a VFP9 project, though, there only is a benefit of backward compatible programming to a framework or libaray you absolutely want to use in older projects.

If you don't pass something to a private paramater, it also becomes .F., that's simple to see just doing it:
Code:
test()

Function test()
   Parameters p1
   ? p1

Chriss
 
Yes Chris, I can see how that would work.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
I think Chris suggested either 6 or 7, and gave rough examples.

I chose to simplify it to dow(,6) because it was then clear, to me at least, that NextSaturday(), NextTuesday() etc.
would only require modifying that index - maybe ,7) would too - but I haven't tested that.

No disrespect was intended.

As coders we all rely on the people who came before us, who built the OS we work on, the compilers that made that possible,
the people who developed dBase, Fox, Delphi, C, assemblers...

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Thinking in the wrong direction, not exhausting possibilities.

Chriss
 
It's simple math, it's not even really special math, just the four basic mathematical operations, in which I include modulo, there's nothing to be proud about. I did this in school, calculating julian day number is not hard and faster done than SYS(11) plus string conversion. And then date differences and DOW also become two derived results from the same value, whereas your code is repetitive in that aspect, as you just never thought about skipping inbuilt functions and doing all math yourself. That's were you actually fail to impress me and I have to give it back to you, that I am astonished it's still not obvious to you. One more point about it is that it's not even halfways genius to find Julian day numbers useful when Unix already had the idea to make it the norm for dates to simply be a number of days or seconds or now even nanoseconds from a root datetime. You can do all the essential calculations in it simpler, there's no art nor craftsmanship in this, it's pure knowledge that even preexisted computer science.

I actually already posted details about how you go about this, just not in this forum. Anyway, why do you even ask? You don't need anybody's enlightenment anyway.
You're sketching scenarios that are not true, that hass actually again become quite unbearable.

Chriss
 
Just want to point out that I did NOT give permission for PDFSLIDE to share my documents. I'm about to report the violation of my copyright.

Please if you're sharing my papers, do so from my website:
Thanks,
Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top