Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Parse date intervals from strings.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 date_interval.gemspec
README.md

DateInterval

Build Status Code Climate

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.

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.

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:

# 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, +monday, +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.

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] #<Date: 2014-01-01>,
#     [1] #<Date: 2014-12-25>
#    ]

Validating expressions

To validation if an expression is valid use the DateInterval.valid? method.

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
Something went wrong with that request. Please try again.