In Files

Files

DBI::Type::Timestamp

Represents a SQL TIMESTAMP and returns DateTime. Falls back to Null.

Public Class Methods

create(year, month, day, hour, min, sec, usec=0, of=0) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.