tempus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport", ">= 2.3.5"
4
+ gem "i18n"
5
+
6
+ group :development do
7
+ gem "rspec", "~> 2.3.0"
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.7.0"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.1.3)
5
+ multi_json (~> 1.0)
6
+ diff-lcs (1.1.3)
7
+ git (1.2.5)
8
+ i18n (0.6.0)
9
+ jeweler (1.7.0)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.6.5)
15
+ multi_json (1.0.4)
16
+ rake (0.9.2.2)
17
+ rdoc (3.12)
18
+ json (~> 1.4)
19
+ rspec (2.3.0)
20
+ rspec-core (~> 2.3.0)
21
+ rspec-expectations (~> 2.3.0)
22
+ rspec-mocks (~> 2.3.0)
23
+ rspec-core (2.3.1)
24
+ rspec-expectations (2.3.0)
25
+ diff-lcs (~> 1.1.2)
26
+ rspec-mocks (2.3.0)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ activesupport (>= 2.3.5)
33
+ bundler (~> 1.0.0)
34
+ i18n
35
+ jeweler (~> 1.7.0)
36
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Danilo Jeremias da Silva
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,38 @@
1
+ = tempus
2
+
3
+ Gem to efficiently manipulate the time, adding, subtracting and converting hours.
4
+
5
+ == EXAMPLE USAGE
6
+
7
+ hours = Tempus.new(30.hours + 5.minutes + 3.seconds)
8
+ => <Tempus:8508680 seconds=108303.0, formated=30:05:03>
9
+ hours.value_in_minutes
10
+ => 1805.05
11
+ hours.to_s("%H hours and %M minutes")
12
+ => "30 hours and 05 minutes"
13
+
14
+ hours = "35:05:01".to_tempus
15
+ => <Tempus:7228060 seconds=126301.0, formated=35:05:01>
16
+ hours + 6.hours
17
+ => <Tempus:6931320 seconds=147901.0, formated=41:05:01>
18
+
19
+ "1:00:00".to_tempus + "2:00"
20
+ => <Tempus:6634700 seconds=10800.0, formated=03:00:00>
21
+
22
+ Tempus.new Time.now
23
+ => <Tempus:6578680 seconds=46455.162950918, formated=12:54:15>
24
+
25
+ == Contributing to tempus
26
+
27
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
28
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
29
+ * Fork the project.
30
+ * Start a feature/bugfix branch.
31
+ * Commit and push until you are happy with your contribution.
32
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
33
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
34
+
35
+ == Copyright
36
+
37
+ Copyright (c) 2012 Danilo Jeremias da Silva. See LICENSE.txt for
38
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "tempus"
17
+ gem.homepage = "http://github.com/dannnylo/tempus"
18
+ gem.license = "MIT"
19
+ gem.summary = %Q{Gem to manipulate efficiently time}
20
+ gem.description = %Q{Gem to efficiently manipulate the time, adding, subtracting and converting hours.}
21
+ gem.email = "danilo.silva@euax.com.br"
22
+ gem.authors = ["Danilo Jeremias da Silva"]
23
+ end
24
+ Jeweler::RubygemsDotOrgTasks.new
25
+
26
+ require 'rspec/core'
27
+ require 'rspec/core/rake_task'
28
+ RSpec::Core::RakeTask.new(:spec) do |spec|
29
+ spec.pattern = FileList['spec/**/*_spec.rb']
30
+ end
31
+
32
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :default => :spec
38
+
39
+ require 'rdoc/task'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "tempus #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/tempus.rb ADDED
@@ -0,0 +1,204 @@
1
+ require "active_support/core_ext"
2
+
3
+ #Class to manipulate efficiently time
4
+ #=== Example:
5
+ ## >> horas = Tempus.new()
6
+ ## => #<Tempus:0xb7016b40 @value=0.0>
7
+ class Tempus
8
+ attr_accessor :value
9
+
10
+ def initialize(value=0,count_all_time=false)
11
+ set(value, count_all_time)
12
+ end
13
+
14
+ #Atribui o valor para o objeto
15
+ ## Os valores podem ser passados em inteiros/floats ou Time.
16
+ ## O parâmetro count_all_time se falso serve para transformar apenas a hora de um objeto da classe Time, desconsiderando a data.
17
+ def set(value=0, count_all_time=false)
18
+ if(value.class == Time && !count_all_time)
19
+ @value = (value - value.beginning_of_day).to_f
20
+ else
21
+ @value = value.to_f
22
+ end
23
+ end
24
+
25
+ #Formata o horário para HH:MM:SS .
26
+ #=== Exemplo:
27
+ ## >> horas = Tempus.new(30.hours + 5.minutes + 3.seconds)
28
+ ## => #<Tempus:0xb6e4b860 @value=108303.0>
29
+ ## >> horas.to_s
30
+ ## => "30:05:03"
31
+ ## >> horas = Tempus.new(-30.hours - 5.minutes - 3.seconds)
32
+ ## => #<Tempus:0xb6e4b860 @value=-108303.0>
33
+ ## >> horas.to_s
34
+ ## => "-30:05:03"
35
+ ## >> horas.to_s("%H:%M")
36
+ ## => "-30:05"
37
+ ## >> horas.to_s("%H*%M*%S")
38
+ ## => "-30*05*03"
39
+ def to_s(format="%H:%M:%S")
40
+ h = ("%02d" % convert[0].to_i.abs)
41
+ m = ("%02d" % convert[1].to_i.abs)
42
+ s = ("%02d" % convert[2].to_i.abs)
43
+ n = @value<0 ? "-" : ""
44
+ str = format.to_s.gsub("%h","%H").gsub("%m","%M").gsub("%s","%S").gsub("%hh","%H").gsub("%mm","%M").gsub("%ss","%S")
45
+ str.gsub!("%H",h)
46
+ str.gsub!("%HH",h)
47
+ str.gsub!("%M",m)
48
+ str.gsub!("%MM",m)
49
+ str.gsub!("%S",s)
50
+ str.gsub!("%SS",s)
51
+ str = n + str unless(str=="")
52
+ str
53
+ end
54
+
55
+ def convert
56
+ valor = @value.to_f
57
+ h = (valor.to_f / 1.hour.to_f ).to_i
58
+ valor = valor.to_f - h.hours.to_f
59
+ m = (valor.to_f / 1.minute.to_f).to_i
60
+ s = (valor.to_f - m.minutes.to_f).to_i
61
+ return h,m,s
62
+ end
63
+
64
+ #Retorna o número de minutos
65
+ def minutes
66
+ convert[1]
67
+ end
68
+
69
+ #Retorna o número de segundos
70
+ def seconds
71
+ convert[2]
72
+ end
73
+
74
+ #Retorna o número de horas
75
+ def hours
76
+ convert[0]
77
+ end
78
+
79
+ def to_string
80
+ self.to_s
81
+ end
82
+
83
+ #Criando método para conversão para segundos.
84
+ def value_in_seconds
85
+ @value.to_i
86
+ end
87
+
88
+ #Criando método para conversão para horas.
89
+ def value_in_hours
90
+ @value.to_i / 1.hour.to_f
91
+ end
92
+
93
+ #Criando método para conversão para horas.
94
+ def value_in_minutes
95
+ @value.to_i / 1.minute.to_f
96
+ end
97
+
98
+ def value_in_days
99
+ @value.to_i / 1.day.to_f
100
+ end
101
+
102
+ def to_xls_time
103
+ self.value_in_days
104
+ end
105
+
106
+ #Soma valores ao objeto
107
+ def + v
108
+ if(v.class == Tempus)
109
+ Tempus.new(@value + v.value_in_seconds)
110
+ elsif(v.class == NilClass)
111
+ self
112
+ elsif(v.class == String)
113
+ Tempus.new(@value + v.to_tempus.value_in_seconds)
114
+ else
115
+ Tempus.new(@value + v)
116
+ end
117
+ end
118
+
119
+ #Subtrai valores ao objeto
120
+ def - v
121
+ if(v.class == Tempus)
122
+ Tempus.new(@value - v.value_in_seconds)
123
+ elsif(v.class == NilClass)
124
+ self
125
+ elsif(v.class == String)
126
+ Tempus.new(@value - v.to_tempus.value_in_seconds)
127
+ else
128
+ Tempus.new(@value - v)
129
+ end
130
+ end
131
+
132
+ #Criando método para conversão para Inteiro
133
+ def to_i
134
+ value_in_seconds
135
+ end
136
+
137
+ #Método para verificar se o valor é negativo
138
+ def negative?
139
+ to_i < 0
140
+ end
141
+
142
+ #Método para verificar se o valor é positivo
143
+ def positive?
144
+ to_i >= 0
145
+ end
146
+
147
+ def human
148
+ "#{"menos " if negative?}#{hours.abs} horas #{minutes.abs} minutos e #{seconds.abs} segundos"
149
+ end
150
+
151
+ def inspect
152
+ "<Tempus:#{self.object_id} seconds=#{value}, formated=#{to_s}>"
153
+ end
154
+
155
+ def ==(object)
156
+ if object.is_a?(Tempus)
157
+ value == object.value
158
+ else
159
+ false
160
+ end
161
+ end
162
+ end
163
+
164
+
165
+ class Fixnum
166
+ #Criando método para conversão da classe Fixnum
167
+ def to_tempus
168
+ Tempus.new(self)
169
+ end
170
+ end
171
+
172
+ class Float
173
+ #Criando método para conversão da classe Float
174
+ def to_tempus
175
+ Tempus.new(self)
176
+ end
177
+ end
178
+
179
+ class Time
180
+ #Criando método para conversão da classe Float
181
+ def to_tempus(count_all_time=false)
182
+ Tempus.new(self, count_all_time)
183
+ end
184
+ end
185
+
186
+ class NilClass
187
+ #Criando método para a classe nil
188
+ def to_tempus
189
+ Tempus.new(self)
190
+ end
191
+ end
192
+
193
+ class String
194
+ #Criando método para conversão da classe String
195
+ def to_tempus
196
+ str = (self).to_s.split(":")
197
+ value = 0
198
+ [:hours, :minutes, :seconds].each_with_index do |m, i|
199
+ value += str.at(i).to_i.abs.send(m.to_s)
200
+ end
201
+ value = -value if str.to_s.include?('-')
202
+ Tempus.new(value)
203
+ end
204
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'tempus'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,75 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe "Tempus" do
5
+ before do
6
+ @hours = Tempus.new(30.hours + 5.minutes + 3.seconds)
7
+ @negative_hours = Tempus.new(-30.hours - 5.minutes - 3.seconds)
8
+ end
9
+
10
+ it "new" do
11
+ @hours.should_not == nil
12
+ @negative_hours.should_not == nil
13
+ end
14
+
15
+ it "set" do
16
+ hours = @hours.clone
17
+ hours.object_id.should_not == @hours.object_id
18
+ hours.set(125).should == 125.0
19
+ hours.to_s.should == "00:02:05"
20
+ hours.value_in_seconds.should == 125
21
+ hours.set(Time.now.midnight).should == 0.0
22
+ hours.value_in_seconds.should == 0
23
+ hours.to_s.should == "00:00:00"
24
+ hours.set(Time.now.midnight,true).should == Time.now.midnight.to_i
25
+ hours.to_s("%M:%S").should == "00:00"
26
+ end
27
+
28
+ it "to_s" do
29
+ @hours.respond_to?("to_s").should == true
30
+ @hours.to_s("").should == ""
31
+ @hours.to_s().should == "30:05:03"
32
+ @hours.to_s("%H").should == "30"
33
+ @hours.to_s("%M").should == "05"
34
+ @hours.to_s("%S").should == "03"
35
+ @hours.to_s("%Mdakhdkj$#").should == "05dakhdkj$#"
36
+ @hours.to_s("hours Utilizadas: %H:%M").should == "hours Utilizadas: 30:05"
37
+ @hours.to_s("%H:%M:%S").should == "30:05:03"
38
+ @negative_hours.to_s("").should == ""
39
+ @negative_hours.to_s().should == "-30:05:03"
40
+ @negative_hours.to_s("%H").should == "-30"
41
+ @negative_hours.to_s("%M").should == "-05"
42
+ @negative_hours.to_s("%S").should == "-03"
43
+ @negative_hours.to_s("%Mdakhdkj$#").should == "-05dakhdkj$#"
44
+ #Por enquanto existe este bug
45
+ @negative_hours.to_s("hours Utilizadas: %H:%M").should == "-hours Utilizadas: 30:05" #FIXME: Corrigir este problema.
46
+ @negative_hours.to_s("%H:%M:%S").should == "-30:05:03"
47
+ end
48
+
49
+ it "plus" do
50
+ (@hours + "00:01:00").to_s.should == "30:06:03"
51
+ (@hours + 1).to_s.should == "30:05:04"
52
+ (@hours + 1.minutes).to_s.should == "30:06:03"
53
+ (@hours + 1.hours).to_s.should == "31:05:03"
54
+ (@hours + 5.hours).to_s.should == "35:05:03"
55
+ (@hours + nil).to_s.should == "30:05:03"
56
+ (@hours + @negative_hours).to_s.should == "00:00:00"
57
+ end
58
+
59
+ it "minus" do
60
+ (@hours - "00:01:00").to_s.should == "30:04:03"
61
+ (@hours - 1).to_s.should == "30:05:02"
62
+ (@hours - 1.minutes).to_s.should == "30:04:03"
63
+ (@hours - 1.hours).to_s.should == "29:05:03"
64
+ (@hours - 5.hours).to_s.should == "25:05:03"
65
+ (@hours - nil).to_s.should == "30:05:03"
66
+ (@hours - @negative_hours ).to_s.should == "60:10:06"
67
+ end
68
+
69
+ it "String to Tempus" do
70
+ "12:05:35".to_tempus.value.should == Tempus.new(12.hours + 5.minutes + 35.seconds).value
71
+ "01:00:01".to_tempus.value.should == Tempus.new(1.hours + 1.seconds).value
72
+ "30:5:3".to_tempus.should == @hours
73
+ "-30:5:3".to_tempus.should == @negative_hours
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tempus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Danilo Jeremias da Silva
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &18810960 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *18810960
25
+ - !ruby/object:Gem::Dependency
26
+ name: i18n
27
+ requirement: &18810440 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *18810440
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &18809920 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.3.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *18809920
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &18809380 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *18809380
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &18808880 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.7.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *18808880
69
+ description: Gem to efficiently manipulate the time, adding, subtracting and converting
70
+ hours.
71
+ email: danilo.silva@euax.com.br
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE.txt
76
+ - README.rdoc
77
+ files:
78
+ - .document
79
+ - .rspec
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.rdoc
84
+ - Rakefile
85
+ - VERSION
86
+ - lib/tempus.rb
87
+ - spec/spec_helper.rb
88
+ - spec/tempus_spec.rb
89
+ homepage: http://github.com/dannnylo/tempus
90
+ licenses:
91
+ - MIT
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ segments:
103
+ - 0
104
+ hash: -1063616463208382372
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.6
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Gem to manipulate efficiently time
117
+ test_files: []