Class: Chronos::Interval
- Inherits:
-
Object
- Object
- Chronos::Interval
- Defined in:
- lib/chronos/interval.rb,
lib/chronos/interval/gregorian.rb
Overview
An Interval is determinated by a start and an end Datetime. Unlike in Duration, this allows to determine the months part exactly in seconds (and therefore minutes, hours, days, weeks). That opens up the possibility to say how
Direct Known Subclasses
Defined Under Namespace
Classes: Gregorian
Constant Summary collapse
- ValidFixed =
[:begin, :end].freeze
- InspectFixedBegin =
"<%p [%p] - %p, %p>".freeze
- InspectFixedEnd =
"<%p %p - [%p], %p>".freeze
Instance Attribute Summary collapse
-
#begin ⇒ Object
readonly
The smaller of the two datetimes.
-
#end ⇒ Object
readonly
The bigger of the two datetimes.
-
#fixed ⇒ Object
readonly
Which end is fixed, plays a role when adding, subtracting, multiplying, dividing, …
Class Method Summary collapse
-
.between(limit_a, limit_b) ⇒ Object
unlike new, between always creates a positive interval it will switch limit_a and limit_b if limit_a > limit_b it always fixates :begin.
Instance Method Summary collapse
-
#+(duration) ⇒ Object
Enlarges the Interval by duration away from the fixed end.
-
#-(duration) ⇒ Object
Shortens the Interval by duration towards from the fixed end will raise if self < duration.
- #days ⇒ Object
-
#fixed_begin ⇒ Object
Returns the same Interval but with begin as fixpoint for operations.
-
#fixed_end ⇒ Object
Returns the same interval but with end as fixpoint for operations.
- #format(string) ⇒ Object
-
#initialize(limit_a, limit_b, fixed = nil) ⇒ Interval
constructor
- create a new interval that lasts from start_date until end_date === Arguments limit_a
- one of the two limiting datetimes limit_b
- the other of the two limiting datetimes fixated
-
which end to fixate for operations.
- #inspect ⇒ Object
-
#picoseconds ⇒ Object
The number of picoseconds.
-
#to_duration ⇒ Object
converts this interval to a duration if you set as_seconds to true it will convert the month primitive to seconds and use that.
- #to_hash ⇒ Object
- #values_at(*keys) ⇒ Object
Constructor Details
#initialize(limit_a, limit_b, fixed = nil) ⇒ Interval
create a new interval that lasts from start_date until end_date
Arguments
- limit_a
-
one of the two limiting datetimes
- limit_b
-
the other of the two limiting datetimes
- fixated
-
which end to fixate for operations. Defaults to :begin, valid values are (Symbols):
- begin
-
The smaller datetime is fixated
- end
-
The smaller datetime is fixated
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/chronos/interval.rb', line 43 def initialize(limit_a, limit_b, fixed=nil) @fixed = fixed || :begin raise ArgumentError, "limites don't have the same signature" unless (limit_a.time? == limit_b.time? && limit_a.date? == limit_b.date?) raise ArgumentError, "invalid fixed, must be :begin or :end" unless ValidFixed.include?(@fixed) @language = limit_a.language if limit_a > limit_b then @begin = limit_b @end = limit_a @negative = false else @begin = limit_a @end = limit_b @negative = true end overflow = 0 picoseconds = @end.ps_number - @begin.ps_number if @begin.time? days = @end.day_number - @begin.day_number if @begin.date? overflow, picoseconds = *picoseconds.divmod(PS_IN_DAY) if @begin.time? @duration = Duration.new(days+overflow, picoseconds, @language) end |
Instance Attribute Details
#begin ⇒ Object (readonly)
The smaller of the two datetimes
21 22 23 |
# File 'lib/chronos/interval.rb', line 21 def begin @begin end |
#end ⇒ Object (readonly)
The bigger of the two datetimes
24 25 26 |
# File 'lib/chronos/interval.rb', line 24 def end @end end |
#fixed ⇒ Object (readonly)
Which end is fixed, plays a role when adding, subtracting, multiplying, dividing, …
27 28 29 |
# File 'lib/chronos/interval.rb', line 27 def fixed @fixed end |
Class Method Details
.between(limit_a, limit_b) ⇒ Object
unlike new, between always creates a positive interval it will switch limit_a and limit_b if limit_a > limit_b it always fixates :begin
32 33 34 |
# File 'lib/chronos/interval.rb', line 32 def self.between(limit_a, limit_b) limit_a > limit_b ? new(limit_b, limit_a, false, :begin) : new(limit_a, limit_b, false, :begin) end |
Instance Method Details
#+(duration) ⇒ Object
Enlarges the Interval by duration away from the fixed end
78 79 80 81 82 83 84 |
# File 'lib/chronos/interval.rb', line 78 def +(duration) if @fixed == :begin then self.class.new(@begin, @end+duration, @fixed) else self.class.new(@begin-duration, @end, @fixed) end end |
#-(duration) ⇒ Object
Shortens the Interval by duration towards from the fixed end will raise if self < duration
88 89 90 91 92 93 94 |
# File 'lib/chronos/interval.rb', line 88 def -(duration) if @fixed == :begin then self.class.new(@begin, @end-duration, @fixed) else self.class.new(@begin+duration, @end, @fixed) end end |
#days ⇒ Object
101 102 103 |
# File 'lib/chronos/interval.rb', line 101 def days @duration.days end |
#fixed_begin ⇒ Object
Returns the same Interval but with begin as fixpoint for operations
68 69 70 |
# File 'lib/chronos/interval.rb', line 68 def fixed_begin self.class.new(@begin, @end, :begin) end |
#fixed_end ⇒ Object
Returns the same interval but with end as fixpoint for operations
73 74 75 |
# File 'lib/chronos/interval.rb', line 73 def fixed_end self.class.new(@begin, @end, :end) end |
#format(string) ⇒ Object
120 121 122 |
# File 'lib/chronos/interval.rb', line 120 def format(string) raise NoMethodError end |
#inspect ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/chronos/interval.rb', line 124 def inspect if @fixed == :begin then sprintf InspectFixedBegin, self.class, @begin, @end, @duration else sprintf InspectFixedEnd, self.class, @begin, @end, @duration end end |
#picoseconds ⇒ Object
The number of picoseconds
97 98 99 |
# File 'lib/chronos/interval.rb', line 97 def picoseconds @duration.picoseconds end |
#to_duration ⇒ Object
converts this interval to a duration if you set as_seconds to true it will convert the month primitive to seconds and use that
112 113 114 |
# File 'lib/chronos/interval.rb', line 112 def to_duration @duration end |
#to_hash ⇒ Object
116 117 118 |
# File 'lib/chronos/interval.rb', line 116 def to_hash @duration.to_hash.merge(:begin => @begin, :end => @end, :language => @language) end |
#values_at(*keys) ⇒ Object
105 106 107 |
# File 'lib/chronos/interval.rb', line 105 def values_at(*keys) to_hash.values_at(*keys) end |