WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sample code
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Holidays & Work Day Calculation

Keywords: determine holidays weekends 

Question:

I need to calculate a date which is X work days ago excluding weekends and HOLIDAYS. Is there any way to determine if a day is a holiday such as Christmas, Thanksgiving, and so on?

Ex. Today is 12/27/98 and I need to know the date of 3 working days ago. Ans: 12/22/98 because 12/25 is holiday and 12/26, 12/27 are weekends. Since some holidays are floating, I'd have to create a huge lookup table for every year, ugh. Does C++ have work day calculation?

Hope you have a better solution.

Thank you.

Answer:

;UNDEBUGGED UNTESTED CODE !!!!

now=TimeYmdHms()
ThreeDaysAgo=TimeSubtract(now,"0000:00:03:00:00:00")

;Now check the date.  

;If it is a weekend OR a holiday move back a day.
;repeat until selected day is NOT a weekend of holiday

while 1
   gosub CheckWeekendStatus
   if Weekend==@TRUE
      ThreeDaysAgo=TimeSubtract(ThreeDaysAgo,"0000:00:01:00:00:00")
      continue ; while loop
   endif
   gosub CheckHolidayStatus
   If Holiday==TRUE
      ThreeDaysAgo=TimeSubtract(ThreeDaysAgo,"0000:00:01:00:00:00")
      continue ; while loop
   endif
   break
endwhile

Message("Desired Day is",ThreeDaysAgo)

exit

:CheckWeekendStatus
    WeekEnd=@FALSE
    ;This example grabs the Time/Day of the Week.
    b=TimeJulianDay(ThreeDaysAgo) 
    c=(b+5) mod 7   ;sun=0 mon=1 ... sat=6
    if c==0 || c==6 then Weekend=@TRUE
    return

    
:CheckHolidayStatus
;Even though the algorithm could understand 
;fixed holidays such as King Montgomery's 
;birthday and the Holy Starlight Festival.
;It seemed best to sinmply put all they days 
;you wish to call a holiday into a database 
;so that it may be more easily ;customized to 
;what you considered a holiday. 

;The Holiday.ini file should look a bit like
;(note single digit days and months have a leading zero
;
;   [HOLIDAYS]
;   2000:01:01=1
;   2000:07:04=1
;   2000:12:25=1
;   2001:01:01=1
;   2001:07:04=1
;   2001:12:25=1
;   

inifile=strcat(DirHome(),"holiday.ini")
dateonly=Strsub(ThreeDaysAgo,1,10)
holiday=IniReadPvt("HOLIDAYS",dateonly,0,inifile)
return

Article ID:   W14395
Filename:   Holidays and Work Day Calculation.txt
File Created: 2000:01:05:00:15:58
Last Updated: 2000:01:05:00:15:58