Class: SqlPostgres::PgTimestamp

Inherits:
PgType
  • Object
show all
Defined in:
lib/sqlpostgres/PgTimestamp.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PgType

#eql?, #hash

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

#dayObject (readonly)

Returns the value of attribute day.



11
12
13
# File 'lib/sqlpostgres/PgTimestamp.rb', line 11

def day
  @day
end

#hourObject (readonly)

Returns the value of attribute hour.



12
13
14
# File 'lib/sqlpostgres/PgTimestamp.rb', line 12

def hour
  @hour
end

#microsecondsObject (readonly)

Returns the value of attribute microseconds.



15
16
17
# File 'lib/sqlpostgres/PgTimestamp.rb', line 15

def microseconds
  @microseconds
end

#minuteObject (readonly)

Returns the value of attribute minute.



13
14
15
# File 'lib/sqlpostgres/PgTimestamp.rb', line 13

def minute
  @minute
end

#monthObject (readonly)

Returns the value of attribute month.



10
11
12
# File 'lib/sqlpostgres/PgTimestamp.rb', line 10

def month
  @month
end

#secondObject (readonly)

Returns the value of attribute second.



14
15
16
# File 'lib/sqlpostgres/PgTimestamp.rb', line 14

def second
  @second
end

#yearObject (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_timeObject

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_sObject

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_sqlObject

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_timeObject

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