Class: DBI::Type::Timestamp
Overview
Represents a SQL TIMESTAMP and returns DateTime. Falls back to Null.
Class Method Summary collapse
- .create(year, month, day, hour, min, sec, usec = 0, of = 0) ⇒ Object
- .parse(obj) ⇒ Object
- .parse_string(str) ⇒ Object
- .prefill_cache(date, civil, time) ⇒ Object
Class Method Details
.create(year, month, day, hour, min, sec, usec = 0, of = 0) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/dbi/types.rb', line 106 def self.create(year, month, day, hour, min, sec, usec=0, of=0) # DateTime will remove leap and leap-leap seconds sec = 59 if sec > 59 # store this before we modify it civil = year, month, day time = hour, min, sec, usec date = ::DateTime.civil(year, month, day, hour, min, sec, of) date += usec #prefill_cache date, civil, time date end |
.parse(obj) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/dbi/types.rb', line 165 def self.parse(obj) case obj when ::DateTime return obj when ::Date return create(obj.year, obj.month, obj.day, 0, 0, 0) when ::Time return create(obj.year, obj.month, obj.day, obj.hour, obj.min, obj.sec, Rational(obj.usec, 86400000000), Rational(obj.utc_offset, 86400)) else obj = super return obj unless obj return create(*parse_string(obj.to_s)) if obj.respond_to? :to_s return create(*parse_string(obj.to_str)) if obj.respond_to? :to_str return obj end end |
.parse_string(str) ⇒ Object
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 |
# File 'lib/dbi/types.rb', line 135 def self.parse_string str # special casing the common formats here gives roughly an # 8-fold speed boost over using Date._parse case str when /^(\d{4})-(\d{2})-(\d{2})(?: (\d{2}):(\d{2}):(\d{2})(\.\d+)?)?(?: ([+-]?\d{2}):?(\d{2}))?$/ parts = $~[1..-4].map { |s| s.to_i } # i feel unclean. if we have fractional seconds, pad the number and then stuff it into a rational. if $7 frac = $7.to_f * 10000000 parts << Rational(frac.to_i, 864000000000) else parts << 0 end parts << Rational(($8 || 0).to_i * 60 + ($9 || 0).to_i, 1440) else parts = ::Date._parse(str).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset) # some defaults today = nil 8.times do |i| next if parts[i] today ||= ::Time.now.to_a.values_at(5, 4, 3) + [0, 0, 0, 0, 0] parts[i] = today[i] end parts[6] = parts[6].kind_of?(Rational) ? parts[6] : Rational(parts[6], 1) parts[6] *= Rational(1, 86400) parts[7] = Rational(parts[7], 86400) end parts end |
.prefill_cache(date, civil, time) ⇒ Object
122 123 124 125 126 |
# File 'lib/dbi/types.rb', line 122 def self.prefill_cache date, civil, time time[3] /= 86400000000.0 date.instance_variable_set :"@__#{:civil.to_i}__", [civil] date.instance_variable_set :"@__#{:time.to_i}__", [time] end |