date_period_parser 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fff42959392de70cbf672c66f173f184875e6ccc
4
+ data.tar.gz: 62a0d643d3969e188eae4f9f6a8c9cf4efdf261c
5
+ SHA512:
6
+ metadata.gz: 0b452fb2fecc484d1c57448afdaaadac74ea963cbc0693a86db999d483a681cb169db44d03b7495b897765784efb0a7812cc3c94832b35896acbe82a676be67b
7
+ data.tar.gz: d9d383b619362e45485f246cdf85d97dd6061be836dc174a2a10ca98b54c3daa72d1da52f3a0e6ed415afe2b88e5e89f09e7e80ec1955ab5c96a42514ede8242
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in date_period_parser.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 hasclass
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # DatePeriodParser
2
+
3
+ Parse a date-like string and returns it's start and end DateTime.
4
+
5
+ This can be used to pass date-periods for URL parameters or query strings, e.g. filtering records in a given period.
6
+
7
+ ```ruby
8
+ class PostsController
9
+ # GET /posts?period=2015-08
10
+ def index
11
+ date_range = DatePeriodParser.range(params["period"])
12
+ @posts = Posts.where(created_at: date_range)
13
+ end
14
+ end
15
+ ```
16
+
17
+ It is *not* meant to be a natural language date parser like the [https://github.com/mojombo/chronic](chronic gem).
18
+
19
+ ## Examples
20
+
21
+ ```ruby
22
+ # year
23
+ DatePeriodParse.parse("2014")
24
+ #=> [<#DateTime 2014-01-01T00:00:00.000Z>, <#DateTime 2014-12-31T23:59:59.999Z>]
25
+
26
+ # months
27
+ DatePeriodParse.parse("2014-02")
28
+ #=> [<#DateTime 2014-02-01T00:00:00.000Z>, <#DateTime 2014-02-28T23:59:59.999Z>]
29
+
30
+ # day
31
+ DatePeriodParse.parse("2014-12-31")
32
+ #=> [<#DateTime 2014-12-31T00:00:00.000Z>, <#DateTime 2014-12-31T23:59:59.999Z>]
33
+
34
+ DatePeriodParse.parse("today")
35
+ #=> [<#DateTime 2015-08-31T00:00:00.000Z>, <#DateTime 2014-08-31T23:59:59.999Z>]
36
+
37
+ # timezone offsets
38
+ DatePeriodParse.parse("2014-12-31", "+0700")
39
+ #=> [<#DateTime 2014-12-31T00:00:00.000+0700>, <#DateTime 2014-12-31T23:59:59.999+0700>]
40
+ ```
41
+
42
+ ## Installation
43
+
44
+ Add this line to your application's Gemfile:
45
+
46
+ ```ruby
47
+ gem 'date_period_parser'
48
+ ```
49
+
50
+ And then execute:
51
+
52
+ $ bundle
53
+
54
+ Or install it yourself as:
55
+
56
+ $ gem install date_period_parser
57
+
58
+ ## Usage
59
+
60
+ See examples above. Currently supported are:
61
+
62
+ * years "2014"
63
+ * months "2014-01"
64
+ * dates "2014-01-01"
65
+ * shorcuts "today"
66
+
67
+ It currently requires the year to have 4 digits.
68
+
69
+ ### DatePeriodParser.parse
70
+
71
+ ```ruby
72
+ from,until = DatePeriodParser.parse("2014")
73
+ from # => #<DateTime 2014-01-01T00:00:00.000+0000")
74
+ until # => #<DateTime 2014-12-31T23:59:59.999+0000")
75
+
76
+ # offsets:
77
+ from,until = DatePeriodParser.parse("2014", "+0700")
78
+ from # => #<DateTime 2014-01-01T00:00:00.000+0700")
79
+ until # => #<DateTime 2014-12-31T23:59:59.999+0700")
80
+
81
+ # invalid periods
82
+ DatePeriodParser.parse("123213")
83
+ # => nil
84
+
85
+ # so you can do:
86
+ from,until = DatePeriodParser.parse("123213")
87
+ from ||= DateTime.yesterday
88
+ until ||= DateTime.now
89
+
90
+ # parse! raises ArgumentError for invalid periods
91
+ from,until = DatePeriodParser.parse("123213")
92
+ #=> ArgumentError
93
+ ```
94
+
95
+ ### DatePeriodParser.range
96
+
97
+ Works the same as DatePeriodParser.parse but returns a range.
98
+
99
+ ```ruby
100
+ rng = DatePeriodParser.range("2014")
101
+ rng.member? DateTime.new(2014,8,6)
102
+
103
+ # invalid periods return nil
104
+ rng = DatePeriodParser.range("dsf89sfd")
105
+ # => nil
106
+
107
+ # range! raises ArgumentError for invalid periods
108
+ rng = DatePeriodParser.range("dsf89sfd")
109
+ #=> ArgumentError
110
+ ```
111
+
112
+ ## Development
113
+
114
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
115
+
116
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
117
+
118
+ ## Contributing
119
+
120
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hasclss/date_period_parser.
121
+
122
+
123
+ ## License
124
+
125
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
126
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "date_period_parser"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'date_period_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "date_period_parser"
8
+ spec.version = DatePeriodParser::VERSION
9
+ spec.authors = ["hasclass"]
10
+ spec.email = ["sebi.burkhard@gmail.com"]
11
+
12
+ spec.summary = %q{Parse a date-like string and returns it's start and end DateTime.}
13
+ spec.description = %q{Parse a date-like string and returns it's start and end DateTime.}
14
+ spec.homepage = "https://github.com/hasclass/date_period_parser"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ spec.test_files = ["spec/date_period_parser_spec.rb"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ end
@@ -0,0 +1,3 @@
1
+ module DatePeriodParser
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,118 @@
1
+ require "date_period_parser/version"
2
+
3
+ require 'date'
4
+
5
+ module DatePeriodParser
6
+ DEFAULT_OFFSET = "+00:00".freeze
7
+
8
+ # ## Useage
9
+ #
10
+ # from,until = DatePeriodParser.parse("2014")
11
+ # from # => #<DateTime 2014-01-01T00:00:00.000+0000")
12
+ # until # => #<DateTime 2014-12-31T23:59:59.999+0000")
13
+ #
14
+ # # offsets:
15
+ # from,until = DatePeriodParser.parse("2014", "+0700")
16
+ # from # => #<DateTime 2014-01-01T00:00:00.000+0700")
17
+ # until # => #<DateTime 2014-12-31T23:59:59.999+0700")
18
+ #
19
+ # # invalid periods
20
+ # DatePeriodParser.parse("123213") # => nil
21
+ # from,until = DatePeriodParser.parse("123213")
22
+ # from # => nil
23
+ # until # => nil
24
+ #
25
+ #
26
+ #
27
+ def parse(str, offset = nil)
28
+ parse!(str, offset)
29
+ rescue ArgumentError => e
30
+ nil
31
+ end
32
+
33
+ def parse!(str, offset = nil)
34
+ Base.new(str, offset).parse
35
+ end
36
+
37
+ def range(str, offset = nil)
38
+ range!(str, offset)
39
+ rescue ArgumentError => e
40
+ nil
41
+ end
42
+
43
+ def range!
44
+ first,last = Base.new(str, offset).parse
45
+ first..last
46
+ end
47
+
48
+ module_function :parse, :parse!, :range, :range!
49
+
50
+ class Base
51
+ attr_reader :value, :offset
52
+
53
+ def initialize(value, offset = nil)
54
+ @value = value.freeze
55
+ @offset = (offset || DEFAULT_OFFSET).freeze
56
+ end
57
+
58
+ def parse
59
+ case @value
60
+ when /\Atoday\Z/ then parse_date(Date.today)
61
+ when /\A\d\d\d\d\Z/ then parse_year
62
+ when /\A\d\d\d\d\-\d\d\Z/ then parse_month
63
+ when /\A\d\d\d\d\-\d\d\-\d\d\Z/ then parse_date
64
+ else raise ArgumentError.new("invalid date period")
65
+ end
66
+ end
67
+
68
+ protected
69
+
70
+ def parse_date(date = nil)
71
+ if date.nil?
72
+ year, month, day = @value.split("-").map(&:to_i)
73
+ else
74
+ year, month, day = date.year, date.month, date.day
75
+ end
76
+
77
+ date = DateTime.new(year, month, day, 0, 0, 0, offset)
78
+
79
+ [
80
+ date,
81
+ end_of_date(date)
82
+ ]
83
+ end
84
+
85
+ def parse_month
86
+ year, month = @value.split("-")
87
+
88
+ first = DateTime.new(year.to_i, month.to_i, 1, 0, 0, 0, offset)
89
+ [
90
+ first,
91
+ last_date_time_of_month(first)
92
+ ]
93
+ end
94
+
95
+ def parse_year
96
+ year = @value
97
+
98
+ first = DateTime.new(year.to_i, 1,1,0,0,0,offset)
99
+ [
100
+ first,
101
+ last_date_time_of_month(first >> 11)
102
+ ]
103
+ end
104
+
105
+ def last_date_time_of_month(dt)
106
+ first_day_this_month = DateTime.new(dt.year, dt.month, 1, 0, 0, 0, offset)
107
+ first_day_next_month = first_day_this_month >> 1
108
+ last_day_this_month = first_day_next_month - 1
109
+
110
+ d = last_day_this_month
111
+ end_of_date(d)
112
+ end
113
+
114
+ def end_of_date(dt)
115
+ DateTime.new(dt.year, dt.month, dt.day, 23, 59, 59.999, offset)
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,96 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require "minitest/autorun"
3
+ require 'date_period_parser'
4
+
5
+ describe DatePeriodParser do
6
+ def parse(str, offset = nil)
7
+ DatePeriodParser.parse(str, offset)
8
+ end
9
+
10
+ it '2014' do
11
+ assert_equal DateTime.parse("2014-01-01T00:00:00.000+0000"), parse("2014").first
12
+ assert_equal DateTime.parse("2014-12-31T23:59:59.999+0000"), parse("2014").last
13
+ end
14
+
15
+ it '2014-01' do
16
+ assert_equal DateTime.parse("2014-01-01T00:00:00.000+0000"), parse("2014-01").first
17
+ assert_equal DateTime.parse("2014-01-31T23:59:59.999+0000"), parse("2014-01").last
18
+ end
19
+
20
+ it '2014-02' do
21
+ assert_equal DateTime.parse("2014-02-01T00:00:00.000+0000"), parse("2014-02").first
22
+ assert_equal DateTime.parse("2014-02-28T23:59:59.999+0000"), parse("2014-02").last
23
+
24
+ assert_equal DateTime.parse("2012-02-01T00:00:00.000+0000"), parse("2012-02").first
25
+ assert_equal DateTime.parse("2012-02-29T23:59:59.999+0000"), parse("2012-02").last
26
+ end
27
+
28
+ it '2014-12' do
29
+ assert_equal DateTime.parse("2014-12-01T00:00:00.000+0000"), parse("2014-12").first
30
+ assert_equal DateTime.parse("2014-12-31T23:59:59.999+0000"), parse("2014-12").last
31
+ end
32
+
33
+ it '2014-08' do
34
+ assert_equal DateTime.parse("2014-08-01T00:00:00.000+0000"), parse("2014-08").first
35
+ assert_equal DateTime.parse("2014-08-31T23:59:59.999+0000"), parse("2014-08").last
36
+ end
37
+
38
+ it '2014-01-01' do
39
+ assert_equal DateTime.parse("2014-01-01T00:00:00.000+0000"), parse("2014-01-01").first
40
+ assert_equal DateTime.parse("2014-01-01T23:59:59.999+0000"), parse("2014-01-01").last
41
+ end
42
+
43
+ it '2014-12-31' do
44
+ assert_equal DateTime.parse("2014-12-31T00:00:00.000+0000"), parse("2014-12-31").first
45
+ assert_equal DateTime.parse("2014-12-31T23:59:59.999+0000"), parse("2014-12-31").last
46
+ end
47
+
48
+ it 'today' do
49
+ today = Date.today
50
+ assert_equal DateTime.parse("#{today}T00:00:00.000+0000"), parse("today").first
51
+ assert_equal DateTime.parse("#{today}T23:59:59.999+0000"), parse("today").last
52
+ end
53
+
54
+ describe "with offsets" do
55
+ it '2014' do
56
+ assert_equal DateTime.parse("2014-01-01T00:00:00.000+0700"), parse("2014", "+0700").first
57
+ assert_equal DateTime.parse("2014-12-31T23:59:59.999+0700"), parse("2014", "+0700").last
58
+ end
59
+
60
+ it '2014-01' do
61
+ assert_equal DateTime.parse("2014-01-01T00:00:00.000+0700"), parse("2014-01", "+7").first
62
+ assert_equal DateTime.parse("2014-01-31T23:59:59.999+0700"), parse("2014-01", "+7").last
63
+ end
64
+
65
+ it '2014-01-01' do
66
+ assert_equal DateTime.parse("2014-01-01T00:00:00.000+0700"), parse("2014-01-01", "+7").first
67
+ assert_equal DateTime.parse("2014-01-01T23:59:59.999+0700"), parse("2014-01-01", "+7").last
68
+ end
69
+
70
+ it 'today' do
71
+ today = Date.today
72
+ assert_equal DateTime.parse("#{today}T00:00:00.000+0700"), parse("today", "+7").first
73
+ assert_equal DateTime.parse("#{today}T23:59:59.999+0700"), parse("today", "+7").last
74
+ end
75
+ end
76
+
77
+ it 'invalid pattern 2014-01-01-01' do
78
+ from, to = parse("2014-01-01-01")
79
+ assert_equal nil, from
80
+ assert_equal nil, to
81
+ # it is actually nil and not [nil, nil]
82
+ assert_equal nil, parse("2014-01-01-01")
83
+ end
84
+
85
+ it 'invalid date 2014-13-01' do
86
+ assert_raises(ArgumentError) do
87
+ assert_equal DateTime.parse("2014-01-01T00:00:00.000+0000"), DatePeriodParser.parse!("2014-13-01").first
88
+ end
89
+ assert_raises(ArgumentError) do
90
+ assert_equal DateTime.parse("2014-01-01T00:00:00.000+0000"), DatePeriodParser.parse!("2014-12-41").first
91
+ end
92
+ assert_raises(ArgumentError) do
93
+ assert_equal DateTime.parse("2014-01-01T00:00:00.000+0000"), DatePeriodParser.parse!("2014-12-41", "+2400").first
94
+ end
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_period_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - hasclass
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Parse a date-like string and returns it's start and end DateTime.
42
+ email:
43
+ - sebi.burkhard@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - date_period_parser.gemspec
57
+ - lib/date_period_parser.rb
58
+ - lib/date_period_parser/version.rb
59
+ - spec/date_period_parser_spec.rb
60
+ homepage: https://github.com/hasclass/date_period_parser
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ allowed_push_host: https://rubygems.org
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.5
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Parse a date-like string and returns it's start and end DateTime.
85
+ test_files:
86
+ - spec/date_period_parser_spec.rb