From: Date: November 1 2007 6:23pm Subject: Connector/NET commit: r1051 - in branches/5.1: . Driver/Source/Types TestSuite/Source List-Archive: http://lists.mysql.com/commits/36907 X-Bug: 32010 Message-Id: <200711011723.lA1HNubL020362@bk-internal.mysql.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modified: branches/5.1/CHANGES branches/5.1/Driver/Source/Types/MySqlDateTime.cs branches/5.1/TestSuite/Source/DateTimeTests.cs Log: fixed MySqlDateTime.ToString() to properly return the date value (Bug #32010) The problem was that we were using the defined date/time format strings with String.Format and that doesn't work. To fix it we had to write our own custom formatting function that handled parts like MM, dd, and yyyy. Modified: branches/5.1/CHANGES =================================================================== --- branches/5.1/CHANGES 2007-11-01 17:17:58 UTC (rev 1050) +++ branches/5.1/CHANGES 2007-11-01 17:23:56 UTC (rev 1051) @@ -95,7 +95,8 @@ used the wrong case for a user id and then fixed it to still get access denied errors. (Bug #31433) - improved the speed of load data local infile significantly - + - fixed MySqlDateTime.ToString() to properly return the date value (Bug #32010) + Version 5.0.8 8/16/2007 Bug #28706 Log messages are truncated - Fixed a problem with compression over a network. We were letting the inflate stream read Modified: branches/5.1/Driver/Source/Types/MySqlDateTime.cs =================================================================== --- branches/5.1/Driver/Source/Types/MySqlDateTime.cs 2007-11-01 17:17:58 UTC (rev 1050) +++ branches/5.1/Driver/Source/Types/MySqlDateTime.cs 2007-11-01 17:23:56 UTC (rev 1051) @@ -436,23 +436,42 @@ return new DateTime(year, month, day, hour, minute, second); } - /// Returns a MySQL specific string representation of this value - public override string ToString() - { - if (this.IsValidDateTime) - { - DateTime d = new DateTime(year, month, day, hour, minute, second); - return (type == MySqlDbType.Date) ? d.ToString("d") : d.ToString(); - } + private string FormatDateCustom(string format, int monthVal, int dayVal, int yearVal) + { + format = format.Replace("MM", "{0:00}"); + format = format.Replace("M", "{0}"); + format = format.Replace("dd", "{1:00}"); + format = format.Replace("d", "{1}"); + format = format.Replace("yyyy", "{2:0000}"); + format = format.Replace("yy", "{3:00}"); + format = format.Replace("y", "{4:0}"); - if (type == MySqlDbType.Date) - return String.Format(CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern, - year, month, day); + int year2digit = yearVal - ((yearVal / 1000) * 1000); + year2digit -= ((year2digit / 100) * 100); + int year1digit = year2digit - ((year2digit / 10) * 10); - return String.Format(CultureInfo.CurrentUICulture.DateTimeFormat.FullDateTimePattern, - year, month, day, hour, minute, second); - } + return String.Format(format, monthVal, dayVal, yearVal, year2digit, year1digit); + } + /// Returns a MySQL specific string representation of this value + public override string ToString() + { + if (this.IsValidDateTime) + { + DateTime d = new DateTime(year, month, day, hour, minute, second); + return (type == MySqlDbType.Date) ? d.ToString("d") : d.ToString(); + } + + string dateString = FormatDateCustom( + CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern, month, day, year); + if (type == MySqlDbType.Date) + return dateString; + + DateTime dt = new DateTime(1, 2, 3, hour, minute, second); + dateString = String.Format("{0} {1}", dateString, dt.ToLongTimeString()); + return dateString; + } + /// /// /// Modified: branches/5.1/TestSuite/Source/DateTimeTests.cs =================================================================== --- branches/5.1/TestSuite/Source/DateTimeTests.cs 2007-11-01 17:17:58 UTC (rev 1050) +++ branches/5.1/TestSuite/Source/DateTimeTests.cs 2007-11-01 17:23:56 UTC (rev 1051) @@ -153,7 +153,8 @@ [Test] public void TestAllowZeroDateTime() { - execSQL("INSERT INTO Test (id, d, dt) VALUES (1, '0000-00-00', '0000-00-00 00:00:00')"); + execSQL("TRUNCATE TABLE Test"); + execSQL("INSERT INTO Test (id, d, dt) VALUES (1, '0000-00-00', '0000-00-00 00:00:00')"); MySqlConnection c = new MySqlConnection( conn.ConnectionString + ";pooling=false;AllowZeroDatetime=true"); @@ -466,7 +467,18 @@ if (c != null) c.Close(); } - } - } + } + /// + /// Bug #32010 Connector return incorrect value when pulling 0 datetime + /// + [Test] + public void MySqlDateTimeFormatting() + { + DateTime dt = DateTime.Now; + MySqlDateTime mdt = new MySqlDateTime(dt); + Assert.AreEqual(dt.ToString(), mdt.ToString()); + } + } + }