Class: DB::Postgres::Native::Types::DateTime

Inherits:
Object
  • Object
show all
Defined in:
lib/db/postgres/native/types.rb

Overview

A datetime/timestamp type converter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "TIMESTAMP") ⇒ DateTime

Initialize a datetime type converter.



123
124
125
# File 'lib/db/postgres/native/types.rb', line 123

def initialize(name = "TIMESTAMP")
	@name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



128
129
130
# File 'lib/db/postgres/native/types.rb', line 128

def name
  @name
end

#The SQL type name.(SQLtypename.) ⇒ Object (readonly)



128
# File 'lib/db/postgres/native/types.rb', line 128

attr :name

Instance Method Details

#parse(string) ⇒ Object

Parse a datetime value from the database.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/db/postgres/native/types.rb', line 133

def parse(string)
	if string == "-infinity" || string == "infinity" || string.nil?
		return string
	end
	
	if match = string.match(/\A(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+(?:\.\d+)?)([-+]\d\d(?::\d\d)?)?\z/)
		parts = match.captures
		
		parts[5] = Rational(parts[5])
		
		if parts[6].nil?
			parts[6] = "+00"
		end
		
		return Time.new(*parts)
	end
end