time_parser 0.1.0

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.
Files changed (2) hide show
  1. data/lib/time_parser.rb +36 -0
  2. metadata +45 -0
@@ -0,0 +1,36 @@
1
+ ##
2
+ # Library for parsing time intervals from config files.
3
+ #
4
+ # Suppots days, hours, minutes and seconds; leaves numeric as numeric.
5
+ class TimeParser
6
+ ##
7
+ # Usage
8
+ #
9
+ # TimeParser.parse('1m 1s') # => 61
10
+ # TimeParser.parse(10) # => 10
11
+ def self.parse(str)
12
+ return str if str.is_a?(Numeric)
13
+ time = 0
14
+ str.gsub(' ', '').scan(/(\d+)(m|s|h|d)/).each do |amount, measure|
15
+ time += amount.to_i * TOKENS[measure]
16
+ end
17
+ str.gsub(' ', '').scan(/(\d+)$/).each do |amount|
18
+ time += amount[0].to_i
19
+ end
20
+ time
21
+ end
22
+
23
+ def self.version
24
+ '0.1.0'
25
+ end
26
+
27
+ private
28
+
29
+ TOKENS = {
30
+ 's' => 1,
31
+ 'm' => 60,
32
+ 'h' => 60 * 60,
33
+ 'd' => 60 * 60 * 24
34
+ }
35
+ end
36
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Promix17
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-11-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Library to parse time intervals to seconds
15
+ email: promix17@yandex.ru
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ./lib/time_parser.rb
21
+ homepage: https://bitbucket.org/promix17/time_parser/
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.23
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Parse time intervals library
45
+ test_files: []