class SqlPostgres::PgTimestamp

This class holds the value of a “timestamp” column.

Attributes

day[R]
hour[R]
microseconds[R]
minute[R]
month[R]
second[R]
year[R]

Public Class Methods

from_sql(s) click to toggle source

Convert from Postgres (ie '2001-01-01 12:00:01') to a PgTimestamp

# 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
new(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, microseconds = 0) click to toggle source

Constructor taking all the pieces.

# 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

Public Instance Methods

to_local_time() click to toggle source

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.

# File lib/sqlpostgres/PgTimestamp.rb, line 59
def to_local_time
  Time.local(@year, @month, @day, @hour, @minute, @second)
end
to_s() click to toggle source

Convert to a string (ie '2001-01-01 12:00:00').

# File lib/sqlpostgres/PgTimestamp.rb, line 43
def to_s
  "%04d-%02d-%02d %02d:%02d:%02d.%05d" % parts
end
to_sql() click to toggle source

Convert to sql format (ie “timestamp '2001-01-01 12:00:00'”).

# File lib/sqlpostgres/PgTimestamp.rb, line 49
def to_sql
  "timestamp '#{to_s}'"
end
to_utc_time() click to toggle source

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.

# File lib/sqlpostgres/PgTimestamp.rb, line 69
def to_utc_time
  Time.utc(@year, @month, @day, @hour, @minute, @second)
end

Protected Instance Methods

parts() click to toggle source
# File lib/sqlpostgres/PgTimestamp.rb, line 75
def parts
  [@year, @month, @day, @hour, @minute, @second, @microseconds]
end

Private Instance Methods

column_type() click to toggle source
# File lib/sqlpostgres/PgTimestamp.rb, line 81
def column_type
  'timestamp'
end