Class: DBI::Timestamp
- Inherits:
-
Object
- Object
- DBI::Timestamp
- Defined in:
- lib/dbi/utils.rb
Instance Attribute Summary collapse
-
#day ⇒ Object
(also: #mday)
Returns the value of attribute day.
-
#fraction ⇒ Object
Returns fractional seconds, or 0 if not set.
-
#hour ⇒ Object
Returns the value of attribute hour.
-
#minute ⇒ Object
(also: #min)
Returns the value of attribute minute.
-
#month ⇒ Object
(also: #mon)
Returns the value of attribute month.
-
#second ⇒ Object
(also: #sec)
Returns the value of attribute second.
-
#year ⇒ Object
Returns the value of attribute year.
Instance Method Summary collapse
-
#==(timestamp) ⇒ Object
Returns true if
timestamp
has a year, month, day, hour, minute, second and fraction equal to the comparing object. -
#initialize(year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, fraction = nil) ⇒ Timestamp
constructor
DBI::Timestamp(year=0,month=0,day=0,hour=0,min=0,sec=0,fraction=nil) DBI::Timestamp(Time) DBI::Timestamp(Date).
-
#to_date ⇒ Object
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.
-
#to_s ⇒ Object
Returns a DBI::Timestamp object as a string in YYYY-MM-DD HH:MM:SS format.
-
#to_time ⇒ Object
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.
Constructor Details
#initialize(year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, fraction = nil) ⇒ Timestamp
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.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/dbi/utils.rb', line 108 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 |
Instance Attribute Details
#day ⇒ Object Also known as: mday
Returns the value of attribute day.
96 97 98 |
# File 'lib/dbi/utils.rb', line 96 def day @day end |
#fraction ⇒ Object
Returns fractional seconds, or 0 if not set.
138 139 140 |
# File 'lib/dbi/utils.rb', line 138 def fraction @fraction || 0 end |
#hour ⇒ Object
Returns the value of attribute hour.
97 98 99 |
# File 'lib/dbi/utils.rb', line 97 def hour @hour end |
#minute ⇒ Object Also known as: min
Returns the value of attribute minute.
97 98 99 |
# File 'lib/dbi/utils.rb', line 97 def minute @minute end |
#month ⇒ Object Also known as: mon
Returns the value of attribute month.
96 97 98 |
# File 'lib/dbi/utils.rb', line 96 def month @month end |
#second ⇒ Object Also known as: sec
Returns the value of attribute second.
97 98 99 |
# File 'lib/dbi/utils.rb', line 97 def second @second end |
#year ⇒ Object
Returns the value of attribute year.
96 97 98 |
# File 'lib/dbi/utils.rb', line 96 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.
128 129 130 131 132 133 134 135 |
# File 'lib/dbi/utils.rb', line 128 def ==() @year == .year and @month == .month and @day == .day and @hour == .hour and @minute == .minute and @second == .second and (fraction() == .fraction) rescue false end |
#to_date ⇒ Object
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.
175 176 177 |
# File 'lib/dbi/utils.rb', line 175 def to_date @original_date || ::Date.new(@year, @month, @day) end |
#to_s ⇒ Object
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.
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/dbi/utils.rb', line 154 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_time ⇒ Object
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.
169 170 171 |
# File 'lib/dbi/utils.rb', line 169 def to_time @original_time || ::Time.local(@year, @month, @day, @hour, @minute, @second) end |