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
18
19
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 11

def self.from_sql sql_value
  date, time = sql_value.split(/ /)
  year, month, day = date.split(/-/)
  hour, minute, seconds_with_offset = time.split(/:/)
  second = seconds_with_offset[/\d+\.\d+/].to_f
  offset = seconds_with_offset[/(\+|\-)\d+/] + ':00'

  new Time.new(year, month, day, hour, minute, second, offset)
end

Instance Method Details

#dayObject



37
38
39
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 37

def day
  zero_pad(time.day)
end

#hourObject



41
42
43
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 41

def hour
  zero_pad(time.hour)
end

#minuteObject



45
46
47
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 45

def minute
  zero_pad(time.min)
end

#monthObject



33
34
35
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 33

def month
  zero_pad(time.month)
end

#offsetObject



53
54
55
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 53

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

#secondObject



49
50
51
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 49

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

#to_sObject



57
58
59
60
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 57

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

#to_timeObject



21
22
23
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 21

def to_time
  time
end

#valueObject



25
26
27
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 25

def value
  time
end

#yearObject



29
30
31
# File 'lib/perpetuity/postgres/timestamp_value.rb', line 29

def year
  time.year
end