hours_to_seconds 1.0.15

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
+ SHA256:
3
+ metadata.gz: cff5c3ebd3aa21cac001847f472369ac39b332f5500acf1548ccbf7d97b76ff6
4
+ data.tar.gz: 1b234b52ba9604e4eb3378c63fb6c513f30aeae9ce766b9e0afcfa33b43df47e
5
+ SHA512:
6
+ metadata.gz: 3cf687e5717138414f1199c58dfe62c5fe0cbbcaae0382fdcb5c2c49b57670a2cb34af28d237cebb656c1e46317b0bc54af3d047ae1c186a3f6fe1f2a3d2ef93
7
+ data.tar.gz: 8d9b2b77b564f7c9cf6b3bac03d53945c48e5813065c351498ecb7be74c95f76ccd5b11ec97ca22345e605a6f4d20174c7369b04b6d98bc1aaa9c3923af46b0e
data/USAGE.md ADDED
@@ -0,0 +1,11 @@
1
+ = Usage of this Library
2
+
3
+ == Requiring it
4
+
5
+ require 'hours_to_seconds'
6
+
7
+ == Using it in a project
8
+
9
+ _ = HoursToSeconds.new("00:33:55")
10
+ _.run
11
+
@@ -0,0 +1,36 @@
1
+ # =========================================================================== #
2
+ # Gemspec for Project HoursToSeconds.
3
+ # =========================================================================== #
4
+ require 'hours_to_seconds'
5
+ require 'roebe'
6
+
7
+ Gem::Specification.new { |s|
8
+
9
+ s.name = 'hours_to_seconds'
10
+ s.version = HoursToSeconds::VERSION
11
+ s.date = Time.now.strftime('%Y-%m-%d')
12
+
13
+ s.summary = <<-EOF
14
+ EOF
15
+
16
+ s.description = <<-EOF
17
+ This library is called hours_to_seconds.
18
+ EOF
19
+
20
+ s.extra_rdoc_files = %w()
21
+
22
+ s.authors = ['Robert A. Heiler']
23
+ s.email = Roebe.email?
24
+ s.files = Dir['**/*']
25
+ s.license = 'MIT'
26
+
27
+ s.homepage = 'https://rubygems.org/gems/hours_to_seconds'
28
+
29
+ s.required_ruby_version = '>= '+Roebe.second_most_stable_version_of_ruby
30
+ s.required_rubygems_version = '>= '+Gem::VERSION
31
+ s.rubygems_version = '>= '+Gem::VERSION
32
+
33
+ # Dependencies for this project
34
+ s.add_dependency 'colours'
35
+
36
+ }
@@ -0,0 +1,186 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === HoursToSeconds
6
+ #
7
+ # This class converts from a larger duration format to the equivalent
8
+ # amount in seconds.
9
+ #
10
+ # Specific usage examples:
11
+ #
12
+ # HoursToSeconds['01:02:03.33']
13
+ # HoursToSeconds.new '01:02:03.33'
14
+ #
15
+ # =========================================================================== #
16
+ # require 'hours_to_seconds'
17
+ # =========================================================================== #
18
+ class HoursToSeconds # === HoursToSeconds
19
+
20
+ require 'hours_to_seconds/version/version.rb'
21
+
22
+ begin
23
+ require 'colours'
24
+ include Colours
25
+ rescue LoadError; end
26
+
27
+ # ========================================================================= #
28
+ # === initialize
29
+ # ========================================================================= #
30
+ def initialize(
31
+ i = nil,
32
+ run_already = true
33
+ )
34
+ reset
35
+ set_format(i)
36
+ case run_already
37
+ when :dont_run_yet, :do_not_run_yet
38
+ run_already = false
39
+ when :be_silent
40
+ run_already = true
41
+ @be_verbose = false
42
+ end
43
+ start_conversion
44
+ run if run_already
45
+ end
46
+
47
+ # ========================================================================= #
48
+ # === reset (reset tag)
49
+ # ========================================================================= #
50
+ def reset
51
+ # ======================================================================= #
52
+ # === @format
53
+ # ======================================================================= #
54
+ @format = ''
55
+ # ======================================================================= #
56
+ # === @be_verbose
57
+ # ======================================================================= #
58
+ @be_verbose = true
59
+ end
60
+
61
+ # ========================================================================= #
62
+ # === start_conversion
63
+ #
64
+ # This is called from the method run().
65
+ # It will set the ivar @n_seconds.
66
+ #
67
+ # The method will assign to the instance variable @splitted.
68
+ # ========================================================================= #
69
+ def start_conversion(
70
+ i = @format
71
+ )
72
+ @splitted = i.split(':')
73
+ _ = @splitted[0].to_i * 60 * 60 # Add hours here.
74
+ _ += @splitted[1].to_i * 60 # Add minutes here.
75
+ _ = _.to_f
76
+ # ======================================================================= #
77
+ # Next add "n seconds". However had, this can be a String
78
+ # such as "11.91". We don't want to lose the fraction
79
+ # part though.
80
+ # ======================================================================= #
81
+ _ += @splitted[2].to_f # Add Seconds here.
82
+ @n_seconds = _
83
+ end; alias convert start_conversion # === convert
84
+ alias process start_conversion # === process
85
+
86
+ # ========================================================================= #
87
+ # === n_milliseconds
88
+ # ========================================================================= #
89
+ def n_milliseconds
90
+ return @n_seconds.to_i * 1000
91
+ end
92
+
93
+ # ========================================================================= #
94
+ # === to_s
95
+ # ========================================================================= #
96
+ def to_s
97
+ result = sfancy(@format)+' sind '+simp(@n_seconds.to_s)+' Sekunden.'
98
+ return result
99
+ end
100
+
101
+ # ========================================================================= #
102
+ # == be_silent
103
+ # ========================================================================= #
104
+ def be_silent
105
+ @be_verbose = false
106
+ end
107
+
108
+ # ========================================================================= #
109
+ # === be_verbose?
110
+ # ========================================================================= #
111
+ def be_verbose?
112
+ @be_verbose
113
+ end
114
+
115
+ # ========================================================================= #
116
+ # === n_seconds?
117
+ # ========================================================================= #
118
+ def n_seconds?
119
+ @n_seconds
120
+ end; alias n_seconds n_seconds? # === n_seconds
121
+ alias seconds n_seconds? # === seconds
122
+
123
+ # ========================================================================= #
124
+ # === set_format
125
+ #
126
+ # We have to make sure that the format will be proper, as otherwise
127
+ # further calculations may fail/be incorrect.
128
+ # ========================================================================= #
129
+ def set_format(i = nil)
130
+ i = '02:28:07.97' if i.nil? # Default.
131
+ i = i.to_s.dup
132
+ if i.end_with? 's'
133
+ i.chop!
134
+ end
135
+ i.tr!('m',':') if i.include? 'm'
136
+ i.tr!('h',':') if i.include? 'h'
137
+ if i.count(':') == 2 and i =~ /^\d:/
138
+ i.prepend('0') # Correct input such as '1:32:48.2' here.
139
+ end
140
+ @format = i # @format is now something like '02:28:07.97'.
141
+ end
142
+
143
+ # ========================================================================= #
144
+ # === display_result
145
+ # ========================================================================= #
146
+ def display_result
147
+ e to_s if be_verbose? # Simply call to_s here, but only if we are verbose.
148
+ end; alias display display_result # === display
149
+ alias output display_result # === output
150
+ alias report display_result # === report
151
+
152
+ # ========================================================================= #
153
+ # === run
154
+ # ========================================================================= #
155
+ def run
156
+ display_result # We only display it for now.
157
+ end
158
+
159
+ # ========================================================================= #
160
+ # === HoursToSeconds[]
161
+ #
162
+ # The following asks this question:
163
+ #
164
+ # "How many seconds does a 24 hours day have?"
165
+ #
166
+ # Usage example:
167
+ #
168
+ # x = HoursToSeconds[2, :be_quiet] # => 7200
169
+ #
170
+ # ========================================================================= #
171
+ def self.[](i, optional_be_quiet = false)
172
+ _ = new(i, :dont_run_yet)
173
+ case optional_be_quiet
174
+ when :be_quiet, :be_silent
175
+ optional_be_quiet = true
176
+ end
177
+ _.be_silent if optional_be_quiet
178
+ _.run
179
+ return _.n_seconds
180
+ end
181
+
182
+ end
183
+
184
+ if __FILE__ == $PROGRAM_NAME
185
+ _ = HoursToSeconds.new(ARGV.first, true)
186
+ end # hours 02:28:07.97
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ class HoursToSeconds
6
+
7
+ # ========================================================================= #
8
+ # === VERSION
9
+ # ========================================================================= #
10
+ VERSION = '1.0.15'
11
+
12
+ end
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'hours_to_seconds/www/embeddable_interface.rb'
6
+ # include HoursToSeconds::EmbeddableInterface
7
+ # =========================================================================== #
8
+ class HoursToSeconds
9
+
10
+ module EmbeddableInterface
11
+
12
+ # ========================================================================= #
13
+ # === HoursToSeconds::EmbeddableInterface.routes?
14
+ #
15
+ # Define all legal routes via this Array. This Array will then be used
16
+ # to add more routes to any sinatra-application that needs it.
17
+ # ========================================================================= #
18
+ def self.routes?
19
+ [
20
+ 'hours_to_seconds',
21
+ 'hours_to_seconds/*'
22
+ ]
23
+ end
24
+
25
+ # ========================================================================= #
26
+ # === return_sinatra_hours_to_seconds
27
+ # ========================================================================= #
28
+ def return_sinatra_hours_to_seconds
29
+ 'Please provide the number of hours that should be converted into '\
30
+ 'n seconds.'
31
+ end
32
+
33
+ # ========================================================================= #
34
+ # === return_sinatra_hours_to_seconds_with_arguments
35
+ # ========================================================================= #
36
+ def return_sinatra_hours_to_seconds_with_arguments(
37
+ i = web_params_as_string?
38
+ )
39
+ i = i.dup if i.frozen?
40
+ n_seconds = HoursToSeconds[i.to_i, :be_quiet] # => 7200
41
+ _ = ''.dup
42
+ _ << '<b>'+i.to_s+'</b> hours equal <b>'+n_seconds.to_s+'</b> seconds.<br>'
43
+ return _
44
+ end
45
+
46
+ end
47
+
48
+ # =========================================================================== #
49
+ # === HoursToSeconds.embeddable_interface
50
+ # =========================================================================== #
51
+ def self.embeddable_interface
52
+ object = Object.new
53
+ object.extend(::HoursToSeconds::EmbeddableInterface)
54
+ return object
55
+ end
56
+
57
+ end
@@ -0,0 +1,3 @@
1
+ require 'hours_to_seconds/hours_to_seconds.rb'
2
+ require 'hours_to_seconds/www/embeddable_interface.rb'
3
+
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # =========================================================================== #
4
+ require 'colours/autoinclude'
5
+ require 'hours_to_seconds'
6
+
7
+ input = '01:52:02.09'
8
+ e 'Input format is: '+sfancy(input)
9
+ hours_to_seconds = HoursToSeconds.new(input, :dont_run_yet)
10
+ e 'These are n seconds: '+sfancy(hours_to_seconds.n_seconds)
11
+
12
+ input = '00:15:00.00'
13
+ e 'Input format is: '+sfancy(input)
14
+ hours_to_seconds = HoursToSeconds.new(input, :dont_run_yet)
15
+ e 'These are n seconds: '+sfancy(hours_to_seconds.n_seconds)
16
+
17
+ input = '1h10m30s'
18
+ e 'Input format is: '+sfancy(input)
19
+ hours_to_seconds = HoursToSeconds.new(input, :dont_run_yet)
20
+ e 'These are n seconds: '+sfancy(hours_to_seconds.n_seconds)
21
+
22
+ # Testing '1:32:48.2' as input.
23
+ input = '1:32:48.2'
24
+ e 'Input format is: '+sfancy(input)
25
+ hours_to_seconds = HoursToSeconds.new(input, :dont_run_yet)
26
+ e 'These are n seconds: '+sfancy(hours_to_seconds.n_seconds)
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hours_to_seconds
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.15
5
+ platform: ruby
6
+ authors:
7
+ - Robert A. Heiler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colours
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: " This library is called hours_to_seconds.\n"
28
+ email: shevy@inbox.lt
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - USAGE.md
34
+ - hours_to_seconds.gemspec
35
+ - lib/hours_to_seconds.rb
36
+ - lib/hours_to_seconds/hours_to_seconds.rb
37
+ - lib/hours_to_seconds/version/version.rb
38
+ - lib/hours_to_seconds/www/embeddable_interface.rb
39
+ - test/testing_hours_to_seconds.rb
40
+ homepage: https://rubygems.org/gems/hours_to_seconds
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.7.3
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 3.2.24
58
+ requirements: []
59
+ rubygems_version: 3.2.24
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: ''
63
+ test_files: []