workdays 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: 2fe52d73f1f4eef2999c3151a731cd9773924800
4
+ data.tar.gz: 376caf3fe7df1c970d6eb935f244bde249b02b3c
5
+ SHA512:
6
+ metadata.gz: 79a42621e594e248f764ad7646b368e76bcffc2c017830293c7543b3cc6bc5746bfd403bd33342dcb4b88885dde8ab4a48301acc91afceca452d21e260cf46ff
7
+ data.tar.gz: a6d8a0a83a64d60d7a10350532157541ff72d63511165ffc1ba89b01073b7e3f3b3d524911b3e7671bc5c8dfa7baab51f0375e4d42f042484c94d7b6b96d2aa3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Julien Roger
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,61 @@
1
+ # Workdays
2
+
3
+ A hard-working collection of methods to make calculating working dates easier.
4
+
5
+ This gem provides a number of convenience methods to simplify calculating dates while respecting non-working days. Uses the Holiday gem so that holiday schedules can be localized and customized.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "workdays"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install workdays
22
+
23
+ ## Usage
24
+
25
+ By default, Workdays will use `:ca` as the default locale.
26
+
27
+ ```ruby
28
+ Date.new(2017, 2, 8) .workday? # => true
29
+ Date.new(2017, 12, 25).workday? # => false
30
+
31
+ fri_before_family_day = Date.new(2016, 2, 12) # Fri, 12 Feb 2016
32
+ family_day = Date.new(2016, 2, 15) # Mon, 15 Feb 2016
33
+ tues_after_family_day = Date.new(2016, 2, 16) # Tue, 16 Feb 2016
34
+
35
+ family_day.workday?(:ca_on) # => false
36
+ family_day.workday?(:ca_qc) # => true
37
+
38
+ tues_after_family_day.last_workday(:ca_on) # => Fri, 12 Feb 2016
39
+ tues_after_family_day.last_workday(:ca_qc) # => Mon, 15 Feb 2016
40
+ tues_after_family_day.workdays_before(1, :ca_on) # => Fri, 12 Feb 2016
41
+ tues_after_family_day.workdays_before(1, :ca_qc) # => Mon, 15 Feb 2016
42
+ tues_after_family_day.workdays_after(-1, :ca_qc) # => Mon, 15 Feb 2016
43
+
44
+ fri_before_family_day.workdays_after(1, :ca_on) # => Tue, 16 Feb 2016
45
+ fri_before_family_day.workdays_after(1, :ca_qc) # => Mon, 15 Feb 2016
46
+
47
+
48
+ Workdays.workdays_in(fri_before_family_day, family_day, :ca_on) # => 1
49
+ Workdays.workdays_in(fri_before_family_day, family_day, :ca_qc) # => 2
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/julienroger/workdays.
55
+
56
+
57
+ ## License
58
+
59
+ [MIT](./LICENSE)
60
+
61
+ Copyright (c) 2017 Julien Roger
data/bin/console ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'console' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ require "rubygems"
16
+ require "bundler/setup"
17
+
18
+ load Gem.bin_path("workdays", "console")
data/bin/setup ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'setup' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ require "rubygems"
16
+ require "bundler/setup"
17
+
18
+ load Gem.bin_path("workdays", "setup")
data/lib/workdays.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/time"
4
+ require "holidays"
5
+
6
+ require "workdays/version"
7
+ require "workdays/time_extensions"
8
+ require "workdays/core_ext/date"
9
+ require "workdays/core_ext/time"
10
+ require "workdays/core_ext/active_support/time_with_zone"
11
+
12
+ module Workdays
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveSupport::TimeWithZone
4
+ include Workdays::TimeExtensions
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "holidays/core_extensions/date"
4
+
5
+ class Date
6
+ include Holidays::CoreExtensions::Date
7
+ include Workdays::TimeExtensions
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "holidays/core_extensions/time"
4
+
5
+ class Time
6
+ include Holidays::CoreExtensions::Time
7
+ include Workdays::TimeExtensions
8
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This module contains methods to help with dates
4
+ module Workdays
5
+ DEFAULT_LOCALE = [:ca].freeze
6
+
7
+ module TimeExtensions
8
+ def workday?(locale = DEFAULT_LOCALE)
9
+ on_weekday? && !to_date.holiday?(locale, :observed)
10
+ end
11
+
12
+ def last_workday(locale = DEFAULT_LOCALE)
13
+ day = self - 1.day
14
+ day -= 1.day until day.workday?(locale)
15
+ day
16
+ end
17
+
18
+ def to_workday(locale = DEFAULT_LOCALE)
19
+ day = self
20
+ return day if day.workday?(locale)
21
+ day.last_workday(locale)
22
+ end
23
+
24
+ def workdays_after(delta = 0, locale = DEFAULT_LOCALE)
25
+ workdays_before(-delta, locale)
26
+ end
27
+
28
+ def workdays_before(delta = 0, locale = DEFAULT_LOCALE)
29
+ day = self
30
+ days = delta.abs
31
+
32
+ while days.positive? || !day.workday?(locale)
33
+ days -= 1 if day.workday?(locale)
34
+ day -= delta.positive? ? 1.day : -1.days
35
+ end
36
+
37
+ day
38
+ end
39
+ end
40
+
41
+ class << self
42
+ def workdays_in(start_time, end_time, locale = DEFAULT_LOCALE)
43
+ total_days = days_in(start_time, end_time)
44
+ weekends = weekends_in(start_time, end_time)
45
+ holidays = non_weekend_holidays_in(start_time, end_time, locale)
46
+
47
+ total_days - weekends - holidays
48
+ end
49
+
50
+ # Inclusive of start and end dates
51
+ def days_in(start_time, end_time)
52
+ (end_time.to_date - start_time.to_date).to_i + 1
53
+ end
54
+
55
+ private def weekends_in(start_time, end_time)
56
+ (start_time..end_time).map(&:on_weekend?).count(true)
57
+ end
58
+
59
+ private def non_weekend_holidays_in(start_time, end_time, locale = DEFAULT_LOCALE)
60
+ start_date = start_time.to_date
61
+ end_date = end_time.to_date
62
+
63
+ Holidays.between(start_date, end_date, locale, :observed).map do |holiday|
64
+ holiday[:date].on_weekday?
65
+ end.count(true)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Workdays
4
+ VERSION = "0.1.1"
5
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: workdays
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Julien Roger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: holidays
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.49'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.49'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.15'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.15'
111
+ description: Provides a number of convenience methods to simplify calculating dates
112
+ while respecting non-working days. Uses the Holiday gem so that holiday schedules
113
+ can be localized and customized.
114
+ email:
115
+ - jroger@abletribe.com
116
+ executables:
117
+ - console
118
+ - setup
119
+ extensions: []
120
+ extra_rdoc_files: []
121
+ files:
122
+ - LICENSE
123
+ - README.md
124
+ - bin/console
125
+ - bin/setup
126
+ - lib/workdays.rb
127
+ - lib/workdays/core_ext/active_support/time_with_zone.rb
128
+ - lib/workdays/core_ext/date.rb
129
+ - lib/workdays/core_ext/time.rb
130
+ - lib/workdays/time_extensions.rb
131
+ - lib/workdays/version.rb
132
+ homepage: https://github.com/julienroger/workdays
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.6.11
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: A hard-working collection of methods to make calculating working dates easier.
156
+ test_files: []