diff --git a/Sources/MySQLNIO/MySQLTime.swift b/Sources/MySQLNIO/MySQLTime.swift index 985c9f4..3afb4f4 100644 --- a/Sources/MySQLNIO/MySQLTime.swift +++ b/Sources/MySQLNIO/MySQLTime.swift @@ -82,11 +82,16 @@ public struct MySQLTime: Equatable, MySQLDataConvertible { let day = UInt16(parts[2]), let hour = UInt16(parts[3]), let minute = UInt16(parts[4]), - let second = UInt16(parts[5]) + let second = Double(parts[5]) else { return nil } - self.init(year: year, month: month, day: day, hour: hour, minute: minute, second: second) + + let fullSecond = UInt32((second * 1_000_000).rounded()) + let integer = UInt16(fullSecond / 1_000_000) + let real = fullSecond % 1_000_000 + + self.init(year: year, month: month, day: day, hour: hour, minute: minute, second: integer, microsecond: real) } /// See ``MySQLDataConvertible/init(mysqlData:)``. diff --git a/Tests/MySQLNIOTests/MySQLNIOTests.swift b/Tests/MySQLNIOTests/MySQLNIOTests.swift index 5a52ddd..a76d520 100644 --- a/Tests/MySQLNIOTests/MySQLNIOTests.swift +++ b/Tests/MySQLNIOTests/MySQLNIOTests.swift @@ -646,6 +646,21 @@ final class MySQLNIOTests: XCTestCase { XCTAssertEqual(rows[0].column("qux")?.int, 3) } + func testTextWithMicrosecondsMySQLTimeParse() throws { + let dateString = "2024-04-15 22:38:12.392812" + + let time = MySQLTime(dateString) + + XCTAssertNotNil(time) + XCTAssertEqual(time?.year, 2024) + XCTAssertEqual(time?.month, 4) + XCTAssertEqual(time?.day, 15) + XCTAssertEqual(time?.hour, 22) + XCTAssertEqual(time?.minute, 38) + XCTAssertEqual(time?.second, 12) + XCTAssertEqual(time?.microsecond, 392812) + } + // https://github.com/vapor/mysql-nio/issues/87 func testUnexpectedPacketHandling() async throws { struct PingCommand: MySQLCommand {