quinnj / Datetime.jl

A Date and DateTime implementation for Julia; Now deprecated in favor of https://github.com/quinnj/Dates.jl
Other
20 stars 15 forks source link

Function to calculate business days between two dates #38

Closed msravi closed 10 years ago

msravi commented 10 years ago

It would be a nice enhancement to have a function that returns the number of business days between two dates, discounting Saturdays, Sundays, and holidays. Below is one such implementation that takes two dates and a list of holidays, and returns the number of business days between them:

function bizDays(frmDate, toDate, holidays=[])
  dbw = int(floor(float(toDate-frmDate)/7)*5 + (dayofweek(toDate)>=dayofweek(frmDate) ? (dayofweek(toDate)>5 ? 5 : dayofweek(toDate))-dayofweek(frmDate) : (5-(dayofweek(frmDate)>5 ? 5 : dayofweek(frmDate)))+dayofweek(toDate)))
  dbw -= length(find(h->(frmDate < h <= toDate), holidays))
  return dbw
end

The implementation assumes that the frmDate is always exclusive of the range and the toDate is always inclusive.

Regards, Ravi

Edit: Corrected typo in variable names

quinnj commented 10 years ago

This issue has been moved to the DateExtensions.jl package where it can be pursued there (a new package for experimental features).

https://github.com/quinnj/DateExtensions.jl/issues/1