timing 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd0774a3a4df5f473172bb742f667b388625bb73
4
+ data.tar.gz: c9c190cd180b5b82f8dab5bc7af91ee192380a10
5
+ SHA512:
6
+ metadata.gz: c16f06a4f2ed359760f0c626e5805b75676e45b42a17895d05dc1a88ca6097a19e7bbac56909f75060e944ee9b794e5af8b06a2e0c63a998c89a0898d193e4a0
7
+ data.tar.gz: c03937acac594c5a4aa71f03f7eed0148481e69ad18739fc5966e29d6d30a86a4ec18a33a38e0f53306ab54a11f6933e2d035ce3c1aa7bc3cefe1aa3b02dd1e7
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token: pKGpUYmgvOL0y88aDpXbysqylu39yTgOb
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/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ timing
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby 2.0.0
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+ - jruby
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in timing.gemspec
4
+ gemspec
5
+
6
+ gem 'coveralls', require: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Gabriel Naiman
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,40 @@
1
+ # Timing
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/timing.png)](https://rubygems.org/gems/timing)
4
+ [![Build Status](https://travis-ci.org/gabynaiman/timing.png?branch=master)](https://travis-ci.org/gabynaiman/timing)
5
+ [![Coverage Status](https://coveralls.io/repos/gabynaiman/timing/badge.png?branch=master)](https://coveralls.io/r/gabynaiman/timing?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/gabynaiman/timing.png)](https://codeclimate.com/github/gabynaiman/timing)
7
+ [![Dependency Status](https://gemnasium.com/gabynaiman/timing.png)](https://gemnasium.com/gabynaiman/timing)
8
+
9
+ Time utils
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'timing'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install timing
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/timing.
35
+
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
40
+
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:spec) do |t|
5
+ t.libs << 'spec'
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ t.verbose = false
8
+ end
9
+
10
+ task default: :spec
11
+
12
+ desc 'Pry console'
13
+ task :console do
14
+ require 'timing'
15
+ require 'pry'
16
+ ARGV.clear
17
+ Pry.start
18
+ end
@@ -0,0 +1,16 @@
1
+ module Timing
2
+ module Helpers
3
+
4
+ TIME_REGEXP = /(\d\d:\d\d:\d\d)/
5
+ TIME_FORMAT = '%F %T %z'
6
+
7
+ def beginning_of_day(time)
8
+ TimeInZone.parse time.strftime(TIME_FORMAT).sub(TIME_REGEXP, '00:00:00')
9
+ end
10
+
11
+ def end_of_day(time)
12
+ TimeInZone.parse time.strftime(TIME_FORMAT).sub(TIME_REGEXP, '23:59:59')
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,71 @@
1
+ module Timing
2
+ class Interval < SimpleDelegator
3
+
4
+ UNITS_NAMES = {
5
+ s: :seconds,
6
+ m: :minutes,
7
+ h: :hours,
8
+ d: :days,
9
+ w: :weeks
10
+ }
11
+
12
+ CONVERSIONS = {
13
+ s: 1,
14
+ m: 60,
15
+ h: 60 * 60,
16
+ d: 60 * 60 * 24,
17
+ w: 60 * 60 * 24 * 7
18
+ }
19
+
20
+ REGEXP = /^([\d\.]+)([smhdw])$/
21
+
22
+ def initialize(seconds)
23
+ raise ArgumentError, "#{seconds} is not a number" unless seconds.is_a? Numeric
24
+ super seconds.abs
25
+ end
26
+
27
+ CONVERSIONS.each do |unit, factor|
28
+ unit_name = UNITS_NAMES[unit]
29
+
30
+ define_method "to_#{unit_name}" do
31
+ to_f / factor
32
+ end
33
+
34
+ define_singleton_method unit_name do |number|
35
+ new number * factor
36
+ end
37
+ end
38
+
39
+ def begin_of(time)
40
+ normalized_time = time + time.utc_offset
41
+ gap = normalized_time.to_i % self
42
+ normalized_time - gap - time.utc_offset
43
+ end
44
+
45
+ def end_of(time)
46
+ begin_of(time) + self - 1
47
+ end
48
+
49
+ def to_s
50
+ integer = CONVERSIONS.map { |u,f| [to_f / f, "#{to_i / f}#{u}"] }
51
+ .sort_by { |v,t| v }
52
+ .detect { |v,t| v == v.to_i }
53
+ integer ? integer[1] : "#{to_seconds}s"
54
+ end
55
+
56
+ def inspect
57
+ "#{to_s} (#{to_seconds})"
58
+ end
59
+
60
+ def self.parse(expression)
61
+ match = REGEXP.match expression.strip
62
+ raise "Invalid interval expression #{expression}" unless match
63
+ new match.captures[0].to_f * CONVERSIONS[match.captures[1].to_sym]
64
+ end
65
+
66
+ def self.between(time_1, time_2)
67
+ new (time_1 - time_2).round
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,91 @@
1
+ module Timing
2
+ class TimeInZone
3
+
4
+ extend Forwardable
5
+
6
+ REGEXP = /[+-]\d\d:?\d\d/
7
+
8
+ def_delegators :time, :to_i, :to_f, :<, :<=, :==, :>, :>=, :between?, :eql?, :hash
9
+ def_delegators :time_with_offset, :year, :month, :day, :hour, :min, :sec
10
+
11
+ attr_reader :zone_offset
12
+
13
+ def initialize(time, zone_offset=nil)
14
+ @time = time
15
+ @zone_offset = build_zone_offset(zone_offset || time.utc_offset)
16
+ end
17
+
18
+ def zone_offset=(zone_offset)
19
+ @zone_offset = build_zone_offset zone_offset
20
+ end
21
+
22
+ alias_method :utc_offset, :zone_offset
23
+ alias_method :gmt_offset, :zone_offset
24
+ alias_method :gmtoff, :zone_offset
25
+
26
+ def +(seconds)
27
+ self.class.new (time + seconds), zone_offset
28
+ end
29
+
30
+ def -(seconds)
31
+ self.class.new (time - seconds), zone_offset
32
+ end
33
+
34
+ def utc?
35
+ zone_offset == 0
36
+ end
37
+ alias_method :gmt?, :utc?
38
+
39
+ def to_utc
40
+ self.class.new time, 0
41
+ end
42
+
43
+ def to_zone(zone_offset)
44
+ self.class.new time, zone_offset
45
+ end
46
+
47
+ def to_time
48
+ time
49
+ end
50
+
51
+ def to_s
52
+ strftime '%F %T %z'
53
+ end
54
+ alias_method :inspect, :to_s
55
+
56
+ def strftime(format)
57
+ time_with_offset.strftime format.gsub('%Z', '').gsub('%z', zone_offset.to_s)
58
+ end
59
+
60
+ def self.now(zone_offset=nil)
61
+ new Time.now, zone_offset
62
+ end
63
+
64
+ def self.at(seconds, zone_offset=nil)
65
+ new Time.at(seconds), zone_offset
66
+ end
67
+
68
+ def self.parse(text)
69
+ match = REGEXP.match text
70
+ zone_offset = match ? match.to_s : nil
71
+ new Time.parse(text), zone_offset
72
+ end
73
+
74
+ private
75
+
76
+ attr_reader :time
77
+
78
+ def build_zone_offset(zone_offset)
79
+ case zone_offset
80
+ when ZoneOffset then zone_offset
81
+ when Numeric then ZoneOffset.new(zone_offset)
82
+ else ZoneOffset.parse(zone_offset)
83
+ end
84
+ end
85
+
86
+ def time_with_offset
87
+ time.getutc + zone_offset
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module Timing
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,36 @@
1
+ module Timing
2
+ class ZoneOffset < SimpleDelegator
3
+
4
+ REGEXP = /^([+-]?)(\d\d):?(\d\d)$/
5
+
6
+ def initialize(seconds)
7
+ raise ArgumentError, "#{seconds} is not a number" unless seconds.is_a? Numeric
8
+ super
9
+ end
10
+
11
+ def to_s
12
+ hours = Interval.new(to_f).to_hours.to_i
13
+ minutes = Interval.new(to_f % Interval.hours(1)).to_minutes.to_i
14
+ sign = self < 0 ? '-' : '+'
15
+
16
+ "#{sign}#{hours.to_s.rjust(2, '0')}#{minutes.to_s.rjust(2, '0')}"
17
+ end
18
+
19
+ def inspect
20
+ "#{to_s} (#{to_f})"
21
+ end
22
+
23
+ def self.parse(expression)
24
+ match = REGEXP.match expression.strip
25
+
26
+ raise ArgumentError, "Invalid time zone offset #{expression}" unless match
27
+
28
+ sign = match.captures[0] == '-' ? -1 : 1
29
+ hours = match.captures[1].to_i
30
+ minutes = match.captures[2].to_i
31
+
32
+ new (Interval.hours(hours) + Interval.minutes(minutes)) * sign
33
+ end
34
+
35
+ end
36
+ end
data/lib/timing.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'time'
2
+ require 'delegate'
3
+ require 'forwardable'
4
+
5
+ require 'timing/version'
6
+ require 'timing/helpers'
7
+ require 'timing/interval'
8
+ require 'timing/zone_offset'
9
+ require 'timing/time_in_zone'
10
+
11
+
12
+ module Timing
13
+ extend Helpers
14
+ end
data/timing.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'timing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'timing'
8
+ spec.version = Timing::VERSION
9
+ spec.authors = ['Gabriel Naiman']
10
+ spec.email = ['gnaiman@keepcon.com']
11
+
12
+ spec.summary = 'Time utils'
13
+ spec.description = 'Time utils'
14
+ spec.homepage = 'https://github.com/gabynaiman/timing'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.6'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'minitest', '~> 4.7'
25
+ spec.add_development_dependency 'turn', '~> 0.9'
26
+ spec.add_development_dependency 'simplecov'
27
+ spec.add_development_dependency 'pry-nav'
28
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Naiman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-18 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: turn
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-nav
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Time utils
98
+ email:
99
+ - gnaiman@keepcon.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".coveralls.yml"
105
+ - ".gitignore"
106
+ - ".ruby-gemset"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - lib/timing.rb
114
+ - lib/timing/helpers.rb
115
+ - lib/timing/interval.rb
116
+ - lib/timing/time_in_zone.rb
117
+ - lib/timing/version.rb
118
+ - lib/timing/zone_offset.rb
119
+ - timing.gemspec
120
+ homepage: https://github.com/gabynaiman/timing
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.6
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Time utils
144
+ test_files: []