Class: ODPI::BindType::TimestampBase

Inherits:
Base
  • Object
show all
Defined in:
lib/odpi/bindtype.rb

Direct Known Subclasses

Date, Timestamp, TimestampLTZ, TimestampTZ

Constant Summary collapse

@@datetime_fsec_base =
(1 / ::DateTime.parse('0001-01-01 00:00:00.000000001').sec_fraction).to_i

Instance Attribute Summary

Attributes inherited from Base

#raw_var

Class Method Summary collapse

Methods inherited from Base

#[], #[]=, #get, #initialize, #set, to_bindclass

Constructor Details

This class inherits a constructor from ODPI::BindType::Base

Class Method Details

.convert_in(conn, val) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/odpi/bindtype.rb', line 120

def self.convert_in(conn, val)
  # year
  year = val.year
  # month
  if val.respond_to? :mon
    month = val.mon
  elsif val.respond_to? :month
    month = val.month
  else
    raise "expect Time, Date or DateTime but #{val.class}"
  end
  # day
  if val.respond_to? :mday
    day = val.mday
  elsif val.respond_to? :day
    day = val.day
  else
    raise "expect Time, Date or DateTime but #{val.class}"
  end
  # hour
  if val.respond_to? :hour
    hour = val.hour
  else
    hour = 0
  end
  # minute
  if val.respond_to? :min
    minute = val.min
  else
    minute = 0
  end
  # second
  if val.respond_to? :sec
    sec = val.sec
  else
    sec = 0
  end
  # fractional second
  if val.respond_to? :sec_fraction
    fsec = (val.sec_fraction * @@datetime_fsec_base).to_i
  elsif val.respond_to? :nsec
    fsec = val.nsec
  elsif val.respond_to? :usec
    fsec = val.usec * 1000
  else
    fsec = 0
  end
  # time zone
  if val.respond_to? :offset
    # DateTime
    tz_min = (val.offset * 1440).to_i
  elsif val.respond_to? :utc_offset
    # Time
    tz_min = val.utc_offset / 60
  else
    tz_hour = nil
    tz_min = nil
  end
  if tz_min
    if tz_min < 0
      tz_min = - tz_min
      tz_hour = - (tz_min / 60)
      tz_min = (tz_min % 60)
    else
      tz_hour = tz_min / 60
      tz_min = tz_min % 60
    end
  end
  [year, month, day, hour, minute, sec, fsec, tz_hour, tz_min]
end

.convert_out(conn, val) ⇒ Object


191
192
193
194
195
196
197
198
199
200
201
# File 'lib/odpi/bindtype.rb', line 191

def self.convert_out(conn, val)
  year, month, day, hour, minute, sec, nsec, tz_hour, tz_min = val

  sec += nsec.to_r / 1_000_000_000 if nsec && nsec != 0
  if tz_hour
    utc_offset = tz_hour * 3600 + tz_min * 60
    ::Time.new(year, month, day, hour, minute, sec, utc_offset)
  else
    ::Time.utc(year, month, day, hour, minute, sec) # TODO: add a parameter to use Time.local.
  end
end