Class: DBI::Timestamp

Inherits:
Object
  • Object
show all
Defined in:
lib/dbi/utils/timestamp.rb

Overview

Represents a Timestamp.

DEPRECATED: Please use a regular DateTime object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dayObject Also known as: mday

Returns the value of attribute day.



8
9
10
# File 'lib/dbi/utils/timestamp.rb', line 8

def day
  @day
end

#fractionObject

Returns fractional seconds, or 0 if not set.



55
56
57
# File 'lib/dbi/utils/timestamp.rb', line 55

def fraction
   @fraction || 0
end

#hourObject

Returns the value of attribute hour.



9
10
11
# File 'lib/dbi/utils/timestamp.rb', line 9

def hour
  @hour
end

#minuteObject Also known as: min

Returns the value of attribute minute.



9
10
11
# File 'lib/dbi/utils/timestamp.rb', line 9

def minute
  @minute
end

#monthObject Also known as: mon

Returns the value of attribute month.



8
9
10
# File 'lib/dbi/utils/timestamp.rb', line 8

def month
  @month
end

#secondObject Also known as: sec

Returns the value of attribute second.



9
10
11
# File 'lib/dbi/utils/timestamp.rb', line 9

def second
  @second
end

#yearObject

Returns the value of attribute year.



8
9
10
# File 'lib/dbi/utils/timestamp.rb', line 8

def year
  @year
end

Instance Method Details

#==(timestamp) ⇒ Object

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.



45
46
47
48
49
50
51
52
# File 'lib/dbi/utils/timestamp.rb', line 45

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

#to_dateObject

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.



92
93
94
# File 'lib/dbi/utils/timestamp.rb', line 92

def to_date
   @original_date || ::Date.new(@year, @month, @day)
end

#to_sObject

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.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dbi/utils/timestamp.rb', line 71

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

#to_timeObject

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.



86
87
88
# File 'lib/dbi/utils/timestamp.rb', line 86

def to_time
   @original_time || ::Time.local(@year, @month, @day, @hour, @minute, @second)
end