Bug 14443 - Reading Time or DateTime with ODBC is not working on Linux 64 bit
Summary: Reading Time or DateTime with ODBC is not working on Linux 64 bit
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data (show other bugs)
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-09-04 15:33 UTC by wdehoog
Modified: 2013-09-06 04:54 UTC (History)
1 user (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
the test file I use (1.12 KB, text/x-csharp)
2013-09-05 12:53 UTC, wdehoog
Details

Description wdehoog 2013-09-04 15:33:42 UTC
When trying this code on Ubuntu 64 bits:

string sql = "SELECT '23:45:01'::time, '12:34:56'::time ";
dbcmd.CommandText = sql;
IDataReader reader = dbcmd.ExecuteReader();
reader.Read();
Console.WriteLine("0: " + reader.GetDataTypeName(0));
Console.WriteLine("1: " + reader.GetDataTypeName(1));
string str =  reader[0].ToString();
Console.WriteLine(str);
str =  reader[1].ToString();
Console.WriteLine(str);

it outputs:
0: time
1: time
00:00:01
00:00:56

On Ubuntu 32 bits:
0: time
1: time
23:45:01
12:34:56

When reading a DateTime value (postgres type ::date or ::timestamp) the DateTime constructor throws an exception (unrepresentable date time) on 64bit platform. On 32bits it works normally.

On 64 bits Libreoffice using the same odbc driver (postgresql) does show the correct time or date values so it is probably not an odbc or postgres problem.

I suspect something is wrong with the way libodbc is used on 64bit platform. (Is struct OdbcTimestamp ok?)
Comment 1 wdehoog 2013-09-05 12:52:07 UTC
I played around in OdbcDataReader.cs and noticed some very weird behaviour:

this original source crashes:
  DateTime dtv = new DateTime(ts_data.year, ts_data.month,
                              ts_data.day, ts_data.hour, ts_data.minute,
                              ts_data.second);

this actually works:
  DateTime dtv = new DateTime(ts_data.year,ts_data.month,ts_data.day);
  TimeSpan time = new TimeSpan (ts_data.hour, ts_data.minute, ts_data.second);
  dtv = dtv.Add(time);
  DataValue = dtv;

It should not be different according to the source of DateTime.

Another weird thing is that when I print the output of libodbc using 
Console.WriteLine(String.Format("libodbc: {0}-{1}-{2} {3}:{4}:{5}:{6}", ts_data.year, ts_data.month, ts_data.day, ts_data.hour, ts_data.minute, ts_data.second, ts_data.fraction));

using sql: SELECT '2013-12-03 12:34:56'::timestamp, '2013-12-03'::date, '12:34:56'::time 

with the original version
  0: timestamp without time zone
  1: date
  2: time
  libodbc: 0-0-3 12:34:56:0
  <exeption>

with the working version
  0: timestamp without time zone
  1: date
  2: time
  libodbc: 2013-12-3 12:34:56:0
  12/3/2013 12:34:56 PM
  libodbc: 2013-12-3 0:0:0:0
  12/3/2013 12:00:00 AM
  12:34:56

Note that the Console.WriteLine statement is _before_ the DateTime construction. 


Last weird thing is when I add this extra code:
  DateTime dtv2 = new DateTime(1,1,1,0,0,0);
it crashes. When adding
  DateTime dtv2 = new DateTime(1,1,1);
it works.

How can a constructor cause data to be corrupted?
Comment 2 wdehoog 2013-09-05 12:53:33 UTC
Created attachment 4789 [details]
the test file I use
Comment 3 wdehoog 2013-09-05 13:33:36 UTC
Forgot to add:

mono -- version
Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-5ubuntu1)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            Included Boehm (with typed GC and Parallel Mark)

lsb_release -a
Description:	Ubuntu 13.04
Release:	13.04
Codename:	raring

uname -a
Linux xtreamer-ultra-2 3.8.0-26-generic #38-Ubuntu SMP Mon Jun 17 21:43:33 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Comment 4 wdehoog 2013-09-06 04:54:12 UTC
Tried current mono from git and the problem is here as well and the work around solves it here too.

Mono JIT compiler version 3.2.3 (master/e682ece do sep  5 22:01:49 CEST 2013)
Copyright (C) 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug 
        LLVM:          supported, not enabled.
        GC:            sgen

Note You need to log in before you can comment on or make changes to this bug.