relativity 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/Changelog.rdoc +6 -0
- data/README.rdoc +14 -3
- data/TODO +72 -0
- data/lib/relativity/day_time/comparison.rb +7 -0
- data/lib/relativity/day_time/conversions.rb +35 -0
- data/lib/relativity/day_time/new.rb +42 -0
- data/lib/relativity/day_time.rb +8 -45
- data/lib/relativity/day_time_range.rb +29 -0
- data/lib/relativity/exception.rb +6 -1
- data/lib/relativity/version.rb +1 -1
- data/lib/relativity/week_time.rb +29 -0
- data/lib/relativity.rb +1 -0
- data/relativity.gemspec +5 -5
- data/spec/day_time/comparison_spec.rb +23 -0
- data/spec/day_time/conversions_spec.rb +41 -0
- data/spec/day_time/new_spec.rb +76 -0
- data/spec/day_time_range/new_spec.rb +25 -0
- data/spec/day_time_range/parsing_spec.rb +24 -0
- data/spec/week_time/week_time_spec.rb +23 -0
- metadata +15 -39
- data/spec/day_time/day_time_spec.rb +0 -79
- data/spec/day_time_range/day_time_range_spec.rb +0 -9
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Changelog.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -4,14 +4,17 @@ Time, Date and DateTime classes don't have a mode for working with relative
|
|
4
4
|
time inside 1 day (or 1 week, 1 month, 1 quarter, etc.).
|
5
5
|
|
6
6
|
A relative time object, relative to a day or a week, is useful to describe
|
7
|
-
e.g. opening hours of a business.
|
8
|
-
|
7
|
+
e.g. opening hours of a business. Time ranges are built on top, so the
|
8
|
+
ranges of opening hours can be represented.
|
9
9
|
|
10
10
|
== Example
|
11
11
|
|
12
12
|
require 'relativity'
|
13
13
|
opens_at = DayTime.new(9) #=> 09:00:00
|
14
|
-
closes_at = DayTime.new(
|
14
|
+
closes_at = DayTime.new(12,30) #=> 12:30:00
|
15
|
+
|
16
|
+
opens_at = DayTime.new("8:35") #=> 08:35:00
|
17
|
+
closes_at = DayTime.new("17:59:30") #=> 17:59:30
|
15
18
|
|
16
19
|
dt = DayTime.new #=> 21:17:40
|
17
20
|
dt.seconds_since_midnight #=> #<BigDecimal:9d35d48,'0.7666056825 7238E5',18(45)>
|
@@ -19,3 +22,11 @@ be built on top, so the ranges of opening hours can be represented.
|
|
19
22
|
dt.minutes #=> 17
|
20
23
|
dt.seconds #=> 40
|
21
24
|
dt.nano_seconds #=> 568257238
|
25
|
+
|
26
|
+
dtr = DayTimeRange.new("8 until 12:30")
|
27
|
+
# => #<DayTimeRange:0x9bfa4ec @start_day_time=08:00:00, @end_day_time=12:30:00>
|
28
|
+
dtr.start # => 08:00:00
|
29
|
+
dtr.end # => 12:30:00
|
30
|
+
|
31
|
+
night_shift = DayTimeRange.new("21:45..06:05", :separator => '..')
|
32
|
+
# => #<DayTimeRange:0x9b997dc @start_day_time=21:45:00, @end_day_time=06:05:00>
|
data/TODO
CHANGED
@@ -10,3 +10,75 @@ week_time_range
|
|
10
10
|
week_day_range
|
11
11
|
|
12
12
|
ActiveRecord coupling (Aggregation ?)
|
13
|
+
|
14
|
+
=====================================
|
15
|
+
A discussion I sent on 2012-01-12 on ruby-talk:
|
16
|
+
|
17
|
+
Also, in version 0.0.3 that is now online, I changed the concept more
|
18
|
+
fundamentally to #{"period"Time} and #{"period"Day}:
|
19
|
+
|
20
|
+
* DayTime (a time inside a "cycle" of 1 day, think %)
|
21
|
+
* planning also this
|
22
|
+
* WeekTime (a time inside a "cycle" of 1 week, no start_of_week needed, just time + day names)
|
23
|
+
* WeekDay (a day inside a "cycle" of 1 week, no start_of_week (cyclical), just a day names)
|
24
|
+
* QuarterDay etc.
|
25
|
+
* DayTimeRange
|
26
|
+
* WeekTimeRange
|
27
|
+
* WeekDayRange
|
28
|
+
etc.
|
29
|
+
|
30
|
+
Will be really useful to code things like:
|
31
|
+
|
32
|
+
bar.opening_hours = WeekTimeMultiRange.new <<
|
33
|
+
WeekTimeRange.new("Monday 09:00 until Monday 18:00") <<
|
34
|
+
WeekTimeRange.new("Tuesday 13:00 until Tuesday 21:30") <<
|
35
|
+
WeekTimeRange.new(WeekTime.new("Wednesday 19:00"), WeekTime.new("Thursday 02:00")) <<
|
36
|
+
WeekTimeRange.new("Thursday 22:00 until Friday 02:00") <<
|
37
|
+
WeekTimeRange.new("Sunday 22:00 until Monday 02:00") # seeming "overflow" but no issue since cyclical
|
38
|
+
|
39
|
+
if bar.opening_hours.include?(Time.now)
|
40
|
+
display_open
|
41
|
+
end
|
42
|
+
|
43
|
+
workdays = WeekDayRange.new("Monday until Friday")
|
44
|
+
|
45
|
+
if workdays.include?(Time.now)
|
46
|
+
go_to_work
|
47
|
+
end
|
48
|
+
|
49
|
+
Also, what I really want to do is an abstract presentation of
|
50
|
+
the "last day of the month" (independent of the which month
|
51
|
+
we are actually in, just the abstract concept).
|
52
|
+
|
53
|
+
first_day = MonthDay.new(:first)
|
54
|
+
first_day = MonthDay.new(1) # in days, the first is 1 (not 0) ... Pascal anyone ;-)
|
55
|
+
|
56
|
+
last_day = MonthDay.new(:last)
|
57
|
+
last_day = MonthDay.new(0) # logically, the last one then becomes 0 in a cyclic counter
|
58
|
+
before_last_day = MonthDay.new(-1) # E.g. 30 Dec is a before_last_day
|
59
|
+
pay_day = MonthDay.new(-5) # E.g. 26 Jan would be pay_day in monthly scheme
|
60
|
+
|
61
|
+
if pay_day.today?
|
62
|
+
prepare_paycheck
|
63
|
+
end
|
64
|
+
|
65
|
+
Similar:
|
66
|
+
|
67
|
+
reporting_day = QuarterDay.new(+15)
|
68
|
+
blocked_period = QuarterDayRange(reporting_day-28, reporting_day+1)
|
69
|
+
|
70
|
+
if reporting_day.today?
|
71
|
+
publish_quarterly_reports
|
72
|
+
end
|
73
|
+
|
74
|
+
if blocked_period.today?
|
75
|
+
forbid_employee_stock_transactions
|
76
|
+
end
|
77
|
+
|
78
|
+
I know ActiveSupport has support for certain aspects, but I dont think it is
|
79
|
+
as abstract as I want it. IIRC , ActiveSupport works on actual Date instances
|
80
|
+
(which are not "relative" but anchored to a specific absolute time or date).
|
81
|
+
I did not see this abstract concept of the "last day of the month" or
|
82
|
+
"a Friday in a random week" fully implemented.
|
83
|
+
|
84
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module DayTime::Conversions
|
2
|
+
|
3
|
+
def hours
|
4
|
+
hours = Integer(@seconds_since_midnight/3600).tap do |h|
|
5
|
+
raise Relativity::InternalError, "hours is out of bound : #{h}" if (h < 0 || h > 23)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def minutes
|
10
|
+
Integer(@seconds_since_midnight / 60) % 60
|
11
|
+
end
|
12
|
+
|
13
|
+
def seconds
|
14
|
+
Integer(@seconds_since_midnight) % 60
|
15
|
+
end
|
16
|
+
|
17
|
+
def nano_seconds
|
18
|
+
Integer((@seconds_since_midnight % 1) * 1000000000)
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
[hours, minutes, seconds].map{|e| rjust_2_0(e.to_s)}.join(self.class.separator)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.separator
|
26
|
+
':'
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def rjust_2_0(s)
|
32
|
+
s.rjust(2,'0')
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module DayTime::New
|
2
|
+
|
3
|
+
attr_reader :seconds_since_midnight
|
4
|
+
|
5
|
+
def initialize(first= nil, minutes = nil, seconds = nil, nano_seconds = nil)
|
6
|
+
super()
|
7
|
+
case first
|
8
|
+
when Integer
|
9
|
+
hh = first #hours
|
10
|
+
mm = minutes || 0
|
11
|
+
ss = seconds || 0
|
12
|
+
nn = nano_seconds || 0
|
13
|
+
when String
|
14
|
+
hh, mm, ss = hh_mm_ss_from_string(first)
|
15
|
+
nn = 0
|
16
|
+
else
|
17
|
+
t = Time.new
|
18
|
+
hh = t.hour
|
19
|
+
mm = t.min
|
20
|
+
ss = t.sec
|
21
|
+
nn = t.nsec
|
22
|
+
end
|
23
|
+
@seconds_since_midnight = ((hh * 3600) + (mm * 60) + ss + BigDecimal(nn)/BigDecimal(1000000000))%(24*3600)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def hh_mm_ss_from_string(input)
|
29
|
+
separator = self.class.separator
|
30
|
+
separator = Regexp.escape(separator) # e.g. separator is '.'
|
31
|
+
input.strip!
|
32
|
+
match_hh = '(?<hh>\d\d?)'
|
33
|
+
match_mm = '(' + separator + '(?<mm>\d\d?))?'
|
34
|
+
match_ss = '(' + separator + '(?<ss>\d\d?))?'
|
35
|
+
matcher = '\A' + match_hh + match_mm + match_ss + '\Z'
|
36
|
+
r = Regexp.new(matcher)
|
37
|
+
matchdata = r.match(input)
|
38
|
+
raise Relativity::InvalidFormat if matchdata.nil?
|
39
|
+
return [matchdata[:hh].to_i, matchdata[:mm].to_i, matchdata[:ss].to_i]
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/lib/relativity/day_time.rb
CHANGED
@@ -1,52 +1,15 @@
|
|
1
1
|
class DayTime
|
2
2
|
|
3
3
|
require 'bigdecimal'
|
4
|
+
require 'relativity/day_time/new'
|
5
|
+
require 'relativity/day_time/conversions'
|
6
|
+
require 'relativity/day_time/comparison'
|
7
|
+
include DayTime::Comparison
|
8
|
+
include DayTime::New
|
9
|
+
include DayTime::Conversions
|
4
10
|
|
5
|
-
|
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 Relativity::InternalError, "hours is out of bound : #{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
|
11
|
+
def self.separator
|
12
|
+
':'
|
36
13
|
end
|
37
14
|
|
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
15
|
end
|
@@ -2,5 +2,34 @@ class DayTimeRange
|
|
2
2
|
|
3
3
|
def initialize(first, second = nil)
|
4
4
|
super()
|
5
|
+
@start_day_time, @end_day_time =
|
6
|
+
case first
|
7
|
+
when String
|
8
|
+
start_end_from_string(first, second)
|
9
|
+
end
|
5
10
|
end
|
11
|
+
|
12
|
+
def start
|
13
|
+
@start_day_time
|
14
|
+
end
|
15
|
+
|
16
|
+
def end
|
17
|
+
@end_day_time
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.separator
|
21
|
+
" until "
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def start_end_from_string(input, options)
|
27
|
+
separator = (options && options[:separator]) || self.class.separator
|
28
|
+
separator = Regexp.escape(separator)
|
29
|
+
matcher = '\A(?<start>.+)' + separator + '(?<end>.+)\Z'
|
30
|
+
r = Regexp.new(matcher)
|
31
|
+
matchdata = r.match(input)
|
32
|
+
return [DayTime.new(matchdata[:start]), DayTime.new(matchdata[:end])]
|
33
|
+
end
|
34
|
+
|
6
35
|
end
|
data/lib/relativity/exception.rb
CHANGED
data/lib/relativity/version.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
class WeekTime
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
require 'time'
|
5
|
+
|
6
|
+
attr_reader :seconds_since_sunday_midnight
|
7
|
+
|
8
|
+
def initialize(week_day_string=nil, hours = nil, minutes = nil, seconds = nil, nano_seconds = nil)
|
9
|
+
super()
|
10
|
+
if week_day_string
|
11
|
+
# week_day_d =
|
12
|
+
# hh = hours.to_i
|
13
|
+
# mm = minutes.to_i
|
14
|
+
# ss = seconds.to_i
|
15
|
+
# nn = nano_seconds.to_i
|
16
|
+
else
|
17
|
+
t = Time.new
|
18
|
+
week_day_d = t.wday
|
19
|
+
hh = t.hour
|
20
|
+
mm = t.min
|
21
|
+
ss = t.sec
|
22
|
+
nn = t.nsec
|
23
|
+
end
|
24
|
+
|
25
|
+
seconds_since_midnight = ((hh * 3600) + (mm * 60) + ss + BigDecimal(nn)/BigDecimal(1000000000))%(24*3600)
|
26
|
+
@seconds_since_sunday_midnight = week_day_d * 24 * 3600 + seconds_since_midnight
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/relativity.rb
CHANGED
data/relativity.gemspec
CHANGED
@@ -18,9 +18,9 @@ Gem::Specification.new do |s|
|
|
18
18
|
|
19
19
|
# specify any dependencies here; for example:
|
20
20
|
s.add_development_dependency 'rspec', '>= 2.7'
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
# this will only work on Linux, so commented out by default
|
22
|
+
# for Mac use an alternative strategy
|
23
|
+
# s.add_development_dependency 'guard-rspec', '>= 0.5'
|
24
|
+
# s.add_development_dependency 'rb-inotify'
|
25
|
+
# s.add_development_dependency 'libnotify'
|
26
26
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DayTime do
|
4
|
+
|
5
|
+
it "== reports exact equality for hours" do
|
6
|
+
dt_1 = DayTime.new(8)
|
7
|
+
dt_2 = DayTime.new(8)
|
8
|
+
dt_1.should == dt_2
|
9
|
+
end
|
10
|
+
|
11
|
+
it "== reports difference when different" do
|
12
|
+
dt_1 = DayTime.new(8)
|
13
|
+
dt_2 = DayTime.new(8, 0, 0, 1) # 1 nanosecond off
|
14
|
+
dt_1.should_not == dt_2
|
15
|
+
end
|
16
|
+
|
17
|
+
it "== reports exact equality for all arguments" do
|
18
|
+
dt_1 = DayTime.new(0,23,45,457834889)
|
19
|
+
dt_2 = DayTime.new(0,23,45,457834889)
|
20
|
+
dt_1.should == dt_2
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DayTime do
|
4
|
+
|
5
|
+
it "hours" do
|
6
|
+
(((subject.hours - Time.new.hour).abs+1)%24).should be <= 2
|
7
|
+
end
|
8
|
+
|
9
|
+
it "hours should be Integer" do
|
10
|
+
subject.hours.should be_kind_of(Integer)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "minutes" do
|
14
|
+
(((subject.minutes - Time.new.min).abs+1)%60).should be <= 2
|
15
|
+
end
|
16
|
+
|
17
|
+
it "minutes should be Integer" do
|
18
|
+
subject.minutes.should be_kind_of(Integer)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "seconds" do
|
22
|
+
(((subject.seconds - Time.new.sec).abs+2)%60).should be <= 4
|
23
|
+
end
|
24
|
+
|
25
|
+
it "seconds should be Integer" do
|
26
|
+
subject.seconds.should be_kind_of(Integer)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "nano_seconds should be Integer" do
|
30
|
+
subject.nano_seconds.should be_kind_of(Integer)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "seconds_since_midnight" do
|
34
|
+
lambda { subject.seconds_since_midnight }.should_not raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
it "seconds_since_midnight should be BigDecimal" do
|
38
|
+
subject.seconds_since_midnight.should be_kind_of(BigDecimal)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DayTime do
|
4
|
+
|
5
|
+
it "new builds a DayTime" do
|
6
|
+
lambda { subject }.should_not raise_error
|
7
|
+
end
|
8
|
+
|
9
|
+
it "new creates a DayTime close to now" do
|
10
|
+
dt_s = subject.seconds_since_midnight
|
11
|
+
t = Time.new
|
12
|
+
t_s = (t.hour*60 + t.min)*60 + t.sec + t.nsec/1E9 # seconds_since_midnight from Time
|
13
|
+
(((t_s-dt_s).abs+2)%(24*60*60)).should be <= 4
|
14
|
+
end
|
15
|
+
|
16
|
+
it "new with argument hours" do
|
17
|
+
dt = DayTime.new(20)
|
18
|
+
dt.hours.should == 20
|
19
|
+
dt.minutes.should == 0
|
20
|
+
dt.seconds.should == 0
|
21
|
+
dt.nano_seconds.should == 0
|
22
|
+
end
|
23
|
+
|
24
|
+
it "new with argument minutes" do
|
25
|
+
dt = DayTime.new(10,25)
|
26
|
+
dt.minutes.should == 25
|
27
|
+
dt.seconds.should == 0
|
28
|
+
dt.nano_seconds.should == 0
|
29
|
+
end
|
30
|
+
|
31
|
+
it "new with argument seconds" do
|
32
|
+
dt = DayTime.new(10,37,45)
|
33
|
+
dt.seconds.should == 45
|
34
|
+
dt.nano_seconds.should == 0
|
35
|
+
end
|
36
|
+
|
37
|
+
it "new with argument nano_seconds" do
|
38
|
+
dt = DayTime.new(0,23,45,457834889)
|
39
|
+
dt.nano_seconds.should == 457834889
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'new with string argument "8"' do
|
43
|
+
dt = DayTime.new("8")
|
44
|
+
dt.hours.should == 8
|
45
|
+
dt.minutes.should == 0
|
46
|
+
dt.seconds.should == 0
|
47
|
+
dt.nano_seconds.should == 0
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'new with string argument "8:35"' do
|
51
|
+
dt = DayTime.new("8:35")
|
52
|
+
dt.hours.should == 8
|
53
|
+
dt.minutes.should == 35
|
54
|
+
dt.seconds.should == 0
|
55
|
+
dt.nano_seconds.should == 0
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'new with string argument "8:35:30"' do
|
59
|
+
dt = DayTime.new("8:35:30")
|
60
|
+
dt.hours.should == 8
|
61
|
+
dt.minutes.should == 35
|
62
|
+
dt.seconds.should == 30
|
63
|
+
dt.nano_seconds.should == 0
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'new with string does implicit strip' do
|
67
|
+
dt = DayTime.new(" 8:35:30 ")
|
68
|
+
dt.hours.should == 8
|
69
|
+
dt.minutes.should == 35
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'new with string rejects too much separators' do
|
73
|
+
lambda { DayTime.new("8:35:30:45") }.should raise_error Relativity::InvalidFormat
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DayTimeRange do
|
4
|
+
|
5
|
+
it "builds new with 1 argument" do
|
6
|
+
lambda { DayTimeRange.new("8 until 11") }.should_not raise_error
|
7
|
+
end
|
8
|
+
|
9
|
+
it "start day_time is correct" do
|
10
|
+
dtr = DayTimeRange.new("8 until 11")
|
11
|
+
dtr.start.should == DayTime.new(8)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "end day_time is correct" do
|
15
|
+
dtr = DayTimeRange.new("8 until 11")
|
16
|
+
dtr.end.should == DayTime.new(11)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "day_times are correct with complex time" do
|
20
|
+
dtr = DayTimeRange.new("8:35 until 17:59:30")
|
21
|
+
dtr.start.should == DayTime.new(8,35)
|
22
|
+
dtr.end.should == DayTime.new(17,59,30)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DayTimeRange do
|
4
|
+
|
5
|
+
it "default separator is ' until '" do
|
6
|
+
DayTimeRange.separator.should == ' until '
|
7
|
+
end
|
8
|
+
|
9
|
+
it "start time is correct" do
|
10
|
+
dtr = DayTimeRange.new("8 until 11", :separator => " until ")
|
11
|
+
dtr.start.should == DayTime.new(8)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "start time is correct" do
|
15
|
+
dtr = DayTimeRange.new("8 - 11", :separator => " - ")
|
16
|
+
dtr.start.should == DayTime.new(8)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "start time is correct" do
|
20
|
+
dtr = DayTimeRange.new("8..11", :separator => "..")
|
21
|
+
dtr.start.should == DayTime.new(8)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WeekTime do
|
4
|
+
|
5
|
+
it "new builds a WeekTime" do
|
6
|
+
lambda { subject }.should_not raise_error
|
7
|
+
end
|
8
|
+
|
9
|
+
it "seconds_since_sunday_midnight" do
|
10
|
+
lambda { subject.seconds_since_sunday_midnight }.should_not raise_error
|
11
|
+
end
|
12
|
+
|
13
|
+
it "new creates a WeekTime close to now" do
|
14
|
+
wt_s = subject.seconds_since_sunday_midnight
|
15
|
+
t = Time.new
|
16
|
+
day_time_s = (t.hour*60 + t.min)*60 + t.sec + t.nsec/1E9
|
17
|
+
week_day_d = t.wday #0..6 0 is Sunday
|
18
|
+
week_day_s = week_day_d * 24 * 3600
|
19
|
+
week_time_s = day_time_s + week_day_s
|
20
|
+
(((week_time_s - wt_s).abs+2)%(7*24*3600)).should be <= 4
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relativity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &78729730 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,40 +21,7 @@ dependencies:
|
|
21
21
|
version: '2.7'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: guard-rspec
|
27
|
-
requirement: &81173310 !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
29
|
-
requirements:
|
30
|
-
- - ! '>='
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '0.5'
|
33
|
-
type: :development
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *81173310
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: rb-inotify
|
38
|
-
requirement: &81171830 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ! '>='
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '0'
|
44
|
-
type: :development
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: *81171830
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: libnotify
|
49
|
-
requirement: &81170730 !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
|
-
requirements:
|
52
|
-
- - ! '>='
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
type: :development
|
56
|
-
prerelease: false
|
57
|
-
version_requirements: *81170730
|
24
|
+
version_requirements: *78729730
|
58
25
|
description: time and time_ranges relative to day, week, month, quarter etc.
|
59
26
|
email:
|
60
27
|
- peter@vandenabeele.com
|
@@ -63,6 +30,7 @@ extensions: []
|
|
63
30
|
extra_rdoc_files: []
|
64
31
|
files:
|
65
32
|
- .gitignore
|
33
|
+
- .rspec
|
66
34
|
- .rvmrc
|
67
35
|
- Changelog.rdoc
|
68
36
|
- Gemfile
|
@@ -73,16 +41,24 @@ files:
|
|
73
41
|
- TODO
|
74
42
|
- lib/relativity.rb
|
75
43
|
- lib/relativity/day_time.rb
|
44
|
+
- lib/relativity/day_time/comparison.rb
|
45
|
+
- lib/relativity/day_time/conversions.rb
|
46
|
+
- lib/relativity/day_time/new.rb
|
76
47
|
- lib/relativity/day_time_range.rb
|
77
48
|
- lib/relativity/exception.rb
|
78
49
|
- lib/relativity/version.rb
|
50
|
+
- lib/relativity/week_time.rb
|
79
51
|
- relativity.gemspec
|
80
|
-
- spec/day_time/
|
52
|
+
- spec/day_time/comparison_spec.rb
|
53
|
+
- spec/day_time/conversions_spec.rb
|
81
54
|
- spec/day_time/exception_spec.rb
|
55
|
+
- spec/day_time/new_spec.rb
|
82
56
|
- spec/day_time/output_spec.rb
|
83
57
|
- spec/day_time/overflow_spec.rb
|
84
|
-
- spec/day_time_range/
|
58
|
+
- spec/day_time_range/new_spec.rb
|
59
|
+
- spec/day_time_range/parsing_spec.rb
|
85
60
|
- spec/spec_helper.rb
|
61
|
+
- spec/week_time/week_time_spec.rb
|
86
62
|
homepage: https://github.com/petervandenabeele/relativity
|
87
63
|
licenses: []
|
88
64
|
post_install_message:
|
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe DayTime do
|
4
|
-
|
5
|
-
it "new build a DayTime" do
|
6
|
-
lambda { subject }.should_not raise_error
|
7
|
-
end
|
8
|
-
|
9
|
-
it "seconds_since_midnight" do
|
10
|
-
lambda { subject.seconds_since_midnight }.should_not raise_error
|
11
|
-
end
|
12
|
-
|
13
|
-
it "new creates a DayTime close to now" do
|
14
|
-
rt_s = subject.seconds_since_midnight
|
15
|
-
t = Time.new
|
16
|
-
t_s = (t.hour*60 + t.min)*60 + t.sec + t.nsec/1E9 # seconds_since_midnight from Time
|
17
|
-
(((t_s-rt_s).abs+2)%(24*60*60)).should be <= 4
|
18
|
-
end
|
19
|
-
|
20
|
-
it "hours" do
|
21
|
-
(((subject.hours - Time.new.hour).abs+1)%24).should be <= 2
|
22
|
-
end
|
23
|
-
|
24
|
-
it "minutes" do
|
25
|
-
(((subject.minutes - Time.new.min).abs+1)%60).should be <= 2
|
26
|
-
end
|
27
|
-
|
28
|
-
it "seconds" do
|
29
|
-
(((subject.seconds - Time.new.sec).abs+2)%60).should be <= 4
|
30
|
-
end
|
31
|
-
|
32
|
-
it "seconds_since_midnight should be BigDecimal" do
|
33
|
-
subject.seconds_since_midnight.should be_kind_of(BigDecimal)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "hours should be Integer" do
|
37
|
-
subject.hours.should be_kind_of(Integer)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "minutes should be Integer" do
|
41
|
-
subject.minutes.should be_kind_of(Integer)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "seconds should be Integer" do
|
45
|
-
subject.seconds.should be_kind_of(Integer)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "nano_seconds should be Integer" do
|
49
|
-
subject.nano_seconds.should be_kind_of(Integer)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "new with argument hours" do
|
53
|
-
DayTime.new(10).hours.should == 10
|
54
|
-
rt = DayTime.new(20)
|
55
|
-
rt.hours.should == 20
|
56
|
-
rt.minutes.should == 0
|
57
|
-
rt.seconds.should == 0
|
58
|
-
rt.nano_seconds.should == 0
|
59
|
-
end
|
60
|
-
|
61
|
-
it "new with argument minutes" do
|
62
|
-
rt = DayTime.new(10,25)
|
63
|
-
rt.minutes.should == 25
|
64
|
-
rt.seconds.should == 0
|
65
|
-
rt.nano_seconds.should == 0
|
66
|
-
end
|
67
|
-
|
68
|
-
it "new with argument seconds" do
|
69
|
-
rt = DayTime.new(10,37,45)
|
70
|
-
rt.seconds.should == 45
|
71
|
-
rt.nano_seconds.should == 0
|
72
|
-
end
|
73
|
-
|
74
|
-
it "new with argument nano_seconds" do
|
75
|
-
rt = DayTime.new(0,23,45,457834889)
|
76
|
-
rt.nano_seconds.should == 457834889
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|