relativity 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/README.rdoc +21 -0
- data/Rakefile +1 -0
- data/lib/relativity.rb +2 -0
- data/lib/relativity/relative_time.rb +52 -0
- data/lib/relativity/version.rb +3 -0
- data/relativity.gemspec +22 -0
- data/spec/relative_time_spec.rb +147 -0
- data/spec/spec_helper.rb +1 -0
- metadata +78 -0
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create 1.9.3@relativity
|
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= Relativity
|
2
|
+
|
3
|
+
Time, Date and DateTime classes don't have a mode for working with relative
|
4
|
+
time inside 1 day (or 1 week, 1 month, 1 quarter, etc.).
|
5
|
+
|
6
|
+
A RelativeTime object, relative to a day or a week, is useful to describe
|
7
|
+
e.g. opening hours of a business. In a next phase, a RelativeTimeRange
|
8
|
+
will be built on top, so the ranges of opening hours can be represented.
|
9
|
+
|
10
|
+
== Example
|
11
|
+
|
12
|
+
require 'relativity'
|
13
|
+
opens_at = RelativeTime.new(9) #=> 09:00:00
|
14
|
+
closes_at = RelativeTime.new(18,30) #=> 18:30:00
|
15
|
+
|
16
|
+
rt = RelativeTime.new #=> 21:17:40
|
17
|
+
rt.seconds_since_midnight #=> #<BigDecimal:9d35d48,'0.7666056825 7238E5',18(45)>
|
18
|
+
rt.hours #=> 21
|
19
|
+
rt.minutes #=> 17
|
20
|
+
rt.seconds #=> 40
|
21
|
+
rt.nano_seconds #=> 568257238
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/relativity.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
class RelativeTime
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
|
5
|
+
attr_reader :seconds_since_midnight
|
6
|
+
|
7
|
+
def initialize(hours = nil, minutes = nil, seconds = nil, nano_seconds = nil)
|
8
|
+
super()
|
9
|
+
if hours
|
10
|
+
hh = hours.to_i
|
11
|
+
mm = minutes.to_i
|
12
|
+
ss = seconds.to_i
|
13
|
+
nn = nano_seconds.to_i
|
14
|
+
else
|
15
|
+
t = Time.new
|
16
|
+
hh = t.hour
|
17
|
+
mm = t.min
|
18
|
+
ss = t.sec
|
19
|
+
nn = t.nsec
|
20
|
+
end
|
21
|
+
@seconds_since_midnight = ((hh * 3600) + (mm * 60) + ss + BigDecimal(nn)/BigDecimal(1000000000))%(24*3600)
|
22
|
+
end
|
23
|
+
|
24
|
+
def hours
|
25
|
+
hours = Integer(@seconds_since_midnight/(3600)).tap do |h|
|
26
|
+
raise "Internal ERROR in RelativeTime; hours is #{h}" if (h < 0 || h > 23)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def minutes
|
31
|
+
Integer(@seconds_since_midnight / 60) % 60
|
32
|
+
end
|
33
|
+
|
34
|
+
def seconds
|
35
|
+
Integer(@seconds_since_midnight) % 60
|
36
|
+
end
|
37
|
+
|
38
|
+
def nano_seconds
|
39
|
+
Integer((@seconds_since_midnight % 1) * 1000000000)
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
[hours, minutes, seconds].map{|e| rjust_2_0(e.to_s)}.join(':')
|
44
|
+
end
|
45
|
+
|
46
|
+
def rjust_2_0(s)
|
47
|
+
s.rjust(2,'0')
|
48
|
+
end
|
49
|
+
|
50
|
+
private :rjust_2_0
|
51
|
+
|
52
|
+
end
|
data/relativity.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "relativity/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "relativity"
|
7
|
+
s.version = Relativity::VERSION
|
8
|
+
s.authors = ["Peter Vandenabeele"]
|
9
|
+
s.email = ["peter@vandenabeele.com"]
|
10
|
+
s.homepage = "https://github.com/petervandenabeele/relativity"
|
11
|
+
s.summary = %q{time and time_ranges relative to day, week, month, quarter etc.}
|
12
|
+
s.description = s.summary
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
# specify any dependencies here; for example:
|
20
|
+
s.add_development_dependency 'rspec', '>= 2.7'
|
21
|
+
s.add_dependency 'bundler', '>= 1.0'
|
22
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RelativeTime do
|
4
|
+
|
5
|
+
it "new creates a RelativeTime" do
|
6
|
+
lambda { subject }.should_not raise_error
|
7
|
+
end
|
8
|
+
|
9
|
+
context "time in a day" do
|
10
|
+
|
11
|
+
it "seconds_since_midnight" do
|
12
|
+
lambda { subject.seconds_since_midnight }.should_not raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it "new creates a RelativeTime close to now" do
|
16
|
+
rt_s = subject.seconds_since_midnight
|
17
|
+
t = Time.new
|
18
|
+
t_s = (t.hour*60 + t.min)*60 + t.sec # seconds_since_midnight from Time
|
19
|
+
(((t_s-rt_s).abs+2)%(24*60*60)).should be <= 4
|
20
|
+
end
|
21
|
+
|
22
|
+
it "hours" do
|
23
|
+
(((subject.hours - Time.new.hour).abs+1)%24).should be <= 2
|
24
|
+
end
|
25
|
+
|
26
|
+
it "minutes" do
|
27
|
+
(((subject.minutes - Time.new.min).abs+1)%60).should be <= 2
|
28
|
+
end
|
29
|
+
|
30
|
+
it "seconds" do
|
31
|
+
(((subject.seconds - Time.new.sec).abs+2)%60).should be <= 4
|
32
|
+
end
|
33
|
+
|
34
|
+
it "seconds_since_midnight should be BigDecimal" do
|
35
|
+
subject.seconds_since_midnight.should be_kind_of(BigDecimal)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "hours should be Integer" do
|
39
|
+
subject.hours.should be_kind_of(Integer)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "minutes should be Integer" do
|
43
|
+
subject.minutes.should be_kind_of(Integer)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "seconds should be Integer" do
|
47
|
+
subject.seconds.should be_kind_of(Integer)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "nano_seconds should be Integer" do
|
51
|
+
subject.nano_seconds.should be_kind_of(Integer)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "new with argument hours" do
|
55
|
+
RelativeTime.new(10).hours.should == 10
|
56
|
+
rt = RelativeTime.new(20)
|
57
|
+
rt.hours.should == 20
|
58
|
+
rt.minutes.should == 0
|
59
|
+
rt.seconds.should == 0
|
60
|
+
rt.nano_seconds.should == 0
|
61
|
+
end
|
62
|
+
|
63
|
+
it "new with argument minutes" do
|
64
|
+
rt = RelativeTime.new(10,25)
|
65
|
+
rt.minutes.should == 25
|
66
|
+
rt.seconds.should == 0
|
67
|
+
rt.nano_seconds.should == 0
|
68
|
+
end
|
69
|
+
|
70
|
+
it "new with argument seconds" do
|
71
|
+
rt = RelativeTime.new(10,37,45)
|
72
|
+
rt.seconds.should == 45
|
73
|
+
rt.nano_seconds.should == 0
|
74
|
+
end
|
75
|
+
|
76
|
+
it "new with argument nano_seconds" do
|
77
|
+
rt = RelativeTime.new(0,23,45,457834889)
|
78
|
+
rt.nano_seconds.should == 457834889
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
context "overflow / underflow " do
|
84
|
+
|
85
|
+
it "normalizes on overflow of hours" do
|
86
|
+
rt = RelativeTime.new(24,1,2,3)
|
87
|
+
rt.hours.should == 0
|
88
|
+
rt.minutes.should == 1
|
89
|
+
rt.seconds.should == 2
|
90
|
+
rt.nano_seconds.should == 3
|
91
|
+
end
|
92
|
+
|
93
|
+
it "normalizes on underflow of hours" do
|
94
|
+
rt = RelativeTime.new(-3,1,2,3)
|
95
|
+
rt.hours.should == 21
|
96
|
+
rt.minutes.should == 1
|
97
|
+
rt.seconds.should == 2
|
98
|
+
rt.nano_seconds.should == 3
|
99
|
+
end
|
100
|
+
|
101
|
+
it "normalizes on overflow of minutes" do
|
102
|
+
rt = RelativeTime.new(23,65,2,3)
|
103
|
+
rt.hours.should == 0
|
104
|
+
rt.minutes.should == 5
|
105
|
+
rt.seconds.should == 2
|
106
|
+
rt.nano_seconds.should == 3
|
107
|
+
end
|
108
|
+
|
109
|
+
it "normalizes on underflow of minutes" do
|
110
|
+
rt = RelativeTime.new(1,-100,2,3)
|
111
|
+
rt.hours.should == 23
|
112
|
+
rt.minutes.should == 20
|
113
|
+
rt.seconds.should == 2
|
114
|
+
rt.nano_seconds.should == 3
|
115
|
+
end
|
116
|
+
|
117
|
+
it "normalizes on overflow of nano_seconds" do
|
118
|
+
rt = RelativeTime.new(22,55,2,3600000000)
|
119
|
+
rt.hours.should == 22
|
120
|
+
rt.minutes.should == 55
|
121
|
+
rt.seconds.should == 5
|
122
|
+
rt.nano_seconds.should == 600000000
|
123
|
+
end
|
124
|
+
|
125
|
+
it "normalizes on underflow of nano_seconds" do
|
126
|
+
rt = RelativeTime.new(22,55,2,-3600000000)
|
127
|
+
rt.hours.should == 22
|
128
|
+
rt.minutes.should == 54
|
129
|
+
rt.seconds.should == 58
|
130
|
+
rt.nano_seconds.should == 400000000
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
context "output" do
|
136
|
+
|
137
|
+
it "does HH:MM:SS for to_s" do
|
138
|
+
subject.to_s.should match(/\d{2}:\d{2}:\d{2}/)
|
139
|
+
end
|
140
|
+
|
141
|
+
it "does correct hours, minutes, seconds for to_s" do
|
142
|
+
rt = RelativeTime.new(0,23,45,457834889)
|
143
|
+
rt.to_s.should == "00:23:45"
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'relativity'
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: relativity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Peter Vandenabeele
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &83683860 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.7'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *83683860
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bundler
|
27
|
+
requirement: &83683550 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *83683550
|
36
|
+
description: time and time_ranges relative to day, week, month, quarter etc.
|
37
|
+
email:
|
38
|
+
- peter@vandenabeele.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- .rvmrc
|
45
|
+
- Gemfile
|
46
|
+
- README.rdoc
|
47
|
+
- Rakefile
|
48
|
+
- lib/relativity.rb
|
49
|
+
- lib/relativity/relative_time.rb
|
50
|
+
- lib/relativity/version.rb
|
51
|
+
- relativity.gemspec
|
52
|
+
- spec/relative_time_spec.rb
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
homepage: https://github.com/petervandenabeele/relativity
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.10
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: time and time_ranges relative to day, week, month, quarter etc.
|
78
|
+
test_files: []
|