DB::PostgresSourceDBPostgresNativeTypesDateTime

class DateTime

A datetime/timestamp type converter.

Definitions

def initialize(name = "TIMESTAMP")

Initialize a datetime type converter.

Signature

parameter name String

The SQL type name.

Implementation

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

attr :name

Signature

attribute String

The SQL type name.

def parse(string)

Parse a datetime value from the database.

Signature

parameter string String | Nil

The raw string value or special values like 'infinity'.

returns Time | String | Nil

The parsed datetime as a Time object, or special values as strings.

Implementation

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