Useful Time Functions
vining
Posts: 4,368
Here's some useful time functions. There are similar functions out there but I can never find them when I need them so I made these.
This uses the standard TIME keyword format so you can just pass it "TIME" and it will return that time in total amount of seconds so you can do math on it.
This uses the standard TIME keyword format so you can just pass it "TIME" and it will return that time in total amount of seconds so you can do math on it.
DEFINE_FUNCTION LONG fn24HourTimeTo_Seconds(CHAR iTime[]) { RETURN ((((ATOI(REMOVE_STRING(iTime,"':'",1)) * 60) * 60) + ATOI(REMOVE_STRING(iTime,"':'",1)) * 60) + ATOI(iTime)) ; }Once you do your math on the seconds you can convert it back to the "TIME" format with this.
DEFINE_FUNCTION CHAR[8] fnSecondsTo_24HourTime(LONG iTimeInSeconds) { RETURN "right_string("'0',ITOA(iTimeInSeconds / 3600)",2),':', right_string("'0',ITOA((iTimeInSeconds % 3600) / 60)",2),':', right_string("'0',ITOA((iTimeInSeconds % 3600) % 60)",2)" ; }Here's a function which uses these functions to add tiime to the current time. In this case to create an expiration time 10 minutes later.
DEFINE_CONSTANT CHAR WEB_MONTH[12][3] = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'} ; DEFINE_FUNCTION fnUpdateDATE_TIME() { STACK_VAR CHAR cLDATE[10] ; STACK_VAR INTEGER nMonth ; STACK_VAR INTEGER nDay ; STACK_VAR CHAR cYear[4] ; cLDATE = LDATE ; nMonth = atoi(REMOVE_STRING(cLDATE,"'/'",1)) ; nDay = atoi(REMOVE_STRING(cLDATE,"'/'",1)) ; cYear = cLDATE ; cWebCurDateTime = "DAY,', ',itoa(nDay),' ',WEB_MONTH[nMonth],' ',cLDATE,' ',TIME,' EST'" ; cWebExpDateTime = "DAY,', ',itoa(nDay),' ',WEB_MONTH[nMonth],' ',cLDATE,' ', fnSecondsTo_24HourTime(fn24HourTimeTo_Seconds(TIME) + EXPIRES_IN_SECONDS),' EST'" ; RETURN ; }In the above example EXPIRES_IN_SECONDS is a constant of 600. Which is 600 seconds or ten minutes that gets added to the current time to create the expiration time.
0
Comments
Using your example, cWebExpDateTime will not be correct if the function is called to add the 10 minutes at say 11:55 PM. The first problem (which has an easy enough fix) is that the time will come back as 24:05, an illegal time. The day of the month will definitely be wrong as will the day of the week. It?s also possible the month may be wrong and for that matter the year will also be wrong if the function is called on Dec. 31st at 11:55 PM.
Unless the application can live with these limitations the only real way to go in my mind is to work with UNIX time functions when time/date addition/subtraction come into play.
What?s wrong with the FORMAT command? It?s way more flexible and powerful and there isn?t a 6 character limitation.
Instead of this:
All you really need is this:
I?ve been hired as the 2009 FORMAT spokesperson so I was compelled to respond and spread the good word.
</FORMAT_PSA>
That's cheating.
Paul
Paul
yuri wrote: I use it for Sunrise/Sunset and have been doing so for ever but I didn't know it did anything else. Does it? If it does I might have used it and save myself from having to think a little.
I also wondered why there's the built in function "DAY_OF_WEEK" which can return what the day of the week is for any date you plug but there's no other standard functions for time manipulation.
You're kidding right?
Paul
To figure out these days I'll do a DAY_OF_WEEK on 03/01/cCurYear which will tell me the day of the week 1-7 that date is and I'll do math to figure out when the 2nd "1" would be. Then do the same thing for 11/01/cCurYear and then figure out when the first "1" would be.
I thinks this would work and be fairly simple but I've been wrong before...........
Paul
If I was to attempt DST I would just make work for my area and my needs and if others want to use it they could do the same for theirs. Otherwise it would be a real pain to make universal. Still if I did it like iTimeManager and make rules that the programmer has to fill in based on their region that still might not be so bad. DST yes/no, begins/ends. I once wanted to do a module that would figure holidays and when I started looking at Muslim holidays I had to give up since they'll change for any number of reasons like rain or just overcast skies.
As for my needs, I only wanted to add 10 minutes to the current time for a web page expiration time which I don't even need to include in my web page header. I just figured why not just for the heck of it and now we're talking DST rules for the world. :eek: