Sha256: 96f23477e0699b53c998ac68f8f976a2898f5dbfa4ca11380b84793bfa94aaf6

Contents?: true

Size: 1.2 KB

Versions: 17

Compression:

Stored size: 1.2 KB

Contents

#!/usr/bin/env ruby

require 'date'
require 'json'

module PG
	module TextDecoder
		class Date < SimpleDecoder
			ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/

			def decode(string, tuple=nil, field=nil)
				if string =~ ISO_DATE
					::Date.new $1.to_i, $2.to_i, $3.to_i
				else
					string
				end
			end
		end

		class TimestampWithoutTimeZone < SimpleDecoder
			ISO_DATETIME_WITHOUT_TIMEZONE = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/

			def decode(string, tuple=nil, field=nil)
				if string =~ ISO_DATETIME_WITHOUT_TIMEZONE
					Time.new $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, "#{$6}#{$7}".to_r
				else
					string
				end
			end
		end

		class TimestampWithTimeZone < SimpleDecoder
			ISO_DATETIME_WITH_TIMEZONE = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?([-\+]\d\d):?(\d\d)?:?(\d\d)?\z/

			def decode(string, tuple=nil, field=nil)
				if string =~ ISO_DATETIME_WITH_TIMEZONE
					Time.new $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, "#{$6}#{$7}".to_r, "#{$8}:#{$9 || '00'}:#{$10 || '00'}"
				else
					string
				end
			end
		end

		class JSON < SimpleDecoder
			def decode(string, tuple=nil, field=nil)
				::JSON.parse(string, quirks_mode: true)
			end
		end
	end
end # module PG

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
pg-1.0.0-x64-mingw32 lib/pg/text_decoder.rb
pg-1.0.0-x86-mingw32 lib/pg/text_decoder.rb
pg-1.0.0 lib/pg/text_decoder.rb
pg-0.21.0-x64-mingw32 lib/pg/text_decoder.rb
pg-0.21.0-x86-mingw32 lib/pg/text_decoder.rb
pg-0.21.0 lib/pg/text_decoder.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/pg-0.20.0/lib/pg/text_decoder.rb
pg-0.20.0-x86-mingw32 lib/pg/text_decoder.rb
pg-0.20.0-x64-mingw32 lib/pg/text_decoder.rb
pg-0.20.0 lib/pg/text_decoder.rb
pg-0.19.1.pre20170124220800-x64-mingw32 lib/pg/text_decoder.rb
pg-0.19.1.pre20170124220800-x86-mingw32 lib/pg/text_decoder.rb
pg-0.19.1.pre20170124220800 lib/pg/text_decoder.rb
pg-0.19.1.pre20170115074000-x64-mingw32 lib/pg/text_decoder.rb
pg-0.19.1.pre20170115074000-x86-mingw32 lib/pg/text_decoder.rb
pg-0.19.1.pre20170115074000 lib/pg/text_decoder.rb
pg-0.19.0.pre20170115074000 lib/pg/text_decoder.rb