Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type to easily add or substract business days #53

Open
junder873 opened this issue Sep 17, 2021 · 0 comments
Open

Add type to easily add or substract business days #53

junder873 opened this issue Sep 17, 2021 · 0 comments

Comments

@junder873
Copy link

I think it would be convenient if you could do date arithmetic. For example, if I want somebody to pass a period to another function, I want to be able to have that period be a day, month, or business day. I think this could be pretty simply done by adding a new type:

struct BDay <: DatePeriod
    value::Int64
    calendar::Union{Symbol, String}
    BDay(v::Number, cal::Union{Symbol, String}) = new(v, cal)
end

Base.:(+)(dt::Date, z::BDay) = advancebdays(z.calendar, dt, z.value)
Base.:(-)(dt::Date, z::BDay) = advancebdays(z.calendar, dt, -1 * z.value)

Which allows for easy calculations:

d1 = today()
c = BDay(1, :USNYSE)
d1 - c
# 2021-09-16

And for writing easy functions:

change_days(dt::Date, c::DatePeriod) = dt + c

change_days(today(), Day(2)) # 2021-09-19
change_days(today(), BDay(2, :USNYSE)) # 2021-09-21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants