https://github.com/fnando/date_interval
Parse date intervals from strings.
https://github.com/fnando/date_interval
Last synced: 7 months ago
JSON representation
Parse date intervals from strings.
- Host: GitHub
- URL: https://github.com/fnando/date_interval
- Owner: fnando
- License: mit
- Created: 2014-01-30T21:04:56.000Z (almost 12 years ago)
- Default Branch: main
- Last Pushed: 2024-10-26T20:45:47.000Z (about 1 year ago)
- Last Synced: 2025-04-07T03:47:58.263Z (7 months ago)
- Language: Ruby
- Homepage:
- Size: 22.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# DateInterval
[](https://github.com/fnando/date_interval)
[](https://rubygems.org/gems/date_interval)
[](https://rubygems.org/gems/date_interval)
Parse date intervals from strings.
## Installation
Add this line to your application's Gemfile:
gem "date_interval"
And then execute:
$ bundle
Or install it yourself as:
$ gem install date_interval
## Usage
You must always define at least one range at the beginning of the expression.
The snippet below will return three dates objects.
```ruby
require "date_interval"
expr = "2014-01-01 - 2014-01-03"
dates = DateInterval.parse(expr)
```
You can provide as many intervals as you want. The following expression returns
six date objects.
```ruby
require "date_interval"
expr = "2014-01-01 - 2014-01-03, 2014-02-01 - 2014-02-03"
dates = DateInterval.parse(expr)
```
You can also define filters. Filters are applied in sequence, from left to
right. The following filters are available:
- `none`: return no dates. Useful for applying specific filters afterwards.
- `[+-]weekends`: filter weekend dates
- `[+-]weekdays`: filter weekdays
- `[+-]sundays`: filter sundays. You can use any weekday name
(sundays-saturdays)
- `[+-]holidays`: filter holidays. You must add the holidays by yourself (see
below)
- `[+-]yyy-mm-dd`: add/remove the given date.
Beware that duplicated dates are removed from the final result. They're also
sorted.
Some expression examples:
```text
# Don't return weekends
2014-01-01 - 2014-01-05, -weekends
# Return only weekends
2014-01-01 - 2014-01-05, none, +weekends
# Don't return weekdays
2014-01-01 - 2014-01-05, -weekdays
# Return only weekdays
2014-01-01 - 2014-01-05, none, +weekdays
# Return mondays, wednesdays and fridays
2014-01-01 - 2014-01-05, none, +mondays, +wednesdays, +fridays
# Return the specified range, including one more date
2014-01-01 - 2014-01-05, +2014-07-31
# Return the specified range, excluding one date
2014-01-01 - 2014-01-05, -2014-01-05
# Return the specified range, excluding holidays
2014-01-01 - 2014-01-05, -holidays
```
### Defining holidays
To define your holidays you must use the method
`DateInterval::Filter::Holiday.add`. It accepts one or more dates.
```ruby
require "date_interval"
DateInterval::Filter::Holidays.add(
Date.parse("2014-01-01"),
Date.parse("2014-12-25"),
)
DateInterval.parse("2014-01-01 - 2014-12-31, none, +holidays")
# => [
# [0] #,
# [1] #
# ]
```
### Validating expressions
To validate if an expression is valid use the `DateInterval.valid?` method.
```ruby
require "date_interval"
DateInterval.valid?("2014-01-01 - 2014-01-05")
#=> true
DateInterval.valid?("invalid")
#=> false
```
## Contributing
1. Fork it ( http://github.com/fnando/date_interval/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request