Class: DBI::Timestamp

Inherits:
Object
  • Object
show all
Defined in:
lib/dbi/utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, fraction = nil) ⇒ Timestamp

DBI::Timestamp(year=0,month=0,day=0,hour=0,min=0,sec=0,fraction=nil) DBI::Timestamp(Time) DBI::Timestamp(Date)

Creates and returns a new DBI::Timestamp object. This is similar to a Time object in the standard library, but it also contains fractional seconds, expressed in nanoseconds. In addition, the constructor accepts either a Date or Time object.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dbi/utils.rb', line 108

def initialize(year=0, month=0, day=0, hour=0, min=0, sec=0, fraction=nil)
   case year
      when ::Time
         @year, @month, @day = year.year, year.month, year.day 
         @hour, @minute, @second, @fraction = year.hour, year.min, year.sec, nil 
         @original_time = year
      when ::Date
         @year, @month, @day = year.year, year.month, year.day 
         @hour, @minute, @second, @fraction = 0, 0, 0, nil 
         @original_date = year
      else
         @year, @month, @day = year, month, day
         @hour, @minute, @second, @fraction = hour, min, sec, fraction
   end
end

Instance Attribute Details

#dayObject Also known as: mday

Returns the value of attribute day.



96
97
98
# File 'lib/dbi/utils.rb', line 96

def day
  @day
end

#fractionObject

Returns fractional seconds, or 0 if not set.



138
139
140
# File 'lib/dbi/utils.rb', line 138

def fraction
   @fraction || 0
end

#hourObject

Returns the value of attribute hour.



97
98
99
# File 'lib/dbi/utils.rb', line 97

def hour
  @hour
end

#minuteObject Also known as: min

Returns the value of attribute minute.



97
98
99
# File 'lib/dbi/utils.rb', line 97

def minute
  @minute
end

#monthObject Also known as: mon

Returns the value of attribute month.



96
97
98
# File 'lib/dbi/utils.rb', line 96

def month
  @month
end

#secondObject Also known as: sec

Returns the value of attribute second.



97
98
99
# File 'lib/dbi/utils.rb', line 97

def second
  @second
end

#yearObject

Returns the value of attribute year.



96
97
98
# File 'lib/dbi/utils.rb', line 96

def year
  @year
end

Instance Method Details

#==(timestamp) ⇒ Object

Returns true if timestamp has a year, month, day, hour, minute, second and fraction equal to the comparing object.

Returns false if the comparison fails for any reason.



128
129
130
131
132
133
134
135
# File 'lib/dbi/utils.rb', line 128

def ==(timestamp)
   @year == timestamp.year and @month == timestamp.month and
   @day == timestamp.day and @hour == timestamp.hour and
   @minute == timestamp.minute and @second == timestamp.second and
   (fraction() == timestamp.fraction)
rescue
   false
end

#to_dateObject

Returns a new Date object based on the year, month and day or, if a Date object was passed to the constructor, returns that object.



175
176
177
# File 'lib/dbi/utils.rb', line 175

def to_date
   @original_date || ::Date.new(@year, @month, @day)
end

#to_sObject

Returns a DBI::Timestamp object as a string in YYYY-MM-DD HH:MM:SS format. If a fraction is present, then it is appended in “.FF” format.



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dbi/utils.rb', line 154

def to_s
   string = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
       @year, @month, @day, @hour, @minute, @second) 

   if @fraction
      fraction = ("%.9f" % (@fraction.to_i / 1e9)).
                  to_s[1..-1].gsub(/0{1,8}$/, "")
      string += fraction
   end

   string
end

#to_timeObject

Returns a new Time object based on the year, month and day or, if a Time object was passed to the constructor, returns that object.



169
170
171
# File 'lib/dbi/utils.rb', line 169

def to_time
   @original_time || ::Time.local(@year, @month, @day, @hour, @minute, @second)
end