Class Chronos::Interval
In: lib/chronos/interval/gregorian.rb
lib/chronos/interval.rb
Parent: Object

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

Methods

+   -   between   days   fixed_begin   fixed_end   format   inspect   new   picoseconds   to_duration   to_hash   values_at  

Classes and Modules

Class Chronos::Interval::Gregorian

Constants

ValidFixed = [:begin, :end].freeze
InspectFixedBegin = "<%p [%p] - %p, %p>".freeze
InspectFixedEnd = "<%p %p - [%p], %p>".freeze

Attributes

begin  [R]  The smaller of the two datetimes
end  [R]  The bigger of the two datetimes
fixed  [R]  Which end is fixed, plays a role when adding, subtracting, multiplying, dividing, …

Public Class methods

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

[Source]

    # File lib/chronos/interval.rb, line 32
32:     def self.between(limit_a, limit_b)
33:       limit_a > limit_b ? new(limit_b, limit_a, false, :begin) : new(limit_a, limit_b, false, :begin)
34:     end

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

[Source]

    # File lib/chronos/interval.rb, line 43
43:     def initialize(limit_a, limit_b, fixed=nil)
44:       @fixed = fixed || :begin
45:       raise ArgumentError, "limites don't have the same signature" unless (limit_a.time? == limit_b.time? && limit_a.date? == limit_b.date?)
46:       raise ArgumentError, "invalid fixed, must be :begin or :end" unless ValidFixed.include?(@fixed)
47: 
48:       @language = limit_a.language
49: 
50:       if limit_a > limit_b then
51:         @begin    = limit_b
52:         @end      = limit_a
53:         @negative = false
54:       else
55:         @begin    = limit_a
56:         @end      = limit_b
57:         @negative = true
58:       end
59: 
60:       overflow    = 0
61:       picoseconds = @end.ps_number  - @begin.ps_number  if @begin.time?
62:       days        = @end.day_number - @begin.day_number if @begin.date?
63:       overflow, picoseconds = *picoseconds.divmod(PS_IN_DAY) if @begin.time?
64:       @duration = Duration.new(days+overflow, picoseconds, @language)
65:     end

Public Instance methods

Enlarges the Interval by duration away from the fixed end

[Source]

    # File lib/chronos/interval.rb, line 78
78:     def +(duration)
79:       if @fixed == :begin then
80:         self.class.new(@begin, @end+duration, @fixed)
81:       else
82:         self.class.new(@begin-duration, @end, @fixed)
83:       end
84:     end

Shortens the Interval by duration towards from the fixed end will raise if self < duration

[Source]

    # File lib/chronos/interval.rb, line 88
88:     def -(duration)
89:       if @fixed == :begin then
90:         self.class.new(@begin, @end-duration, @fixed)
91:       else
92:         self.class.new(@begin+duration, @end, @fixed)
93:       end
94:     end

[Source]

     # File lib/chronos/interval.rb, line 101
101:     def days
102:       @duration.days
103:     end

Returns the same Interval but with begin as fixpoint for operations

[Source]

    # File lib/chronos/interval.rb, line 68
68:     def fixed_begin
69:       self.class.new(@begin, @end, :begin)
70:     end

Returns the same interval but with end as fixpoint for operations

[Source]

    # File lib/chronos/interval.rb, line 73
73:     def fixed_end
74:       self.class.new(@begin, @end, :end)
75:     end

[Source]

     # File lib/chronos/interval.rb, line 120
120:     def format(string)
121:       raise NoMethodError
122:     end

[Source]

     # File lib/chronos/interval.rb, line 124
124:     def inspect
125:       if @fixed == :begin then
126:         sprintf InspectFixedBegin, self.class, @begin, @end, @duration
127:       else
128:         sprintf InspectFixedEnd, self.class, @begin, @end, @duration
129:       end
130:     end

The number of picoseconds

[Source]

    # File lib/chronos/interval.rb, line 97
97:     def picoseconds
98:       @duration.picoseconds
99:     end

converts this interval to a duration if you set as_seconds to true it will convert the month primitive to seconds and use that

[Source]

     # File lib/chronos/interval.rb, line 112
112:     def to_duration
113:       @duration
114:     end

[Source]

     # File lib/chronos/interval.rb, line 116
116:     def to_hash
117:       @duration.to_hash.merge(:begin => @begin, :end => @end, :language => @language)
118:     end

[Source]

     # File lib/chronos/interval.rb, line 105
105:     def values_at(*keys)
106:       to_hash.values_at(*keys)
107:     end

[Validate]