time_second 0.1.0

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: bdd87946e5418973cb5d2e615ba2b12cb2cb6972152492b29c6f2d15f48bbb88
4
+ data.tar.gz: 4cb54d9f1bf168e827bd15d3f62f7ec8a2d30738a303507090683acca08fa7a6
5
+ SHA512:
6
+ metadata.gz: 8258fbc75ef28ff793d2116762a068b0f775db669cb70fbcbc8d26db6a3e1af229da89b2b7ac57434eedcf01f11d060d056c33477251e36ca6faba9a59e6c27d
7
+ data.tar.gz: 650be6fb91cb67fc364d17338ae767709027bc5e20f63d5efedc367bee0fee07336c92159533efb7729901433848cb6dcb1475a519fde24bbe1f17dd04b51eea
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
@@ -0,0 +1 @@
1
+ inherit_from: .rubocop_todo.yml
@@ -0,0 +1,63 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2020-02-01 00:24:29 +0900 using RuboCop version 0.79.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Cop supports --auto-correct.
11
+ # Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment.
12
+ Layout/ExtraSpacing:
13
+ Exclude:
14
+ - 'time_second.gemspec'
15
+
16
+ # Offense count: 1
17
+ # Cop supports --auto-correct.
18
+ # Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator.
19
+ # SupportedStylesForExponentOperator: space, no_space
20
+ Layout/SpaceAroundOperators:
21
+ Exclude:
22
+ - 'time_second.gemspec'
23
+
24
+ # Offense count: 1
25
+ # Cop supports --auto-correct.
26
+ Style/ExpandPathArguments:
27
+ Exclude:
28
+ - 'time_second.gemspec'
29
+
30
+ # Offense count: 2
31
+ # Cop supports --auto-correct.
32
+ # Configuration parameters: EnforcedStyle.
33
+ # SupportedStyles: format, sprintf, percent
34
+ Style/FormatString:
35
+ Exclude:
36
+ - 'lib/time_second.rb'
37
+
38
+ # Offense count: 6
39
+ # Cop supports --auto-correct.
40
+ # Configuration parameters: EnforcedStyle.
41
+ # SupportedStyles: always, never
42
+ Style/FrozenStringLiteralComment:
43
+ Exclude:
44
+ - 'Gemfile'
45
+ - 'Rakefile'
46
+ - 'lib/time_second.rb'
47
+ - 'time_second.gemspec'
48
+ - 'test/time_second_test.rb'
49
+ - 'test/test_helper.rb'
50
+
51
+ # Offense count: 1
52
+ # Cop supports --auto-correct.
53
+ # Configuration parameters: UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
54
+ # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
55
+ Style/HashSyntax:
56
+ EnforcedStyle: hash_rockets
57
+
58
+ # Offense count: 1
59
+ # Cop supports --auto-correct.
60
+ # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
61
+ # URISchemes: http, https
62
+ Layout/LineLength:
63
+ Max: 85
@@ -0,0 +1,10 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.3.8
6
+ - 2.4.9
7
+ - 2.5.7
8
+ - 2.6.3
9
+ - 2.7.0
10
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.1.0] - 2020-02-06
8
+
9
+ - Initial release.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ time_second (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ jaro_winkler (1.5.4)
11
+ minitest (5.14.0)
12
+ parallel (1.19.1)
13
+ parser (2.7.0.2)
14
+ ast (~> 2.4.0)
15
+ rainbow (3.0.0)
16
+ rake (12.3.3)
17
+ rubocop (0.79.0)
18
+ jaro_winkler (~> 1.5.1)
19
+ parallel (~> 1.10)
20
+ parser (>= 2.7.0.1)
21
+ rainbow (>= 2.2.2, < 4.0)
22
+ ruby-progressbar (~> 1.7)
23
+ unicode-display_width (>= 1.4.0, < 1.7)
24
+ ruby-progressbar (1.10.1)
25
+ unicode-display_width (1.6.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ minitest (~> 5.8)
32
+ rake (~> 12.0)
33
+ rubocop (~> 0.79)
34
+ time_second!
35
+
36
+ BUNDLED WITH
37
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Toreta, Inc.
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,56 @@
1
+ [![Build Status](https://travis-ci.com/toreta/time_second.svg?branch=master)](https://travis-ci.com/toreta/time_second)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/73a1cfd234b42457bc79/maintainability)](https://codeclimate.com/github/toreta/time_second/maintainability)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/73a1cfd234b42457bc79/test_coverage)](https://codeclimate.com/github/toreta/time_second/test_coverage)
4
+
5
+ # TimeSecond
6
+
7
+ Make it easy to handle numeric as seconds.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'time_second'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install time_second
24
+
25
+ ## Usage
26
+
27
+
28
+ ``` ruby
29
+ # New from Integer
30
+ t = TimeSecond.new(2 * 60 * 60 + 1 * 60 + 39) # 2:01:39
31
+ t.hour #=> 2
32
+ t.minute #=> 1
33
+ t.second #=> 39
34
+ t.hms #=> '02:01:39'
35
+ t.hm #=> '02:01'
36
+
37
+ # New from String
38
+ t = TimeSecond.parse('02:01:39')
39
+ t.to_i #=> 7299
40
+
41
+ # Support basic arithmetic operations
42
+ TimeSecond.new(7) + 3 #=> 10
43
+ TimeSecond.new(7) - 3 #=> 4
44
+ TimeSecond.new(7) * 3 #=> 21
45
+ TimeSecond.new(7) / 3 #=> 2
46
+ TimeSecond.new(7) / 3.0 #=> 2.3333333333333335
47
+ TimeSecond.new(7) % 3 #=> 1
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/toreta/time_second](https://github.com/toreta/time_second).
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rubocop/rake_task'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'test'
7
+ t.libs << 'lib'
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ end
10
+
11
+ RuboCop::RakeTask.new
12
+
13
+ task :default => :test
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Make it easy to handle numeric value as seconds.
4
+ class TimeSecond < Numeric
5
+ VERSION = '0.1.0'
6
+
7
+ include Comparable
8
+
9
+ # Parse 'HH:MM:SS' format string and return its object
10
+ #
11
+ # @param [String] str 'HH:MM:SS' or 'HH:MM'
12
+ #
13
+ # @return [TimeSecond]
14
+ def self.parse(str)
15
+ unless str.match(/\A\d{1,2}:\d{2}(?:\:\d{2})?\z/)
16
+ raise ArgumentError, 'Invalid string format'
17
+ end
18
+
19
+ h, m, s = str.split(':')
20
+ new(h.to_i * 60 * 60 + m.to_i * 60 + s.to_i)
21
+ end
22
+
23
+ def initialize(time) # :nodoc:
24
+ @time = time
25
+ end
26
+
27
+ # Returns integer value of seconds
28
+ #
29
+ # @return [Integer] seconds
30
+ def to_i
31
+ @time.to_int
32
+ end
33
+
34
+ # Returns float value of seconds
35
+ #
36
+ # @return [Float] seconds
37
+ def to_f
38
+ @time.to_f
39
+ end
40
+
41
+ # Return hour
42
+ #
43
+ # @return [Integer] Hour (0 ~ )
44
+ def hour
45
+ @time.to_i / 60 / 60
46
+ end
47
+
48
+ # Return minute
49
+ #
50
+ # @return [Integer] Minute (0 ~ 59)
51
+ def minute
52
+ @time.to_i / 60 % 60
53
+ end
54
+
55
+ # Return second
56
+ #
57
+ # @return [Integer] TimeSecond (0 ~ 59)
58
+ def second
59
+ @time.to_i % 60
60
+ end
61
+
62
+ # Return 'HH:MM:SS' format string
63
+ #
64
+ # @param [String] sep Seperator string. Default is ':'.
65
+ #
66
+ # @return [String] 'HH:MM:SS'
67
+ def hms(sep = ':')
68
+ "%02d#{sep}%02d#{sep}%02d" % [hour, minute, second]
69
+ end
70
+
71
+ # Return 'HH:MM' format string
72
+ #
73
+ # @param [String] sep Seperator string. Default is ':'.
74
+ #
75
+ # @return [String] 'HH:MM'
76
+ def hm(sep = ':')
77
+ "%02d#{sep}%02d" % [hour, minute]
78
+ end
79
+
80
+ # Compares one TimeSecond and another or a Numeric to this TimeSecond.
81
+ def <=>(other)
82
+ @time <=> other
83
+ end
84
+
85
+ # Returns true if other is with the same time.
86
+ def ==(other)
87
+ @time.to_f == other.to_f
88
+ end
89
+
90
+ # Adds another TimeSecond or Numeric to this TimeSecond.
91
+ def +(other)
92
+ r, l = @time.coerce(other)
93
+ self.class.new(l + r)
94
+ end
95
+
96
+ # Subtracts another TimeSecond or Numeric from this TimeSecond.
97
+ def -(other)
98
+ r, l = @time.coerce(other)
99
+ self.class.new(l - r)
100
+ end
101
+
102
+ # Multiple self by a Numeric and returns a new TimeSecond.
103
+ def *(other)
104
+ r, l = @time.coerce(other)
105
+ self.class.new(l * r)
106
+ end
107
+
108
+ # Divides self by a Numeric and returns a new TimeSecond.
109
+ def /(other)
110
+ r, l = @time.coerce(other)
111
+ self.class.new(l / r)
112
+ end
113
+
114
+ # Returns the modulo of this by another TimeSecond or Numeric.
115
+ def %(other)
116
+ r, l = @time.coerce(other)
117
+ self.class.new(l % r)
118
+ end
119
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/time_second'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'time_second'
5
+ spec.version = ::TimeSecond::VERSION
6
+ spec.authors = ['ziguzagu']
7
+ spec.email = ['ziguzagu@gmail.com']
8
+
9
+ spec.summary = 'Make it easy to handle numeric value as seconds.'
10
+ spec.description = 'Make it easy to handle numeric value as seconds.'
11
+ spec.homepage = 'https://github.com/toreta/time_second'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['changelog_uri'] = 'https://github.com/toreta/time_second/CHANGELOG.md'
17
+
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.executables = []
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'minitest', '~> 5.8'
25
+ spec.add_development_dependency 'rake', '~> 12.0'
26
+ spec.add_development_dependency 'rubocop', '~> 0.79'
27
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_second
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ziguzagu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.79'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.79'
55
+ description: Make it easy to handle numeric value as seconds.
56
+ email:
57
+ - ziguzagu@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rubocop.yml"
64
+ - ".rubocop_todo.yml"
65
+ - ".travis.yml"
66
+ - CHANGELOG.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - lib/time_second.rb
73
+ - time_second.gemspec
74
+ homepage: https://github.com/toreta/time_second
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/toreta/time_second
79
+ changelog_uri: https://github.com/toreta/time_second/CHANGELOG.md
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.3.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Make it easy to handle numeric value as seconds.
99
+ test_files: []