Class: SqlPostgres::PgTime

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PgType

#eql?, #hash, #to_sql

Constructor Details

#initialize(hour = 0, minute = 0, second = 0) ⇒ PgTime

Constructor taking hour (0..23), minute (0..59), and second (0..59)



33
34
35
36
37
# File 'lib/sqlpostgres/PgTime.rb', line 33

def initialize(hour = 0, minute = 0, second = 0)
  @hour = hour
  @minute = minute
  @second = second
end

Instance Attribute Details

#hourObject (readonly)

Return the hour (0..23)



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

def hour
  @hour
end

#minuteObject (readonly)

Return the minute (0..59)



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

def minute
  @minute
end

#secondObject (readonly)

Return the second (0..59)



19
20
21
# File 'lib/sqlpostgres/PgTime.rb', line 19

def second
  @second
end

Class Method Details

.from_sql(s) ⇒ Object

Create a PgTime from a string in Postgres format (ie “12:00:00”).



25
26
27
# File 'lib/sqlpostgres/PgTime.rb', line 25

def from_sql(s)
  PgTime.new(*s.split(":").collect do |p| p.to_i end)
end

Instance Method Details

#to_local_timeObject

Convert to an instance of Time on date 1970/01/01, local time zone.



47
48
49
# File 'lib/sqlpostgres/PgTime.rb', line 47

def to_local_time
  Time.local(1970, 1, 1, @hour, @minute, @second)
end

#to_sObject

Return a string representation (ie “12:00:00”).



41
42
43
# File 'lib/sqlpostgres/PgTime.rb', line 41

def to_s
  "%02d:%02d:%02d" % [@hour, @minute, @second]
end

#to_utc_timeObject

Convert to an instance of Time on date 1970/01/01, utc time zone.



53
54
55
# File 'lib/sqlpostgres/PgTime.rb', line 53

def to_utc_time
  Time.utc(1970, 1, 1, @hour, @minute, @second)
end