cron_timezone_converter 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 284c13e72ab3f5242242234bc508a1dd64c51c6e3a3b6899b2056589de23f652
4
+ data.tar.gz: 758794ca324458ebcbfe0f05a6d3b93f305e128db054e77a37f5f0cd42c0ad19
5
+ SHA512:
6
+ metadata.gz: bff1570cd823c804219d23686df8ceb69340c519b13b880dc1ea1117324617e58204ed4e7f1bf3f44424d35cda033094a0d43694297e8e7c7da75216aafd2a92
7
+ data.tar.gz: 7705a0f769267bb3d7978a6d5a7f0fa9fa03b162fc0e5409a279df98e64c4ca0f5274684a18d1f047b38440e6b083a1fc28541b5bfe5fc19b20ba07efbe85ca4
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.3
7
+ before_install: gem install bundler -v 1.16.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at aemadrid@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cron_zone_converter.gemspec
6
+ gemspec
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cron_zone_converter (0.1.1)
5
+ activesupport
6
+ fugit
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (5.2.2)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ concurrent-ruby (1.1.4)
17
+ diff-lcs (1.3)
18
+ et-orbi (1.1.7)
19
+ tzinfo
20
+ fugit (1.1.8)
21
+ et-orbi (~> 1.1, >= 1.1.7)
22
+ raabro (~> 1.1)
23
+ i18n (1.5.3)
24
+ concurrent-ruby (~> 1.0)
25
+ minitest (5.11.3)
26
+ raabro (1.1.6)
27
+ rake (10.5.0)
28
+ rspec (3.8.0)
29
+ rspec-core (~> 3.8.0)
30
+ rspec-expectations (~> 3.8.0)
31
+ rspec-mocks (~> 3.8.0)
32
+ rspec-core (3.8.0)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-expectations (3.8.2)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.8.0)
37
+ rspec-mocks (3.8.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-support (3.8.0)
41
+ thread_safe (0.3.6)
42
+ timecop (0.9.1)
43
+ tzinfo (1.2.5)
44
+ thread_safe (~> 0.1)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 1.16)
51
+ cron_zone_converter!
52
+ rake (~> 10.0)
53
+ rspec (~> 3.0)
54
+ timecop
55
+
56
+ BUNDLED WITH
57
+ 1.16.6
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Adrian Esteban Madrid
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.
@@ -0,0 +1,149 @@
1
+ # This gem is a full fork of another, just for make it availavle.
2
+
3
+ # CronZoneConverter
4
+
5
+ ## Translate local Cron lines into any time zone
6
+
7
+ The objective of this gem is to translate a single cron line in a time zone into
8
+ a single/multiple cron line/s into any time zone (defaults to UTC).
9
+
10
+ ## Usage
11
+
12
+ ### Cron Line
13
+
14
+ The first (required) parameter to the `convert` method is the cron line:
15
+
16
+ ```ruby
17
+ CronZoneConverter.convert '0 16 * * 1-5 MST'
18
+ # => ["0 23 * * 1,2,3,4,5"]
19
+ ```
20
+
21
+ We use [Fugit::Cron](https://github.com/floraison/fugit) to parse the cron line.
22
+ `Fugit` does allow parsing the time zone from the cron line like so:
23
+
24
+ ```ruby
25
+ CronZoneConverter.convert '0 16 * * 1-5 MST'
26
+ # => ["0 23 * * 1,2,3,4,5"]
27
+ ```
28
+
29
+ But if you don't pass or setup a valid time zone you will get an error:
30
+
31
+ ```ruby
32
+ CronZoneConverter.convert '0 16,20 * * 1-5'
33
+ CronZoneConverter
34
+ ```
35
+
36
+ ### Local Time Zone
37
+
38
+ The second parameter is the local cron line time zone.
39
+ This gem uses [ActiveSupport](https://github.com/rails/rails/tree/master/activesupport)
40
+ to identify available time zones. So you can setup the time zone by:
41
+
42
+ * Passing a `zone` parameter: a `String` (zone name) or and `ActiveSupport::TimeZone`:
43
+
44
+ ```ruby
45
+ CronZoneConverter.convert '0 16 * * 1-5', 'MST'
46
+ # => ["0 23 * * 1,2,3,4,5"]
47
+ CronZoneConverter.convert '0 16 * * 1-5', Time.find_zone('MST')
48
+ # => ["0 23 * * 1,2,3,4,5"]
49
+ ```
50
+
51
+ * A timezone defined in the cron line like we saw before:
52
+
53
+ ```ruby
54
+ CronZoneConverter.convert '0 16,20 * * 1-5 MST'
55
+ ```
56
+
57
+ * Or defining a global time zone:
58
+
59
+ ```ruby
60
+ Time.zone = 'MST'
61
+ CronZoneConverter.convert '0 16 * * 1-5'
62
+ ```
63
+
64
+ ### Remote Time Zone
65
+
66
+ The third parameter is the remote time zone.
67
+ You can setup the time zone by:
68
+
69
+ * Passing a `zone` parameter: a `String` (zone name) or and `ActiveSupport::TimeZone`:
70
+
71
+ ```ruby
72
+ CronZoneConverter.convert '0 16 * * 1-5', 'MST', 'EST'
73
+ # => ["0 18 * * 1,2,3,4,5"]
74
+ CronZoneConverter.convert '0 16 * * 1-5', Time.find_zone('EST')
75
+ # => ["0 18 * * 1,2,3,4,5"]
76
+ ```
77
+
78
+ * Or **use the default (`'UTC'`)**:
79
+
80
+ ```ruby
81
+ CronZoneConverter.convert '0 16 * * 1-5', 'MST'
82
+ # => ["0 23 * * 1,2,3,4,5"]
83
+ ```
84
+
85
+ ## Result
86
+
87
+ We always return an array with one or more cron lines (`String`).
88
+
89
+ ### Why an array? Why multiple lines?
90
+
91
+ Given that you could have multiple hours defined in a cron line when you convert that into some other time zone
92
+ you could end up with some of the hours falling in one day and some other hours falling in another day.
93
+
94
+ #### Single line
95
+
96
+ ```ruby
97
+ CronZoneConverter.convert '0 16,20 * * 1-5 MST'
98
+ # => ['0 23 * * 1,2,3,4,5', '0 3 * * 2,3,4,5,6']
99
+ ```
100
+
101
+ #### Multiple lines
102
+
103
+ ```ruby
104
+ CronZoneConverter.convert '0 20,21 * * 1-5 MST'
105
+ # => ['0 3,4 * * 2,3,4,5,6']
106
+ ```
107
+
108
+ ## Origin Story
109
+
110
+ The need for this functionality came about by our continued use of
111
+ [Sidekiq Enterprise Periodic Jobs](https://github.com/mperham/sidekiq/wiki/Ent-Periodic-Jobs#time-zones)
112
+ and issues we ran with running jobs at our local time instead of the servers UTC time.
113
+
114
+ This gem does consider DST (Daylight Saving Time) but please remember that you will need to restart your servers
115
+ for the time change to kick in for your Sidekiq workers.
116
+
117
+ ## Installation
118
+
119
+ Add this line to your application's Gemfile:
120
+
121
+ ```ruby
122
+ gem 'cron_zone_converter'
123
+ ```
124
+
125
+ And then execute:
126
+
127
+ $ bundle
128
+
129
+ Or install it yourself as:
130
+
131
+ $ gem install cron_zone_converter
132
+
133
+ ## Development
134
+
135
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
136
+
137
+ 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).
138
+
139
+ ## Contributing
140
+
141
+ Bug reports and pull requests are welcome on GitHub at https://github.com/acima-credit/cron_zone_converter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
142
+
143
+ ## License
144
+
145
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
146
+
147
+ ## Code of Conduct
148
+
149
+ Everyone interacting in the CronZoneConverter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/acima-credit/cron_zone_converter/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'cron_zone_converter'
6
+ require 'timecop'
7
+
8
+ require 'irb'
9
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'cron_zone_converter/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'cron_timezone_converter'
9
+ spec.version = CronZoneConverter::VERSION
10
+ spec.authors = ['Adrian Esteban Madrid', 'Max Honcharov']
11
+ spec.email = ['adrian.madrid@acimacredit.com', 'mxgoncharov@gmail.com']
12
+
13
+ spec.summary = 'Convert a cron line in any time zone into any time zone'
14
+ spec.description = spec.summary
15
+ spec.homepage = 'http://github.com/mxhoncharov/cron_zone_converter'
16
+ spec.license = 'MIT'
17
+
18
+ if spec.respond_to?(:metadata)
19
+
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = 'http://github.com/mxhoncharov/cron_zone_converter'
22
+ else
23
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
24
+ 'public gem pushes.'
25
+ end
26
+
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_dependency 'activesupport'
35
+ spec.add_dependency 'fugit'
36
+
37
+ spec.add_development_dependency 'bundler', '~> 1.16'
38
+ spec.add_development_dependency 'rake', '~> 10.0'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ spec.add_development_dependency 'timecop'
41
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/time'
4
+ require 'fugit'
5
+
6
+ require 'cron_zone_converter/version'
7
+ require 'cron_zone_converter/error'
8
+ require 'cron_zone_converter/duplicator'
9
+ require 'cron_zone_converter/merger'
10
+ require 'cron_zone_converter/converter'
11
+
12
+ module CronZoneConverter
13
+ def self.convert(line, local_zone = nil, remote_zone = nil)
14
+ Converter.new(line, local_zone, remote_zone).convert
15
+ end
16
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/time'
4
+ require 'fugit'
5
+
6
+ module CronZoneConverter
7
+ class Converter
8
+ def initialize(line, local_zone = nil, remote_zone = nil)
9
+ @base = build_base line
10
+ @local_zone = build_local_zone local_zone
11
+ @remote_zone = build_remote_zone remote_zone
12
+ @offset = build_offset
13
+ @crons = []
14
+ end
15
+
16
+ def convert
17
+ return [base.original] unless needs_changes?
18
+
19
+ split_base
20
+ apply_offset
21
+ merge_similar
22
+ final_lines
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :base, :local_zone, :remote_zone, :offset, :crons
28
+
29
+ def build_base(line)
30
+ ::Fugit::Cron.new(line).tap { |x| raise Error, 'invalid cron line' if x.nil? }
31
+ rescue StandardError => e
32
+ raise Error.new('invalid cron line', e)
33
+ end
34
+
35
+ def build_local_zone(value)
36
+ return value if value.is_a?(ActiveSupport::TimeZone)
37
+
38
+ build_string_zone(value) || build_other_zone
39
+ end
40
+
41
+ def build_remote_zone(value)
42
+ return value if value.is_a?(ActiveSupport::TimeZone)
43
+
44
+ build_string_zone(value) || Time.find_zone('UTC')
45
+ end
46
+
47
+ def build_string_zone(value)
48
+ return false unless value.is_a? String
49
+
50
+ ::Time.find_zone(value).tap { |x| raise Error, 'invalid zone' if x.nil? }
51
+ end
52
+
53
+ def build_other_zone
54
+ return ::Time.find_zone(base.timezone.name) if base.timezone
55
+ return Time.zone if Time.zone
56
+
57
+ raise Error, 'missing zone'
58
+ end
59
+
60
+ def build_offset
61
+ # local_zone.now.utc_offset.to_f / 3600
62
+ (local_zone.now.utc_offset.to_f - remote_zone.now.utc_offset.to_f) / 3600
63
+ end
64
+
65
+ def needs_changes?
66
+ return false if local_zone.name == remote_zone.name
67
+ return false if base.hours.nil?
68
+ return false if base.original.split(' ')[1] =~ %r{^\*/\d{1,2}$}
69
+
70
+ true
71
+ end
72
+
73
+ def split_base
74
+ @crons = base.hours.map { |x| Duplicator.change base, hour: x }
75
+ @crons = base.monthdays.map { |x| crons.map { |y| Duplicator.change y, dom: x } }.flatten unless base.monthdays.nil?
76
+ @crons = base.weekdays.map { |x| crons.map { |y| Duplicator.change y, dow: x } }.flatten unless base.weekdays.nil?
77
+ end
78
+
79
+ def apply_offset
80
+ @crons.map! do |cron|
81
+ day_diff = nil
82
+ hour = (cron.hours.first - offset).to_i
83
+
84
+ if hour < 0
85
+ day_diff = -1
86
+ hour = 24 + hour
87
+ elsif hour > 23
88
+ day_diff = 1
89
+ hour -= 24
90
+ end
91
+
92
+ Duplicator.change cron, hour: hour, day_diff: day_diff
93
+ end
94
+ end
95
+
96
+ def merge_similar
97
+ @crons = Merger.merge crons
98
+ end
99
+
100
+ def final_lines
101
+ @crons.map(&:to_cron_s)
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CronZoneConverter
4
+ class Duplicator
5
+ def self.change(base, changes = {})
6
+ new(base, changes).change
7
+ end
8
+
9
+ def initialize(base, changes = {})
10
+ @base = ::Fugit::Cron.new base
11
+ @changes = changes
12
+ @parts = @base.to_h
13
+ end
14
+
15
+ def change
16
+ return base.dup if changes.empty?
17
+
18
+ update_hours
19
+ update_weekdays
20
+ update_monthdays
21
+
22
+ ::Fugit::Cron.new line
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :base, :parts, :changes
28
+
29
+ def update_hours
30
+ parts[:hours] = [changes[:hour]] if changes[:hour]
31
+ parts[:hours] = changes[:hours] if changes[:hours]
32
+ parts[:hours] = parts[:hours] + [changes[:add_hour]] if changes[:add_hour]
33
+ end
34
+
35
+ def update_weekdays
36
+ parts[:weekdays] = [[changes[:dow]]] if changes[:dow]
37
+ parts[:weekdays] = parts[:weekdays] + [[changes[:add_dow]]] if changes[:add_dow]
38
+ return unless changes[:day_diff]
39
+
40
+ parts[:weekdays]&.map! do |x|
41
+ v = x.first + changes[:day_diff]
42
+ v -= 6 if v > 6
43
+ v = 6 + v if v < 0
44
+ [v]
45
+ end
46
+ end
47
+
48
+ def update_monthdays
49
+ parts[:monthdays] = [[changes[:dom]]] if changes[:dom]
50
+ parts[:monthdays] = (parts[:monthdays] || []) + [changes[:add_dom]] if changes[:add_dom]
51
+ return unless changes[:day_diff]
52
+
53
+ parts[:monthdays]&.map! do |x|
54
+ v = x + changes[:day_diff]
55
+ raise Error, 'cannot change month' if v < 0
56
+
57
+ v = 31 if v > 31
58
+ v
59
+ end
60
+ end
61
+
62
+ def line
63
+ [
64
+ parts[:seconds] == [0] ? nil : (parts[:seconds] || ['*']).join(','),
65
+ (parts[:minutes] || ['*']).join(','),
66
+ (parts[:hours] || ['*']).join(','),
67
+ (parts[:monthdays] || ['*']).join(','),
68
+ (parts[:months] || ['*']).join(','),
69
+ (parts[:weekdays] || [['*']]).map { |d| d.compact.join('#') }.join(','),
70
+ base.zone
71
+ ].compact.join(' ')
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CronZoneConverter
4
+ class Error < ::RuntimeError
5
+ attr_reader :original
6
+ def initialize(message, original = nil)
7
+ super(message)
8
+ @original = original
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CronZoneConverter
4
+ class Merger
5
+ class << self
6
+ def merge(crons)
7
+ return crons unless crons.size > 1
8
+
9
+ merge_by_hour(merge_by_dow(merge_by_dom(crons))).sort{ |a, b| a.to_cron_s <=> b.to_cron_s }
10
+ end
11
+
12
+ private
13
+
14
+ def merge_by_hour(ary)
15
+ ary.each_with_object({}) do |cron, merged|
16
+ marker = cron_marker cron, :hour
17
+ merged[marker] = if merged.key?(marker)
18
+ Duplicator.change merged[marker], add_hour: cron.hours.first
19
+ else
20
+ cron
21
+ end
22
+ end.values
23
+ end
24
+
25
+ def merge_by_dom(ary)
26
+ ary.each_with_object({}) do |cron, merged|
27
+ marker = cron_marker cron, :dom
28
+ merged[marker] = if merged.key?(marker)
29
+ Duplicator.change merged[marker], add_dom: cron.monthdays.first
30
+ else
31
+ cron
32
+ end
33
+ end.values
34
+ end
35
+
36
+ def merge_by_dow(ary)
37
+ ary.each_with_object({}) do |cron, merged|
38
+ marker = cron_marker cron, :dow
39
+ merged[marker] = if merged.key?(marker)
40
+ Duplicator.change merged[marker], add_dow: cron.weekdays.first.first
41
+ else
42
+ cron
43
+ end
44
+ end.values
45
+ end
46
+
47
+ def cron_marker(cron, exclude_key)
48
+ keys = %i[minute hour dom month dow]
49
+ marks = []
50
+ cron.to_cron_s.split(' ').each_with_index do |x, i|
51
+ next if keys[i] == exclude_key
52
+
53
+ marks << x
54
+ end
55
+ marks.join(' ')
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CronZoneConverter
4
+ VERSION = '0.1.2'
5
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cron_timezone_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Esteban Madrid
8
+ - Max Honcharov
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2020-03-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: fugit
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.16'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.16'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: timecop
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: Convert a cron line in any time zone into any time zone
99
+ email:
100
+ - adrian.madrid@acimacredit.com
101
+ - mxgoncharov@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - cron_to_utc.gemspec
118
+ - lib/cron_zone_converter.rb
119
+ - lib/cron_zone_converter/converter.rb
120
+ - lib/cron_zone_converter/duplicator.rb
121
+ - lib/cron_zone_converter/error.rb
122
+ - lib/cron_zone_converter/merger.rb
123
+ - lib/cron_zone_converter/version.rb
124
+ homepage: http://github.com/mxhoncharov/cron_zone_converter
125
+ licenses:
126
+ - MIT
127
+ metadata:
128
+ homepage_uri: http://github.com/mxhoncharov/cron_zone_converter
129
+ source_code_uri: http://github.com/mxhoncharov/cron_zone_converter
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubygems_version: 3.0.6
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Convert a cron line in any time zone into any time zone
149
+ test_files: []