mmb-seasonal 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/seasonal.rb CHANGED
@@ -37,6 +37,36 @@ module Seasonal
37
37
  end
38
38
  end
39
39
 
40
+ def has_year(s)
41
+ p = Time.parse(s)
42
+ p.eql?(Time.parse(s + ' ' + (p.year + 1).to_s))
43
+ end
44
+
45
+ def is_yearly_range
46
+ !start.nil? and !has_year(start) and !ennd.nil? and !has_year(ennd)
47
+ end
48
+
49
+ def change_year(time, new_year)
50
+ Time.utc(new_year, time.utc.month, time.utc.day, time.utc.hour,
51
+ time.utc.min, time.utc.sec, time.utc.usec)
52
+ end
53
+
54
+ def going_on_yearly_range(test_time)
55
+ if is_yearly_range
56
+ start_shifted = change_year(start_utc, test_time.utc.year)
57
+ end_shifted = change_year(end_utc, test_time.utc.year)
58
+ if (end_utc < start_utc)
59
+ start_shifted_prev = change_year(start_shifted,
60
+ start_shifted.year - 1)
61
+ end_shifted_next = change_year(end_shifted, end_shifted.year + 1)
62
+ ((start_shifted_prev <= test_time and test_time <= end_shifted) or
63
+ (start_shifted <= test_time and test_time <= end_shifted_next))
64
+ else
65
+ (start_shifted <= test_time and test_time <= end_shifted)
66
+ end
67
+ end
68
+ end
69
+
40
70
  def going_on?(test_time=Time.now)
41
71
  # puts "#{start_utc} - #{end_utc}"
42
72
  if start_utc.nil?
@@ -49,7 +79,10 @@ module Seasonal
49
79
  if end_utc.nil?
50
80
  result = test_time >= start_utc
51
81
  else
52
- result = (test_time >= start_utc and test_time <= end_utc)
82
+ result = going_on_yearly_range(test_time)
83
+ if result.nil?
84
+ result = (start_utc <= test_time and test_time <= end_utc)
85
+ end
53
86
  end
54
87
  end
55
88
  result
data/seasonal.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "seasonal"
3
- s.version = "0.0.6"
4
- s.date = "2009-01-14"
3
+ s.version = "0.0.7"
4
+ s.date = "2009-01-15"
5
5
  s.summary = "create logic based on defined date and time events"
6
6
  s.email = "matthewm@boedicker.org"
7
7
  s.homepage = "http://github.com/mmb/seasonal"
@@ -6,46 +6,77 @@ require 'test/unit'
6
6
 
7
7
  class SeasonalTest < Test::Unit::TestCase
8
8
 
9
+ def setup
10
+ @zone = 'America/New_York'
11
+ @tz = TZInfo::Timezone.get(@zone)
12
+ end
13
+
9
14
  def test_event_none
10
15
  e = Seasonal::Event.new(nil, 'America/New_York')
11
16
 
12
- assert_equal(false, e.going_on?)
17
+ assert(!e.going_on?)
13
18
  end
14
19
 
15
20
  def test_event_start
16
21
  ss = 'jan 2'
17
- start = Time.parse(ss).utc
22
+ start = @tz.local_to_utc(Time.parse(ss))
18
23
 
19
- e = Seasonal::Event.new(nil, 'America/New_York', :start => ss)
24
+ e = Seasonal::Event.new(nil, @zone, :start => ss)
20
25
 
21
- assert_equal(false, e.going_on?(start - 1))
26
+ assert(!e.going_on?(start - 1))
22
27
  assert(e.going_on?(start))
23
28
  assert(e.going_on?(start + 1))
24
29
  end
25
30
 
26
31
  def test_event_end
27
32
  es = 'mar 17'
28
- ennd = Time.parse(es).utc
33
+ ennd = @tz.local_to_utc(Time.parse(es))
29
34
 
30
- e = Seasonal::Event.new(nil, 'America/New_York', :end => es)
35
+ e = Seasonal::Event.new(nil, @zone, :end => es)
31
36
 
32
37
  assert(e.going_on?(ennd - 1))
33
38
  assert(e.going_on?(ennd))
34
- assert_equal(false, e.going_on?(ennd + 1))
39
+ assert(!e.going_on?(ennd + 1))
35
40
  end
36
41
 
37
42
  def test_event_start_end
38
43
  ss = 'aug 29'
39
44
  es = 'sep 1'
40
- start = Time.parse(ss).utc
41
- ennd = Time.parse(es).utc
45
+ start = @tz.local_to_utc(Time.parse(ss))
46
+ ennd = @tz.local_to_utc(Time.parse(es))
47
+
48
+ e = Seasonal::Event.new(nil, @zone, :start => ss, :end => es)
49
+
50
+ assert(!e.going_on?(start - 1))
51
+ assert(e.going_on?(start))
52
+ assert(e.going_on?(ennd))
53
+ assert(!e.going_on?(ennd + 1))
54
+ end
55
+
56
+ def test_event_start_end_crosses_year_boundary
57
+ ss = 'dec 31'
58
+ es = 'jan 1'
59
+ start = @tz.local_to_utc(Time.parse(ss))
60
+ ennd = @tz.local_to_utc(Time.parse(es))
42
61
 
43
- e = Seasonal::Event.new(nil, 'America/New_York', :start => ss, :end => es)
62
+ e = Seasonal::Event.new(nil, @zone, :start => ss, :end => es)
44
63
 
45
- assert_equal(false, e.going_on?(start - 1))
64
+ assert(!e.going_on?(start - 1))
46
65
  assert(e.going_on?(start))
47
66
  assert(e.going_on?(ennd))
48
- assert_equal(false, e.going_on?(ennd + 1))
67
+ assert(!e.going_on?(ennd + 1))
68
+ end
69
+
70
+ def test_event_start_end_crosses_year_boundary_other_year
71
+ e = Seasonal::Event.new(nil, @zone, :start => 'nov 6', :end => 'feb 28')
72
+
73
+ assert(!e.going_on?(Time.parse('10/15/1978')))
74
+ assert(e.going_on?(Time.parse('12/25/1979')))
75
+ assert(!e.going_on?(Time.parse('5/14/1980')))
76
+
77
+ assert(!e.going_on?(Time.parse('8/1/2012')))
78
+ assert(e.going_on?(Time.parse('11/26/2015')))
79
+ assert(!e.going_on?(Time.parse('4/1/2020')))
49
80
  end
50
81
 
51
82
  def test_calendar_going_on
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mmb-seasonal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew M. Boedicker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-14 00:00:00 -08:00
12
+ date: 2009-01-15 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency