Skip to content

Improved DateTime handling. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/db/postgres/native/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,18 @@ def initialize(name = "TIMESTAMP")
attr :name

def parse(string)
if match = string.match(/(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)([\+\-].*)?/)
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[6] ||= "UTC"

parts[5] = BigDecimal(parts[5])

if parts[6].nil?
parts[6] = '+00'
end

return Time.new(*parts)
end
Expand Down
63 changes: 63 additions & 0 deletions test/db/postgres/connection/date_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2024, by Samuel Williams.

require 'db/postgres/connection'
require 'sus/fixtures/async'

ATimestamp = Sus::Shared("a timestamp") do |zone, time, expected|
it "can get timestamps with microseconds and timezone" do
connection.send_query("SET TIME ZONE '#{zone}'")

buffer = String.new
buffer << "SELECT "
connection.append_literal(time, buffer)
buffer << "::TIMESTAMPTZ"

connection.send_query buffer

result = connection.next_result
row = result.to_a.first

expect(row.first).to be == expected
ensure
connection.close
end
end

describe DB::Postgres::Connection do
include Sus::Fixtures::Async::ReactorContext

let(:connection) {subject.new(**CREDENTIALS)}

# PG produces: "2022-11-11 12:38:59.123456+00"
it_behaves_like ATimestamp, 'UTC', '2022-11-11 23:38:59.123456+11', Time.new(2022, 11, 11, 23, 38, BigDecimal('59.123456'), '+11:00')

# PG produces: "2022-11-11 12:38:59+00"
it_behaves_like ATimestamp, 'UTC', '2022-11-11 23:38:59+11', Time.new(2022, 11, 11, 23, 38, BigDecimal('59'), '+11:00')

# PG produces: "2022-11-11 23:38:59.123456+00"
it_behaves_like ATimestamp, 'UTC', '2022-11-11 23:38:59.123456', Time.new(2022, 11, 11, 23, 38, BigDecimal('59.123456'), '+00:00')

# PG produces: "2022-11-11 23:38:59+11"
it_behaves_like ATimestamp, 'Australia/Sydney', '2022-11-11 23:38:59', Time.new(2022, 11, 11, 23, 38, BigDecimal('59'), '+11:00')

# PG produces: "2022-11-12 06:08:59.123456+11"
it_behaves_like ATimestamp, 'Australia/Sydney', '2022-11-11 23:38:59.123456+04:30', Time.new(2022, 11, 11, 23, 38, BigDecimal('59.123456'), '+04:30')

# PG produces: "2000-01-01 05:30:00+05:30"
it_behaves_like ATimestamp, 'Asia/Kolkata', '2000-01-01 00:00:00+00', Time.new(2000, 1, 1, 5, 30, 0, '+05:30')

# PG produces: "2022-11-11 23:38:59+01"
it_behaves_like ATimestamp, 'Europe/Lisbon', '2022-11-11 23:38:59+01', Time.new(2022, 11, 11, 23, 38, BigDecimal('59'), '+01:00')

# PG produces: "infinity"
it_behaves_like ATimestamp, 'UTC', 'infinity', 'infinity'

# PG produces: "-infinity"
it_behaves_like ATimestamp, 'UTC', '-infinity', '-infinity'

# PG produces: null
it_behaves_like ATimestamp, 'UTC', nil, nil
end
Loading