Class: SqlPostgres::PgTimeWithTimeZone

Inherits:
PgType
  • Object
show all
Defined in:
lib/sqlpostgres/PgTimeWithTimeZone.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

Constructor Details

#initialize(hour = 0, minute = 0, second = 0, zone_hours = 0, zone_minutes = 0) ⇒ PgTimeWithTimeZone

Constructor.

hour

0..23

minute

0..59

[second}

0..59
zone_hours

The hours of the time-zone offset (-23..23)

zone_minutes

The seconds of the time-zone offset (0..60)



57
58
59
60
61
62
63
64
# File 'lib/sqlpostgres/PgTimeWithTimeZone.rb', line 57

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

Instance Attribute Details

#hourObject (readonly)

Return the hour (0..23)



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

def hour
  @hour
end

#minuteObject (readonly)

Return the minute (0..59)



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

def minute
  @minute
end

#secondObject (readonly)

Return the second (0..59)



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

def second
  @second
end

#zone_hoursObject (readonly)

Return the hours of the time-zone offset.



23
24
25
# File 'lib/sqlpostgres/PgTimeWithTimeZone.rb', line 23

def zone_hours
  @zone_hours
end

#zone_minutesObject (readonly)

Return the minutes of the time-zone offset.



27
28
29
# File 'lib/sqlpostgres/PgTimeWithTimeZone.rb', line 27

def zone_minutes
  @zone_minutes
end

Class Method Details

.from_sql(s) ⇒ Object

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



34
35
36
37
38
39
40
# File 'lib/sqlpostgres/PgTimeWithTimeZone.rb', line 34

def from_sql(s)
  if s =~ /^(\d+):(\d+):(\d+)((?:\+|-)\d+)(?::(\d+))?$/
    PgTimeWithTimeZone.new($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i)
  else
    raise ArgumentError, "Invalid time with time zone: #{s.inspect}"
  end
end

Instance Method Details

#to_sObject

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



68
69
70
# File 'lib/sqlpostgres/PgTimeWithTimeZone.rb', line 68

def to_s
  "%02d:%02d:%02d%+03d:%02d" % parts
end

#to_sqlObject

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



74
75
76
# File 'lib/sqlpostgres/PgTimeWithTimeZone.rb', line 74

def to_sql
  "time with time zone '#{to_s}'"
end