Class | DBI::Type::Timestamp |
In: |
lib/dbi/types.rb
|
Parent: | Null |
# File lib/dbi/types.rb, line 105 105: def self.create(year, month, day, hour, min, sec, usec=0, of=0) 106: # DateTime will remove leap and leap-leap seconds 107: sec = 59 if sec > 59 108: # store this before we modify it 109: civil = year, month, day 110: time = hour, min, sec, usec 111: if month <= 2 112: month += 12 113: year -= 1 114: end 115: y = year + 4800 116: m = month - 3 117: jd = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045 118: #fr = hour / 24.0 + min / 1440.0 + sec / 86400.0 119: # ridiculously, this line does the same thing but twice as fast... :/ 120: fr = ::Time.gm(1970, 1, 1, hour, min, sec, usec).to_f / 86400 121: date = ::DateTime.new!(jd + fr - 0.5, of, ::DateTime::ITALY) 122: prefill_cache date, civil, time 123: date 124: end
# File lib/dbi/types.rb, line 163 163: def self.parse(obj) 164: case obj 165: when ::DateTime 166: return obj 167: when ::Date 168: return create(obj.year, obj.month, obj.day, 0, 0, 0) 169: when ::Time 170: return create(obj.year, obj.month, obj.day, obj.hour, obj.min, obj.sec, obj.usec, obj.utc_offset / 86400.0) 171: else 172: obj = super 173: return obj unless obj 174: return create(*parse_string(obj.to_s)) if obj.respond_to? :to_s 175: return create(*parse_string(obj.to_str)) if obj.respond_to? :to_str 176: return obj 177: end 178: end
# File lib/dbi/types.rb, line 140 140: def self.parse_string str 141: # special casing the common formats here gives roughly an 142: # 8-fold speed boost over using Date._parse 143: case str 144: when /^(\d{4})-(\d{2})-(\d{2})(?: (\d{2}):(\d{2}):(\d{2})(\.\d+)?)?(?: ([+-]?\d{2}):?(\d{2}))?$/ 145: parts = $~[1..-4].map { |s| s.to_i } 146: parts << $7.to_f * 1000000.0 147: parts << ($8 ? ($8.to_f * 60 + $9.to_i) / 1440 : 0) 148: else 149: parts = ::Date._parse(str).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset) 150: # some defaults 151: today = nil 152: 8.times do |i| 153: next if parts[i] 154: today ||= ::Time.now.to_a.values_at(5, 4, 3) + [0, 0, 0, 0, 0] 155: parts[i] = today[i] 156: end 157: parts[6] *= 1000000.0 158: parts[7] /= 86400.0 159: end 160: parts 161: end
# File lib/dbi/types.rb, line 127 127: def self.prefill_cache date, civil, time 128: time[3] /= 86400000000.0 129: date.instance_variable_set "@__#{:civil.to_i}__""@__#{:civil.to_i}__", [civil] 130: date.instance_variable_set "@__#{:time.to_i}__""@__#{:time.to_i}__", [time] 131: end