ziutek/mymysql — pull request #79 by james-chalfant (2013-08-16 21:29:42+00:00)
Title: MYSQL_TYPE_TIMESTAMP: fix Microsecond alignment and use Microseconds

According the mysql documentation here:

http://dev.mysql.com/doc/internals/en/binary-protocol-value.html

"ProtocolBinary::MYSQL_TYPE_TIMESTAMP" expects the final 4 bytes of an 11 byte MYSQL_TYPE_TIMESTAMP to contain a little endian unsigned 32 bit value representing microseconds. Prior to this change, this value was based on nanoseconds and also misaligned (it occupied bytes 7-10 instead of bytes 8-11; byte 7 was later overwritten with seconds as represented by a single byte).

This change both properly aligns the value and converts from Nanoseconds to Microseconds and back as appropriate when writing to and reading from mysql.

Impact: using mysql 5.6, mysql appears to interpret a value larger than the maximum possible number of microseconds in a second (i.e., > 1000000) as an error and consider the entire encoded MYSQL_TYPE_TIMESTAMP as a 0 value. In practice this means there is approximately a 75% chance that any given point in time will have an invalid microseconds value and get rejected by mysql. Prior versions of mysql may have been more accepting of invalid values; I only encountered this behavior recently (previously I was using mysql 5.1).