Skip to content

Commit ba8048d

Browse files
authored
Improve handing of DateTime values for 1970-01-01 (#177)
Fixes #175. As @david-macmahon pointed out, it was a bit strange that we were special-casing the binding of MYSQL_TIME structs when converting from DateTime. The mysql DATETIME type specifically supports dates between 1000-01-01 to 9999-12-31 23:59:59, so there's no reason we should be special-casing 1970-01-01.
1 parent feffdc7 commit ba8048d

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/api/apitypes.jl

+3-9
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,9 @@ Base.convert(::Type{MYSQL_TIME}, t::Dates.Time) =
135135
Base.convert(::Type{MYSQL_TIME}, dt::Date) =
136136
MYSQL_TIME(Dates.year(dt), Dates.month(dt), Dates.day(dt), 0, 0, 0, 0, 0, 0)
137137

138-
function Base.convert(::Type{MYSQL_TIME}, dtime::DateTime)
139-
if Dates.year(dtime) == 1970 && Dates.month(dtime) == 1 && Dates.day(dtime) == 1
140-
MYSQL_TIME(0, 0, 0,
141-
Dates.hour(dtime), Dates.minute(dtime), Dates.second(dtime), 0, 0, 0)
142-
else
143-
MYSQL_TIME(Dates.year(dtime), Dates.month(dtime), Dates.day(dtime),
144-
Dates.hour(dtime), Dates.minute(dtime), Dates.second(dtime), 0, 0, 0)
145-
end
146-
end
138+
Base.convert(::Type{MYSQL_TIME}, dtime::DateTime) =
139+
MYSQL_TIME(Dates.year(dtime), Dates.month(dtime), Dates.day(dtime),
140+
Dates.hour(dtime), Dates.minute(dtime), Dates.second(dtime), 0, 0, 0)
147141

148142
# this is a helper struct, because MYSQL_BIND needs
149143
# to know where the bound data should live, by using this helper

test/runtests.jl

+9
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ res = DBInterface.execute(conn, "select id, t from blob_field") |> columntable
232232
@test length(res) == 2
233233
@test res[2][1] == [0x68, 0x65, 0x79, 0x20, 0x74, 0x68, 0x65, 0x72, 0x65, 0x20, 0x73, 0x61, 0x69, 0x6c, 0x6f, 0x72]
234234

235+
# https://github.com/JuliaDatabases/MySQL.jl/issues/175
236+
DBInterface.execute(conn, "DROP TABLE if exists datetime_field")
237+
DBInterface.execute(conn, "CREATE TABLE datetime_field (id int(11), t DATETIME)")
238+
stmt = DBInterface.prepare(conn, "INSERT INTO datetime_field (id, t) VALUES (?, ?);")
239+
DBInterface.execute(stmt, [1, DateTime(1970, 1, 1, 3)])
240+
resstmt = DBInterface.prepare(conn, "select id, t from datetime_field")
241+
res = DBInterface.execute(resstmt) |> columntable
242+
@test length(res) == 2
243+
@test res[2][1] == DateTime(1970, 1, 1, 3)
235244

236245
DBInterface.execute(conn, """
237246
CREATE PROCEDURE get_employee()

0 commit comments

Comments
 (0)