1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
module DBI
#
# Represents a Timestamp.
#
# DEPRECATED: Please use a regular DateTime object.
#
class Timestamp
include Deprecated
attr_accessor :year, :month, :day
attr_accessor :hour, :minute, :second
attr_writer :fraction
private
# 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.
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
public
deprecated :initialize, :public
# 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.
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
# Returns fractional seconds, or 0 if not set.
def fraction
@fraction || 0
end
# Aliases
alias :mon :month
alias :mon= :month=
alias :mday :day
alias :mday= :day=
alias :min :minute
alias :min= :minute=
alias :sec :second
alias :sec= :second=
# 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.
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
# 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.
def to_time
@original_time || ::Time.local(@year, @month, @day, @hour, @minute, @second)
end
# 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.
def to_date
@original_date || ::Date.new(@year, @month, @day)
end
end
end
|