Class: Perpetuity::Postgres::TimestampValue

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/postgres/timestamp_value.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time) ⇒ TimestampValue

Returns a new instance of TimestampValue.



7
8
9
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 7

def initialize time
  @time = time
end

Instance Attribute Details

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 6

def time
  @time
end

Class Method Details

.from_sql(sql_value) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 11

def self.from_sql sql_value
  match = sql_value =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.?(\d*)?([-+]\d{2})?/
  return new(nil) unless match

  offset = $8 ? "#$8:00" : '+00:00'
  new Time.new($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, "#$6.#$7".to_f, offset)
end

Instance Method Details

#dayObject



35
36
37
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 35

def day
  zero_pad(time.day)
end

#hourObject



39
40
41
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 39

def hour
  zero_pad(time.hour)
end

#minuteObject



43
44
45
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 43

def minute
  zero_pad(time.min)
end

#monthObject



31
32
33
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 31

def month
  zero_pad(time.month)
end

#offsetObject



51
52
53
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 51

def offset
  time.strftime('%z')
end

#secondObject



47
48
49
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 47

def second
  '%02d.%06d' % [time.sec, time.usec]
end

#to_sObject



55
56
57
58
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 55

def to_s
  string = TextValue.new("#{year}-#{month}-#{day} #{hour}:#{minute}:#{second}#{offset}").to_s
  "#{string}::timestamptz"
end

#to_timeObject



19
20
21
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 19

def to_time
  time
end

#valueObject



23
24
25
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 23

def value
  time
end

#yearObject



27
28
29
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 27

def year
  time.year
end