Usage | holidays-rust | Spatie      

 SPATIE  

holidays-rust
=============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Holidays-rust](https://spatie.be/docs/holidays-rust/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/holidays-rust/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/holidays-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/holidays-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/holidays-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/holidays-rust/v1/usage)
- [ Coverage and compatibility ](https://spatie.be/docs/holidays-rust/v1/compatibility)
- [ Adding a country ](https://spatie.be/docs/holidays-rust/v1/adding-a-country)
- [ Testing ](https://spatie.be/docs/holidays-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/holidays-rust/v1/changelog)

 Usage
=====

You can get all holidays for a country by using the `get` method. It returns a vector of `Holiday` values.

```
let holidays = Holidays::for_current_year("be")?.get()?;

for holiday in holidays {
    println!("{}: {} ({:?})", holiday.date, holiday.name, holiday.holiday_type);
}
```

Pass an [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) code to select a country. For Greece, use `gr`. The EU abbreviation `el` remains supported as an alias, and both codes are case-insensitive. `Holidays::countries()` returns all registered codes.

`Holiday` implements Serde's `Serialize` and `Deserialize`:

```
let json = serde_json::to_string(&holidays[0])?;
// {"name":"Nieuwjaar","date":"2024-01-01","type":"national","region":null}
```

### Getting holidays for a specific year

```
let holidays = Holidays::for_country("be", 2024)?.get()?;
```

Years use the Gregorian calendar. Each country retains its PHP supported-year limits; unavailable data returns an error rather than an incomplete substitute calendar.

### Getting holidays between two dates

The `get_in_range` method returns all holidays between two dates (inclusive). Dates are swappable; the lower date is always used as the start. Use `get_in_range_str` for the PHP shorthand formats:

```
let holidays = Holidays::for_country("be", 2024)?
    .get_in_range_str("2024-01-01", "2024-12-31")?;

let calendar = Holidays::for_country("be", 2024)?;
let full_years = calendar.get_in_range_str("2024", "2025")?;
let year_month = calendar.get_in_range_str("2024-06", "2025-05")?;
```

`get_in_range(from, to)` accepts `Date` values. A range can cross years, independently of the year used to construct the calendar. Partial starts expand to the first day; partial ends expand to the last day, before reversed endpoints are swapped.

### Getting holidays in a specific language

```
let holidays = Holidays::for_country("be", 2024)?.locale("fr").get()?;
```

If no translation file exists for the given locale, the original holiday names are returned. Omitting the locale preserves the country's default locale, where the PHP package defines one. All 50 original translation files are included.

### Regional holidays

Some countries have region-specific holidays. You can pass a region code to the `region` method:

```
let holidays = Holidays::for_country("de", 2024)?.region("DE-BW")?.get()?;
```

To discover which regions a country supports:

```
let regions = Holidays::for_country("de", 2024)?.regions();
// ["DE-BW", "DE-BY", "DE-BE", ...]
```

Portugal uses municipal holidays. Its regions are the municipalities, identified by `PT-` followed by their DICO code (distrito + concelho) as published by the INE:

```
let holidays = Holidays::for_country("pt", 2024)?.region("PT-1106")?.get()?;
// Lisboa
```

All 308 municipalities are supported. These are municipal DICO codes, not ISO 3166-2 district codes. Without a region, only national holidays are returned. Holidays applying to the whole autonomous region of Madeira or the Azores are not currently included.

Countries that support regions: Australia, Bosnia and Herzegovina, Brazil, France, Germany, Malaysia, Portugal, Spain, Switzerland. Region codes are case-sensitive, as in PHP. Countries without regional calendars ignore a supplied region. Malaysia without a region includes and deduplicates all its regional holidays, matching PHP.

### Determining if a date is a holiday

```
let calendar = Holidays::for_country("be", 2024)?;
assert!(calendar.is_holiday(date)?);
```

### Determining if today is a holiday

```
let is_holiday = Holidays::for_current_year("be")?.is_today_holiday()?;
```

Today and the current year use the system's local timezone.

### Getting upcoming holidays

```
let holidays = Holidays::for_current_year("be")?.get_upcoming(3)?;

for holiday in holidays {
    println!("{}: {}", holiday.date, holiday.name);
}
```

This returns the next three holidays, including today if it is a holiday. `get_upcoming_from(date, count)` uses an explicit starting date. Both operations can cross years and ignore the configured year, as PHP does. A count of zero returns an empty vector.

### Getting long weekends

```
let calendar = Holidays::for_country("de", 2024)?;
let long_weekends = calendar.get_long_weekends(4)?;
let shorter_weekends = calendar.get_long_weekends(3)?;

for weekend in long_weekends {
    println!("{} to {}: {} days", weekend.start_date, weekend.end_date, weekend.day_count);
    println!("{:?}", weekend.holidays);
}
```

This preserves PHP's grouping algorithm. It groups successive holidays separated by no more than the supplied minimum days, then keeps groups with at least two holidays whose inclusive span meets that minimum. It does not calculate which weekends or working days require time off.

### Getting the name of a holiday

```
let name = Holidays::for_country("be", 2024)?.get_name(date)?;
assert_eq!(name.as_deref(), Some("Nieuwjaar"));
```

Date lookup uses the date's own year, independently of the configured year. An ordinary non-holiday returns `None`, not an error.

### Determining whether a country is supported

```
assert!(Holidays::has("be"));
assert!(!Holidays::has("unknown"));
```

### Holiday types

The holiday types are `National`, `Regional`, `Observed`, `Religious` and `Bank`:

```
let holidays = Holidays::for_country("de", 2024)?
    .region("DE-BW")?
    .get_by_type(HolidayType::Regional)?;
```

### Errors and Rust API differences

Configuration uses consuming builders. Keep the returned `Holidays` value or chain the calls. Unknown countries and invalid regions return typed errors. Supported-year checks happen when holidays are calculated, since different countries have different limits. Date inputs are typed `Date` values; string range endpoints accept ISO year, month or date formats rather than Carbon's natural-language parser.

PHP optional arguments map to explicit Rust operations: `for_current_year` or `for_country`, `get` or `get_by_type`, and `get_upcoming` or `get_upcoming_from`. `get_long_weekends` takes its minimum explicitly; pass `4` for PHP's default. `Holiday` fields are owned values rather than shared mutable objects. Custom PHP country subclasses and runtime registry mutation are not Rust extension mechanisms; see [adding a country](adding-a-country) for repository contributions.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
