Skip to content

Commit

Permalink
Merge pull request #12 from raphasampaio/feature/datetime
Browse files Browse the repository at this point in the history
Generalize the date input type
  • Loading branch information
raphasampaio authored Nov 11, 2024
2 parents 36dc4fb + 866e53c commit e089258
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Holidays"
uuid = "6a61c59e-0e35-4be2-9181-ca44005fb204"
version = "0.1.2"
version = "0.1.3"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ julia> ] add Holidays

```julia
using Holidays
using Dates

usa_holidays = Holidays.UnitedStates()

Expand All @@ -45,6 +46,7 @@ usa_holidays = Holidays.UnitedStates()

```julia
using Holidays
using Dates

# check if april 23rd is a holiday in brazil
brazil_holidays = Holidays.Brazil()
Expand Down
6 changes: 3 additions & 3 deletions src/calendar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function fetch_holidays(::Type{AbstractHolidayCalendar})::Vector{Holiday}
return Vector{Holiday}()
end

function find_holidays(calendar::AbstractHolidayCalendar, date::Date)::Vector{Holiday}
function find_holidays(calendar::AbstractHolidayCalendar, date::TimeType)::Vector{Holiday}
found = Vector{Holiday}()

for holiday in calendar.holidays
Expand Down Expand Up @@ -33,10 +33,10 @@ function find_holidays(calendar::AbstractHolidayCalendar; years::AbstractVector{
return holidays
end

function is_holiday(calendar::AbstractHolidayCalendar, date::Date)::Bool
function is_holiday(calendar::AbstractHolidayCalendar, date::TimeType)::Bool
return length(find_holidays(calendar, date)) > 0
end

function Base.in(date::Date, calendar::AbstractHolidayCalendar)::Bool
function Base.in(date::TimeType, calendar::AbstractHolidayCalendar)::Bool
return is_holiday(calendar, date)
end
64 changes: 32 additions & 32 deletions src/calendars/christian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ module Christian

using Dates

is_shrove_sunday(x::Date) = x == shrove_sunday(Dates.year(x))
function shrove_sunday(year::Integer)::Date
is_shrove_sunday(x::TimeType) = x == shrove_sunday(Dates.year(x))
function shrove_sunday(year::Integer)
return easter(year) - Dates.Day(49)
end

is_shrove_monday(x::Date) = x == shrove_monday(Dates.year(x))
function shrove_monday(year::Integer)::Date
is_shrove_monday(x::TimeType) = x == shrove_monday(Dates.year(x))
function shrove_monday(year::Integer)
return easter(year) - Dates.Day(48)
end

is_shrove_tuesday(x::Date) = x == shrove_tuesday(Dates.year(x))
function shrove_tuesday(year::Integer)::Date
is_shrove_tuesday(x::TimeType) = x == shrove_tuesday(Dates.year(x))
function shrove_tuesday(year::Integer)
return easter(year) - Dates.Day(47)
end

is_ash_wednesday(x::Date) = x == ash_wednesday(Dates.year(x))
function ash_wednesday(year::Integer)::Date
is_ash_wednesday(x::TimeType) = x == ash_wednesday(Dates.year(x))
function ash_wednesday(year::Integer)
return easter(year) - Dates.Day(46)
end

is_easter(x::Date) = x == easter(Dates.year(x))
function easter(year::Integer)::Date
is_easter(x::TimeType) = x == easter(Dates.year(x))
function easter(year::Integer)
g = year % 19
c = div(year, 100)
h = (c - div(c, 4) - div(8 * c + 13, 25) + 19 * g + 15) % 30
Expand All @@ -36,48 +36,48 @@ function easter(year::Integer)::Date
return Date(year, month, day)
end

is_good_friday(x::Date) = x == good_friday(Dates.year(x))
function good_friday(year::Integer)::Date
is_good_friday(x::TimeType) = x == good_friday(Dates.year(x))
function good_friday(year::Integer)
return easter(year) - Dates.Day(2)
end

is_easter_monday(x::Date) = x == easter_monday(Dates.year(x))
function easter_monday(year::Integer)::Date
is_easter_monday(x::TimeType) = x == easter_monday(Dates.year(x))
function easter_monday(year::Integer)
return easter(year) + Dates.Day(1)
end

is_chorus_christi(x::Date) = x == chorus_christi(Dates.year(x))
function chorus_christi(year::Integer)::Date
is_chorus_christi(x::TimeType) = x == chorus_christi(Dates.year(x))
function chorus_christi(year::Integer)
return easter(year) + Dates.Day(60)
end

is_ascension_day(x::Date) = x == ascension_day(Dates.year(x))
function ascension_day(year::Integer)::Date
is_ascension_day(x::TimeType) = x == ascension_day(Dates.year(x))
function ascension_day(year::Integer)
return easter(year) + Dates.Day(39)
end

is_whit_monday(x::Date) = x == whit_monday(Dates.year(x))
function whit_monday(year::Integer)::Date
is_whit_monday(x::TimeType) = x == whit_monday(Dates.year(x))
function whit_monday(year::Integer)
return easter(year) + Dates.Day(50)
end

is_assumption_day(x::Date) = x == assumption_day(Dates.year(x))
function assumption_day(year::Integer)::Date
is_assumption_day(x::TimeType) = x == assumption_day(Dates.year(x))
function assumption_day(year::Integer)
return Date(year, 8, 15)
end

is_all_saints_day(x::Date) = x == all_saints_day(Dates.year(x))
function all_saints_day(year::Integer)::Date
is_all_saints_day(x::TimeType) = x == all_saints_day(Dates.year(x))
function all_saints_day(year::Integer)
return Date(year, 11, 1)
end

is_all_souls_day(x::Date) = x == all_souls_day(Dates.year(x))
function all_souls_day(year::Integer)::Date
is_all_souls_day(x::TimeType) = x == all_souls_day(Dates.year(x))
function all_souls_day(year::Integer)
return Date(year, 11, 2)
end

is_advent_sunday(x::Date) = x == advent_sunday(Dates.year(x))
function advent_sunday(year::Integer)::Date
is_advent_sunday(x::TimeType) = x == advent_sunday(Dates.year(x))
function advent_sunday(year::Integer)
christmas = christmas_day(year)

# find the fourth Sunday before Christmas
Expand All @@ -88,13 +88,13 @@ function advent_sunday(year::Integer)::Date
return first_advent - Dates.Day(day_of_week % 7)
end

is_christmas_day(x::Date) = x == christmas_day(Dates.year(x))
function christmas_day(year::Integer)::Date
is_christmas_day(x::TimeType) = x == christmas_day(Dates.year(x))
function christmas_day(year::Integer)
return Date(year, 12, 25)
end

is_boxing_day(x::Date) = x == boxing_day(Dates.year(x))
function boxing_day(year::Integer)::Date
is_boxing_day(x::TimeType) = x == boxing_day(Dates.year(x))
function boxing_day(year::Integer)
return Date(year, 12, 26)
end

Expand Down
2 changes: 1 addition & 1 deletion src/calendars/gregorian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Gregorian

using Dates

function is_new_years_day(x::Date)
function is_new_years_day(x::TimeType)
return Dates.month(x) == 1 && Dates.day(x) == 1
end

Expand Down
4 changes: 2 additions & 2 deletions src/calendars/international.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module International

using Dates

function is_womens_day(x::Date)
function is_womens_day(x::TimeType)
return Dates.month(x) == Dates.Mar && Dates.day(x) == 8
end

function is_workers_day(x::Date)
function is_workers_day(x::TimeType)
return Dates.month(x) == Dates.May && Dates.day(x) == 1
end

Expand Down
10 changes: 5 additions & 5 deletions src/calendars/islamic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3707,14 +3707,14 @@ const TASUA_DATES = Set{Date}([
Date(2076, 12, 05),
])

is_eid_al_fitr_day(x::Date) = x in EID_AL_FITR_DATES
is_eid_al_fitr_day(x::TimeType) = x in EID_AL_FITR_DATES

is_eid_al_fitr_day_two(x::Date) = (x - Day(1)) in EID_AL_FITR_DATES
is_eid_al_fitr_day_two(x::TimeType) = (x - Day(1)) in EID_AL_FITR_DATES

is_eid_al_fitr_day_three(x::Date) = (x - Day(2)) in EID_AL_FITR_DATES
is_eid_al_fitr_day_three(x::TimeType) = (x - Day(2)) in EID_AL_FITR_DATES

is_eid_al_adha_day(x::Date) = x in EID_AL_ADHA_DATES
is_eid_al_adha_day(x::TimeType) = x in EID_AL_ADHA_DATES

is_mawlid(x::Date) = x in MAWLID_DATES
is_mawlid(x::TimeType) = x in MAWLID_DATES

end
20 changes: 10 additions & 10 deletions src/countries/united_states.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ const Gregorian = Holidays.Gregorian

const UnitedStates = Holidays.UnitedStates

function is_new_years_day(x::Date)
function is_new_years_day(x::TimeType)
return Dates.year(x) >= 1871 && Gregorian.is_new_years_day(x)
end

function is_martin_luther_king_birthday(x::Date)
function is_martin_luther_king_birthday(x::TimeType)
return Dates.year(x) >= 1986 && Dates.month(x) == Dates.Jan && Dates.dayofweekofmonth(x) == 3 && Dates.dayofweek(x) == Dates.Mon
end

function is_washington_birthday(x::Date)
function is_washington_birthday(x::TimeType)
return Dates.year(x) >= 1971 && Dates.month(x) == Dates.Feb && Dates.dayofweekofmonth(x) == 3 && Dates.dayofweek(x) == Dates.Mon
end

function is_columbus_day(x::Date)
function is_columbus_day(x::TimeType)
return Dates.year(x) >= 1971 && Dates.month(x) == Dates.Oct && Dates.dayofweekofmonth(x) == 2 && Dates.dayofweek(x) == Dates.Mon
end

function is_memorial_day(x::Date)
function is_memorial_day(x::TimeType)
return Dates.year(x) >= 1971 && Dates.month(x) == Dates.May && Dates.dayofweek(x) == Dates.Mon && Dates.dayofweekofmonth(x) == Dates.daysofweekinmonth(x)
end

function is_indenpendence_day(x::Date)
function is_indenpendence_day(x::TimeType)
return Dates.year(x) >= 1871 && Dates.month(x) == Dates.Jul && Dates.day(x) == 4
end

function is_labor_day(x::Date)
function is_labor_day(x::TimeType)
return Dates.year(x) >= 1894 && Dates.month(x) == Dates.Sep && Dates.dayofweekofmonth(x) == 1 && Dates.dayofweek(x) == Dates.Mon
end

function is_thanksgiving_day(x::Date)
function is_thanksgiving_day(x::TimeType)
return Dates.year(x) >= 1871 && Dates.month(x) == Dates.Nov && Dates.dayofweekofmonth(x) == 4 && Dates.dayofweek(x) == Dates.Thu
end

function is_veterans_day(x::Date)
function is_veterans_day(x::TimeType)
return Dates.year(x) >= 1954 && Dates.month(x) == Dates.Nov && Dates.day(x) == 11
end

function is_juneteenth_national_independence_day(x::Date)
function is_juneteenth_national_independence_day(x::TimeType)
return Dates.year(x) >= 2021 && Dates.month(x) == Dates.Jun && Dates.day(x) == 19
end

Expand Down
4 changes: 2 additions & 2 deletions src/holiday.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ struct Holiday
handler::Function
end

function is_holiday(holiday::Holiday, date::Date)
function is_holiday(holiday::Holiday, date::TimeType)
return holiday.handler(date)
end

function Base.in(date::Date, holiday::Holiday)::Bool
function Base.in(date::TimeType, holiday::Holiday)::Bool
return is_holiday(holiday, date)
end

2 comments on commit e089258

@raphasampaio
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/119170

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.3 -m "<description of version>" e0892583b9aadd3578e3e78583a306c14bcc785d
git push origin v0.1.3

Please sign in to comment.