Sha256: 9485ec246446554cbba7620b166e2255bfdead4feed8d43d867575e409b92000

Contents?: true

Size: 1.05 KB

Versions: 9

Compression:

Stored size: 1.05 KB

Contents

#!/usr/bin/env ruby

require 'date'

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
	end
end # module PG

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pg-0.19.0.pre20160817083826 lib/pg/text_decoder.rb
pg-0.19.0.pre20160810143640 lib/pg/text_decoder.rb
pg-0.19.0.pre20160409114042 lib/pg/text_decoder.rb
pg-0.18.4-x64-mingw32 lib/pg/text_decoder.rb
pg-0.18.4-x86-mingw32 lib/pg/text_decoder.rb
pg-0.18.4 lib/pg/text_decoder.rb
pg-0.18.3-x86-mingw32 lib/pg/text_decoder.rb
pg-0.18.3-x64-mingw32 lib/pg/text_decoder.rb
pg-0.18.3 lib/pg/text_decoder.rb