Class: SqlPostgres::PgTimestamp
- Defined in:
- lib/sqlpostgres/PgTimestamp.rb
Overview
This class holds the value of a “timestamp” column.
Instance Attribute Summary collapse
-
#day ⇒ Object
readonly
Returns the value of attribute day.
-
#hour ⇒ Object
readonly
Returns the value of attribute hour.
-
#microseconds ⇒ Object
readonly
Returns the value of attribute microseconds.
-
#minute ⇒ Object
readonly
Returns the value of attribute minute.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#second ⇒ Object
readonly
Returns the value of attribute second.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
-
.from_sql(s) ⇒ Object
Convert from Postgres (ie ‘2001-01-01 12:00:01’) to a PgTimestamp.
Instance Method Summary collapse
-
#initialize(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, microseconds = 0) ⇒ PgTimestamp
constructor
Constructor taking all the pieces.
-
#to_local_time ⇒ Object
Convert to an instance of Time in the local time zone.
-
#to_s ⇒ Object
Convert to a string (ie ‘2001-01-01 12:00:00’).
-
#to_sql ⇒ Object
Convert to sql format (ie “timestamp ‘2001-01-01 12:00:00’”).
-
#to_utc_time ⇒ Object
Convert to an instance of Time in the utc time zone.
Methods inherited from PgType
Constructor Details
#initialize(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, microseconds = 0) ⇒ PgTimestamp
Constructor taking all the pieces.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 29 def initialize(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, microseconds = 0) @year = year @month = month @day = day @hour = hour @minute = minute @second = second @microseconds = microseconds end |
Instance Attribute Details
#day ⇒ Object (readonly)
Returns the value of attribute day.
11 12 13 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 11 def day @day end |
#hour ⇒ Object (readonly)
Returns the value of attribute hour.
12 13 14 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 12 def hour @hour end |
#microseconds ⇒ Object (readonly)
Returns the value of attribute microseconds.
15 16 17 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 15 def microseconds @microseconds end |
#minute ⇒ Object (readonly)
Returns the value of attribute minute.
13 14 15 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 13 def minute @minute end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
10 11 12 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 10 def month @month end |
#second ⇒ Object (readonly)
Returns the value of attribute second.
14 15 16 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 14 def second @second end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
9 10 11 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 9 def year @year end |
Class Method Details
.from_sql(s) ⇒ Object
Convert from Postgres (ie ‘2001-01-01 12:00:01’) to a PgTimestamp
21 22 23 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 21 def from_sql(s) PgTimestamp.new(*s.gsub(/\D/, ' ').split.collect do |q| q.to_i end) end |
Instance Method Details
#to_local_time ⇒ Object
Convert to an instance of Time in the local time zone.
Note: Time can’t take all the values that PgTimestamp can, so this method can easily raise an exception. Only use it if you’re sure that the PgTimestamp will fit in a Time.
59 60 61 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 59 def to_local_time Time.local(@year, @month, @day, @hour, @minute, @second) end |
#to_s ⇒ Object
Convert to a string (ie ‘2001-01-01 12:00:00’).
43 44 45 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 43 def to_s "%04d-%02d-%02d %02d:%02d:%02d.%05d" % parts end |
#to_sql ⇒ Object
Convert to sql format (ie “timestamp ‘2001-01-01 12:00:00’”).
49 50 51 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 49 def to_sql "timestamp '#{to_s}'" end |
#to_utc_time ⇒ Object
Convert to an instance of Time in the utc time zone.
Note: Time can’t take all the values that PgTimestamp can, so this method can easily raise an exception. Only use it if you’re sure that the PgTimestamp will fit in a Time.
69 70 71 |
# File 'lib/sqlpostgres/PgTimestamp.rb', line 69 def to_utc_time Time.utc(@year, @month, @day, @hour, @minute, @second) end |