Jump to content
Sign in to follow this  
cando

Recursive function to return DST change date

Recommended Posts

cando

Hi folks,

 

I'm trying to define a calculation that will return the date of the change to Standard Time for any given year (the second Sunday in November in North America), based on the current date. I need to do the same thing for the change to DST in the spring, but I figure once I have one the other will be easy. I've been trying to use a recursive custom function - the idea being to increment a date and return its DayOfWeek value, starting from Nov 1 until 2 Sundays have been counted: The day of the week for Nov 1 for the current year would be DayOfWeek( Date( 11; 1; Year( Get( CurrentDate )))). After that I'm pretty much stymied. Any ideas?

Share this post


Link to post
Share on other sites
comment

I believe it's the second Sunday in March and the first Sunday in November.

http://wwp.daylight-saving-time.com/usa/dst-2007.htm

 

In any case, you don't need to count two Sundays to find the second Sunday. The second Sunday of any month cannot be earlier than the 8th of the month. So you can start with that, exit when you hit Sunday, increment if you don't. That way you'll never have more than 7 dates to check. The truth is you don't even need a recursive calculation for this, but what the heck, it's good practice.

Share this post


Link to post
Share on other sites
cando

Guess I misread the week: I knew it was changing this year.

 

Edit: My head must be more muddled than I thought. I suppose all I really need to do here is use a Case structure and test for any of the first 7 days in the month being Sunday. hmmm. Thanks for pointing me in the right direction.

Share this post


Link to post
Share on other sites
comment

Uhm... that's not what I meant. A Case() function doing 7 tests is still a recursive calculation, albeit a primitive one. I meant this could be computed, arithmetically.

Share this post


Link to post
Share on other sites
cando

OK. I'll bite. How? I know you're the Calc King, comment. smiley-wink

Share this post


Link to post
Share on other sites
comment

Sorry, that wasn't a tease. I need to keep a few cards up my sleeve.

Share this post


Link to post
Share on other sites
cando
I need to keep a few cards up my sleeve.

In fairness, I can appreciate that. But I don't understand why you've suddenly decided to play coy. From your past contributions, I've no doubt you have a solution for this. But I think it's better not to "comment" when you really don't intend to follow through. No offense.

Share this post


Link to post
Share on other sites
LaRetta

Well I would think a great hint would be better than no help; in fact, a good hint is a great compliment which should be embraced as an opportunity to think through the logic! There are many ways of solving this.smiley-undecided smiley-laughing

 

1) Go backwards to the last day of the prior month to normalize your approach: theDate - Day ( theDate )

 

2) Go to the prior Saturday (from that start date ... sorry, I'm rusty at date calcs) of that month: start - DayOfWeek ( start ) . Then ahead 8 (for first Sunday) or 15 (for second Sunday):

 

Let (

start = theDate - Day ( theDate ) ;

start - DayOfWeek ( start ) + 8

)

 

There are many ways of approaching it without going clear to the beginning; this is a rather old-fashioned approach. smiley-wink

 

LaRetta

Share this post


Link to post
Share on other sites
LaRetta

Oooops. This was a recursive function forum. Opps!

 

Well - it isn't needed. smiley-laughing

Share this post


Link to post
Share on other sites
cando
A good hint is a great compliment

Well, I don't mean to sound ill-tempered here, but I don't see how telling me that this can be done arithmetically can be considered a "good hint"; or any kind of hint.

 

And, LaRetta, while I've appreciated some of your past posts as well, you've got me scratching my head with this one. Using 1) just takes me one day prior to where I started (and then what?) and 2) "Go to the prior Saturday" doesn't get me anywhere either. Finding the prior Saturday is the same problem as finding the first (or second) Sunday. I'm not so rusty with date calcs myself, but it looks like I'll just have to resort to a Case() test (which works, even if it's not so elegant or fast).

Share this post


Link to post
Share on other sites
LaRetta

Well for goodness sakes, why don't you plug a date into it in an FM file, bring up your calendar and see what you get? You will be surprised what you learn if you DO the suggestions and watch them in action. Break the calc apart - put the first part in a calc - and the second part in a calc and see what it does!!

 

If it doesn't rock your world - you're in the wrong business. smiley-laughing

 

Create a global date. And use that in place of that first date. You will see that, no matter what date you put in, it will respond the same for that particular month.

 

We differ, I suppose ... I consider what Comment said to be a GREAT HINT which drove me directly to solving it with the approach I chose (which isn't the best, but it doesn't need to be). He gave the clue needed! Good grief!

 

dog gone it ... you've gotten me hot. The hint he gave is that it can never been less than day 8. You just aren't thinking at all - only pissing and moaning I even explained WHY my calc did what it did. Would you rather have had a convoluted custom function?

 

But since you know you can do it with a Case() then why didn't you? Good grief.

 

Indeed - why take my word for it - test it...

Share this post


Link to post
Share on other sites
cando

LaRetta, suffice it to say I'm off with my tail between my legs. My apologies. And thanks. I just need to check in the mirror to see if you didn't blow my head off.

Share this post


Link to post
Share on other sites
comment
I think it's better not to "comment" when you really don't intend to follow through. No offense.

 

It's a dilemma. If I didn't say anything, I would be implying that I think a recursive calculation is the best solution, or the only solution.

Share this post


Link to post
Share on other sites
mfif

Howzabout

 

Date ( 11 ; 15 ; Year ( ReferenceDateField ) - Day ( 11 ; 14 ; Year ( ReferenceDateField )

 

?

Share this post


Link to post
Share on other sites
LaRetta
I just need to check in the mirror to see if you didn't blow my head off.

 

I hope you think twice before you are insulting to two people who's only purpose was to help you. Adding the 'no offense intended' doesn't remove the offense; you obviously knew your words would be offensive or you wouldn't have added that either. Playing victim now gets you no further in my book.

 

What impresses me is someone who AT LEAST trys a suggestion made; and who appreciates ALL help given whether a little or a lot. We have no obligation to tell you everything we know - where did you ever get that impression? We have worked thousands of sweat-filled hours to learn techniques; some we share, some we don't. Nothing fries my butt more than someone that implies we have no rights to keep ANY of that to ourselves.

Share this post


Link to post
Share on other sites
comment

Hey, the man apologized (not that I was offended, even). Leave him be. I would check the mirror myself after such a blast. :eek:

Share this post


Link to post
Share on other sites
LaRetta

Um, yes Michael, I read it later. I had been upset about other things and I took it out on him.

 

 

Cando, I sincerely apologize. I was out of line.

Share this post


Link to post
Share on other sites
mfif

I meant

 

Date ( 11 ; 15 ; Year ( ReferenceDateField ) - DayOfWeek ( 11 ; 14 ; Year ( ReferenceDateField )

Share this post


Link to post
Share on other sites
cando

Let's just say we both lost our cool, LaRetta. My poor excuse is work pressure to produce results. But as well as providing me with a useful solution, you reminded me that one of the main reasons I did go into this business was the great pleasure I get from creative logic.

Share this post


Link to post
Share on other sites
This thread is quite old. Please start a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  



×
×
  • Create New...

Important Information

Terms of Use