pg_data_encoder 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -71,6 +71,7 @@ or
71
71
  * Boolean
72
72
  * Floats (double precision)
73
73
  * Timestamp
74
+ * Date
74
75
  * Array (integer and string single dimension)
75
76
 
76
77
  ## Contributing
@@ -1,7 +1,8 @@
1
1
  require 'tempfile'
2
2
  require 'stringio'
3
3
  module PgDataEncoder
4
- POSTGRES_EPOCH_DATE = (Time.utc(2000,1,1).to_f * 1_000_000).to_i
4
+ POSTGRES_EPOCH_TIME = (Time.utc(2000,1,1).to_f * 1_000_000).to_i
5
+
5
6
  class EncodeForCopy
6
7
  def initialize(options = {})
7
8
  @options = options
@@ -127,7 +128,11 @@ module PgDataEncoder
127
128
  io.write([hash_io.pos].pack("N")) # assumed identifier for hstore column
128
129
  io.write(hash_io.string)
129
130
  when Time
130
- buf = [(field.to_f * 1_000_000 - POSTGRES_EPOCH_DATE).to_i].pack("L!>")
131
+ buf = [(field.to_f * 1_000_000 - POSTGRES_EPOCH_TIME).to_i].pack("L!>")
132
+ io.write([buf.bytesize].pack("N"))
133
+ io.write(buf)
134
+ when Date
135
+ buf = [(field - Date.new(2000,1,1)).to_i].pack("N")
131
136
  io.write([buf.bytesize].pack("N"))
132
137
  io.write(buf)
133
138
  else
@@ -1,3 +1,3 @@
1
1
  module PgDataEncoder
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
Binary file
Binary file
Binary file
@@ -123,6 +123,42 @@ describe "generating data" do
123
123
  str.should == existing_data
124
124
  end
125
125
 
126
+ it 'should encode date data correctly' do
127
+ encoder = PgDataEncoder::EncodeForCopy.new
128
+ encoder.add [Date.parse("1900-12-03")]
129
+ encoder.close
130
+ io = encoder.get_io
131
+ existing_data = filedata("date.dat")
132
+ str = io.read
133
+ io.class.name.should == "StringIO"
134
+ str.force_encoding("ASCII-8BIT")
135
+ str.should == existing_data
136
+ end
137
+
138
+ it 'should encode date data correctly for years > 2000' do
139
+ encoder = PgDataEncoder::EncodeForCopy.new
140
+ encoder.add [Date.parse("2033-01-12")]
141
+ encoder.close
142
+ io = encoder.get_io
143
+ existing_data = filedata("date2000.dat")
144
+ str = io.read
145
+ io.class.name.should == "StringIO"
146
+ str.force_encoding("ASCII-8BIT")
147
+ str.should == existing_data
148
+ end
149
+
150
+ it 'should encode date data correctly in the 70s' do
151
+ encoder = PgDataEncoder::EncodeForCopy.new
152
+ encoder.add [Date.parse("1971-12-11")]
153
+ encoder.close
154
+ io = encoder.get_io
155
+ existing_data = filedata("date2.dat")
156
+ str = io.read
157
+ io.class.name.should == "StringIO"
158
+ str.force_encoding("ASCII-8BIT")
159
+ str.should == existing_data
160
+ end
161
+
126
162
  it 'should encode timestamp data correctly' do
127
163
  encoder = PgDataEncoder::EncodeForCopy.new
128
164
  encoder.add [Time.parse("2013-06-11 15:03:54.62605 UTC")]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_data_encoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-14 00:00:00.000000000 Z
12
+ date: 2013-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -70,6 +70,9 @@ files:
70
70
  - spec/fixtures/array_with_two2.dat
71
71
  - spec/fixtures/big_str_array.dat
72
72
  - spec/fixtures/big_str_array2.dat
73
+ - spec/fixtures/date.dat
74
+ - spec/fixtures/date2.dat
75
+ - spec/fixtures/date2000.dat
73
76
  - spec/fixtures/falseclass.dat
74
77
  - spec/fixtures/float.dat
75
78
  - spec/fixtures/intarray.dat
@@ -115,6 +118,9 @@ test_files:
115
118
  - spec/fixtures/array_with_two2.dat
116
119
  - spec/fixtures/big_str_array.dat
117
120
  - spec/fixtures/big_str_array2.dat
121
+ - spec/fixtures/date.dat
122
+ - spec/fixtures/date2.dat
123
+ - spec/fixtures/date2000.dat
118
124
  - spec/fixtures/falseclass.dat
119
125
  - spec/fixtures/float.dat
120
126
  - spec/fixtures/intarray.dat