forked from mojombo/chronic
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
seasons and weekday/weekend support (Edwin Chen)
- Loading branch information
Showing
8 changed files
with
323 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,24 +1,45 @@ | |||
require 'chronic/repeaters/repeater_season.rb' | |||
|
|||
class Chronic::RepeaterSeasonName < Chronic::RepeaterSeason #:nodoc: | class Chronic::RepeaterSeasonName < Chronic::RepeaterSeason #:nodoc: | ||
@summer = ['jul 21', 'sep 22'] | SEASON_SECONDS = 7_862_400 # 91 * 24 * 60 * 60 | ||
@autumn = ['sep 23', 'dec 21'] | DAY_SECONDS = 86_400 # (24 * 60 * 60) | ||
@winter = ['dec 22', 'mar 19'] | |||
@spring = ['mar 20', 'jul 20'] | |||
|
|
||
def next(pointer) | def next(pointer) | ||
super | direction = pointer == :future ? 1 : -1 | ||
raise 'Not implemented' | find_next_season_span(direction, @type) | ||
end | end | ||
|
|
||
def this(pointer = :future) | def this(pointer = :future) | ||
super | # super | ||
raise 'Not implemented' |
|
||
direction = pointer == :future ? 1 : -1 | |||
|
|||
today = Time.construct(@now.year, @now.month, @now.day) | |||
goal_ssn_start = today + direction * num_seconds_til_start(@type, direction) | |||
goal_ssn_end = today + direction * num_seconds_til_end(@type, direction) | |||
curr_ssn = find_current_season(@now.to_minidate) | |||
case pointer | |||
when :past | |||
this_ssn_start = goal_ssn_start | |||
this_ssn_end = (curr_ssn == @type) ? today : goal_ssn_end | |||
when :future | |||
this_ssn_start = (curr_ssn == @type) ? today + Chronic::RepeaterDay::DAY_SECONDS : goal_ssn_start | |||
this_ssn_end = goal_ssn_end | |||
when :none | |||
this_ssn_start = goal_ssn_start | |||
this_ssn_end = goal_ssn_end | |||
end | |||
|
|||
Chronic::Span.new(this_ssn_start, this_ssn_end) | |||
end | end | ||
|
|
||
def width | def offset(span, amount, pointer) | ||
(91 * 24 * 60 * 60) | Chronic::Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer)) | ||
end | end | ||
|
|
||
def to_s | def offset_by(time, amount, pointer) | ||
super << '-season-' << @type.to_s | direction = pointer == :future ? 1 : -1 | ||
time + amount * direction * Chronic::RepeaterYear::YEAR_SECONDS | |||
end | end | ||
|
|||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,72 @@ | |||
class Chronic::RepeaterWeekday < Chronic::Repeater #:nodoc: | |||
WEEK_WEEKDAYS = 5 | |||
DAY_SECONDS = 86400 # (24 * 60 * 60) | |||
|
|||
def next(pointer) | |||
super | |||
|
|||
direction = pointer == :future ? 1 : -1 | |||
|
|||
if !@current_weekday_start | |||
@current_weekday_start = Time.construct(@now.year, @now.month, @now.day) | |||
@current_weekday_start += direction * DAY_SECONDS | |||
|
|||
until is_weekday?(@current_weekday_start.wday) | |||
@current_weekday_start += direction * DAY_SECONDS | |||
end | |||
else | |||
loop do | |||
@current_weekday_start += direction * DAY_SECONDS | |||
break if is_weekday?(@current_weekday_start.wday) | |||
end | |||
end | |||
|
|||
Chronic::Span.new(@current_weekday_start, @current_weekday_start + DAY_SECONDS) | |||
end | |||
|
|||
def this(pointer = :future) | |||
super | |||
|
|||
case pointer | |||
when :past | |||
self.next(:past) | |||
when :future, :none | |||
self.next(:future) | |||
end | |||
end | |||
|
|||
def offset(span, amount, pointer) | |||
direction = pointer == :future ? 1 : -1 | |||
|
|||
num_weekdays_passed = 0; offset = 0 | |||
until num_weekdays_passed == amount | |||
offset += direction * DAY_SECONDS | |||
num_weekdays_passed += 1 if is_weekday?((span.begin+offset).wday) | |||
end | |||
|
|||
span + offset | |||
end | |||
|
|||
def width | |||
DAY_SECONDS | |||
end | |||
|
|||
def to_s | |||
super << '-weekday' | |||
end | |||
|
|||
private | |||
|
|||
def is_weekend?(day) | |||
day == symbol_to_number(:saturday) || day == symbol_to_number(:sunday) | |||
end | |||
|
|||
def is_weekday?(day) | |||
!is_weekend?(day) | |||
end | |||
|
|||
def symbol_to_number(sym) | |||
lookup = {:sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3, :thursday => 4, :friday => 5, :saturday => 6} | |||
lookup[sym] || raise("Invalid symbol specified") | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,56 @@ | |||
require 'lib/chronic' | |||
require 'test/unit' | |||
|
|||
class TestRepeaterWeekday < Test::Unit::TestCase | |||
|
|||
def setup | |||
@now = Time.local(2007, 6, 11, 14, 0, 0, 0) # Mon | |||
end | |||
|
|||
def test_next_future | |||
weekdays = Chronic::RepeaterWeekday.new(:weekday) | |||
weekdays.start = @now | |||
|
|||
next1_weekday = weekdays.next(:future) # Tues | |||
assert_equal Time.local(2007, 6, 12), next1_weekday.begin | |||
assert_equal Time.local(2007, 6, 13), next1_weekday.end | |||
|
|||
next2_weekday = weekdays.next(:future) # Wed | |||
assert_equal Time.local(2007, 6, 13), next2_weekday.begin | |||
assert_equal Time.local(2007, 6, 14), next2_weekday.end | |||
|
|||
next3_weekday = weekdays.next(:future) # Thurs | |||
assert_equal Time.local(2007, 6, 14), next3_weekday.begin | |||
assert_equal Time.local(2007, 6, 15), next3_weekday.end | |||
|
|||
next4_weekday = weekdays.next(:future) # Fri | |||
assert_equal Time.local(2007, 6, 15), next4_weekday.begin | |||
assert_equal Time.local(2007, 6, 16), next4_weekday.end | |||
|
|||
next5_weekday = weekdays.next(:future) # Mon | |||
assert_equal Time.local(2007, 6, 18), next5_weekday.begin | |||
assert_equal Time.local(2007, 6, 19), next5_weekday.end | |||
end | |||
|
|||
def test_next_past | |||
weekdays = Chronic::RepeaterWeekday.new(:weekday) | |||
weekdays.start = @now | |||
|
|||
last1_weekday = weekdays.next(:past) # Fri | |||
assert_equal Time.local(2007, 6, 8), last1_weekday.begin | |||
assert_equal Time.local(2007, 6, 9), last1_weekday.end | |||
|
|||
last2_weekday = weekdays.next(:past) # Thurs | |||
assert_equal Time.local(2007, 6, 7), last2_weekday.begin | |||
assert_equal Time.local(2007, 6, 8), last2_weekday.end | |||
end | |||
|
|||
def test_offset | |||
span = Chronic::Span.new(@now, @now + 1) | |||
|
|||
offset_span = Chronic::RepeaterWeekday.new(:weekday).offset(span, 5, :future) | |||
|
|||
assert_equal Time.local(2007, 6, 18, 14), offset_span.begin | |||
assert_equal Time.local(2007, 6, 18, 14, 0, 1), offset_span.end | |||
end | |||
end |
Oops, something went wrong.