Module: Informix::Interval

Defined in:
lib/informix/interval.rb

Overview

The Interval module provides shortcuts for creating IntervalYTM and IntervalDTS objects

Class Method Summary collapse

Class Method Details

.day_to_second(*args) ⇒ Object

Shortcut to create an IntervalDTS object.

Interval.day_to_second(days = 0, hours = 0,
                       minutes = 0, seconds = 0)          => interval
Interval.day_to_second(:days => dd, :hours => hh,
                       :minutes => mm, :seconds => ss)    => interval

Interval.day_to_second(5, 3)                      # => '5 03:00:00.00000'
Interval.day_to_second(0, 2, 0, 30)               # => '0 02:00:30.00000'
Interval.day_to_second(:hours=>2.5)               # => '0 02:30:00.00000'
Interval.day_to_second(:seconds=>Rational(151,10))# => '0 00:00:15.10000'
Interval.day_to_second(:seconds=> 20.13)          # => '0 00:00:20.13000'
Interval.day_to_second(:days=>1.5, :hours=>2)     # => '1 14:00:00.00000'


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/informix/interval.rb', line 255

def self.day_to_second(*args)
  if args.size == 1 && Hash === args[0]
    h = args[0]
    days, hours, minutes, seconds = h[:days], h[:hours], h[:minutes],
                                    h[:seconds]
  elsif args.size <= 5 && args.all? {|e| Numeric === e || e.nil? }
    days, hours, minutes, seconds = args
  else
    raise TypeError, "Expected Numerics or a Hash"
  end
  days ||= 0; hours ||= 0; minutes ||= 0; seconds ||= 0
  if ![days, hours, minutes, seconds].all? {|e| Numeric === e && e >= 0 }
    raise ArgumentError, "Expected Numerics >= 0"
  end
  from_seconds(days*24*60*60 + hours*60*60 + minutes*60 + seconds)
end

.from_months(months) ⇒ Object

Shortcut to create an IntervalYTM object.

Interval.from_months(3)   #=>  '0-03'
Interval.from_months(71)  #=>  '5-11'


238
239
240
# File 'lib/informix/interval.rb', line 238

def self.from_months(months)
  IntervalYTM.new(months)
end

.from_seconds(seconds) ⇒ Object

Shortcut to create an IntervalDTS object.

Interval.from_seconds(9000)               #=> '0 02:30:00.00000'
Interval.from_seconds(Rational(151, 10))  #=> '0 00:00:15.10000'


276
277
278
# File 'lib/informix/interval.rb', line 276

def self.from_seconds(seconds)
  IntervalDTS.new(seconds)
end

.year_to_month(*args) ⇒ Object

Shortcut to create an IntervalYTM object.

Interval.year_to_month(years = 0, months = 0)       =>  interval
Interval.year_to_month(:years => yy, :months => mm) =>  interval

Interval.year_to_month(5)                           #=>  '5-00'
Interval.year_to_month(0, 3)                        #=>  '0-03'
Interval.year_to_month(5, 3)                        #=>  '5-03'
Interval.year_to_month(:years => 5.5)               #=>  '5-06'
Interval.year_to_month(:months => 3)                #=>  '0-03'
Interval.year_to_month(:years => 5.5, :months => 5) #=>  '5-11'


219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/informix/interval.rb', line 219

def self.year_to_month(*args)
  if args.size == 1 && Hash === args[0]
    years, months = args[0][:years], args[0][:months]
  elsif args.size <= 2 && args.all? {|e| Numeric === e }
    years, months = args
  else
    raise TypeError, "Expected Numerics or a Hash"
  end
  years ||= 0; months ||= 0
  if ![years, months].all? {|e| Numeric === e && e >= 0 }
    raise ArgumentError, "Expected Numerics >= 0"
  end
  from_months(years*12 + months.to_i)
end