Bug #30528 TIMEDIFF has negative returned if start is after end, not returned by C API
Submitted: 21 Aug 2007 5:16 Modified: 21 Aug 2007 12:55
Reporter: bob smith Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S2 (Serious)
Version:5.0.24a OS:Linux
Assigned to: CPU Architecture:Any

[21 Aug 2007 5:16] bob smith
Description:
SELECT TIMEDIFF('01:00:00','02:00:00'); returns -01:00:00 in mysql, but in a C program, it returns 01:00:00. 

I followed this to /usr/include/mysql/mysql_time.h, which has the returned hour defined as unsigned.

How to repeat:
char *t;
MYSQL_STMT *stmt;
MYSQL_BIND parm[2];
MYSQL_TIME result;

sprintf(t,"SELECT TIMEDIFF('01:00:00','02:00:00');");
if (mysql_stmt_prepare (stmt,t,strlen(t))!=0) {
	cout << "Error on stmt_prepare in do_date_string\n"; 	
	cout << "string is  " << t << '\n'; }
memset((void *)parm, 0, sizeof(parm));
parm[0].buffer_type=MYSQL_TYPE_DATETIME;
parm[0].buffer = (void *) &result;
parm[0].is_null=0;
if (mysql_stmt_execute(stmt)!=0) {
	print_stmt_error(stmt,"Could not execute stmt_execute in do_date_string\n"); }	
if (mysql_stmt_bind_result(stmt,parm)!=0) {
	print_stmt_error(stmt,"Could not bind results"); }
if (mysql_stmt_store_result(stmt)!=0) {
	cout << "Error in mysql_store_result\n"; }
mysql_stmt_fetch(stmt);
cout << "end of date_string " << result.month << "/" << result.day << "/" << result.year << " " << result.hour << ":" << result.minute << "\n";
mysql_stmt_free_result(stmt);  

Suggested fix:
Make hour a signed variable. All I have tried is changing mysql_time.h, but there must be other places internal to the C API.
[21 Aug 2007 12:55] Hartmut Holzgraefe
why would you make hour a signed value? 
why not year instead? or any of the other fields?

what we do instead is to set the MYSQL_TIME::neg flag
to 1 if the result is negative and to 0 if it is positive.