A swifty library for checking whether a given Date is a holiday in a specific country or state. It can also generate a list of holidays in a given year.
Feel free to take a look at SwiftyHolidays Example to get a better feeling of how to use this library.
Xcode Project
Open your project in Xcode, click File > Swift Packages > Add Package Dependency and enter https://github.com/MaxHaertwig/SwiftyHolidays.git
.
Swift Package
Open Package.swift
and add the package to your project's dependencies:
let package = Package(
...
dependencies: [
...
.package(url: "https://github.com/MaxHaertwig/SwiftyHolidays.git", from: "1.0.0")
]
)
LocalDate
The class LocalDate
is used to abstract away the concept of time and time zones. LocalDate
can be converted to Date
and the other way around.
import SwiftyHolidays
let independenceDay = LocalDate(2020, .july, 4)!
>>> independenceDay.isHoliday(in: .unitedStates)
true
>>> independenceDay.isHoliday(in: .germany)
false
let cesarChavezDay = LocalDate(2020, .march, 31)!
>>> cesarChavezDay.isHoliday(in: .unitedStates(state: .california))
true
>>> cesarChavezDay.isHoliday(in: .unitedStates(state: .michigan))
false
let holiday = cesarChavezDay.getHoliday(in: .unitedStates(state: .california))
>>> holiday?.name
"César Chávez Day"
>>> holiday?.date
SwiftyHolidays.LocalDate(year: 2020, month: 3, day: 31)
>>> cesarChavezDay.getHoliday(in: .unitedStates(state: .michigan))
nil
>>> LocalDate.asDate(in: TimeZone(abbreviation: "PST")!)
2020-03-31 07:00:00 +0000
Swift's Date
class can also be used. The related methods require a TimeZone
to interpret the date. If left unspecified, the default time zone for the given country (or state) will be used.
import SwiftyHolidays
let independenceDayUTC = Date(timeIntervalSince1970: 1562198400)
>>> independenceDayUTC.isHoliday(in: .unitedStates(state: .california))
false
>>> independenceDayUTC.isHoliday(in: .unitedStates(state: .california), timeZone: TimeZone(abbreviation: "GMT")!)
true
>>> independenceDayUTC.asLocalDate(in: TimeZone(abbreviation: "GMT")!)
SwiftyHolidays.LocalDate(year: 2019, month: 7, day: 4)
>>> independenceDayUTC.asLocalDate(in: TimeZone(abbreviation: "PST")!)
SwiftyHolidays.LocalDate(year: 2019, month: 7, day: 3)
An instance of Country
or CountryWithState
can be initialized to generate a list of its holidays.
import SwiftyHolidays
>>> Country.germany.allHolidays(in: 2020)
>>> Country.germany.allHolidays(in: 2020..<2030)
>>> CountryWithState.germany(state: .berlin).allHolidays(in: 2020)
- Austria 🇦🇹
- Denmark 🇩🇰
- France 🇫🇷
- Germany 🇩🇪
- Italy 🇮🇹
- Luxembourg 🇱🇺
- Spain 🇪🇸
- Switzerland 🇨🇭
- United States 🇺🇸
You can also call Country.availableCountries
to get a list of supported countries.
Feedback, Issues, and Pull Requests are always welcome.
SwiftyHolidays is released under the MIT license. See LICENSE for more information.