/// <summary> /// Constructs a new <b>MySqlDateTime</b> object by copying the current value of the given object. /// </summary> /// <param name="mdt">The <b>MySqlDateTime</b> object to copy.</param> public MySqlDateTime(MySqlDateTime mdt) { year = mdt.Year; month = mdt.Month; day = mdt.Day; hour = mdt.Hour; minute = mdt.Minute; second = mdt.Second; millisecond = 0; type = MySqlDbType.DateTime; isNull = false; }
internal static MySqlDateTime Parse(string s, Common.DBVersion version) { MySqlDateTime dt = new MySqlDateTime(); return dt.ParseMySql(s); }
private void SerializeText(MySqlPacket packet, MySqlDateTime value) { string val = String.Empty; val = String.Format("{0:0000}-{1:00}-{2:00}", value.Year, value.Month, value.Day); if (type != MySqlDbType.Date) val = String.Format("{0} {1:00}:{2:00}:{3:00}", val, value.Hour, value.Minute, value.Second); packet.WriteStringNoNull("'" + val + "'"); }
void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object value, int length) { MySqlDateTime dtValue; string valueAsString = value as string; if (value is DateTime) dtValue = new MySqlDateTime(type, (DateTime)value); else if (valueAsString != null) dtValue = new MySqlDateTime(type, DateTime.Parse(valueAsString, CultureInfo.CurrentCulture)); else if (value is MySqlDateTime) dtValue = (MySqlDateTime)value; else throw new MySqlException("Unable to serialize date/time value."); if (!binary) { SerializeText(packet, dtValue); return; } if (type == MySqlDbType.Timestamp) packet.WriteByte(11); else packet.WriteByte(7); packet.WriteInteger(dtValue.Year, 2); packet.WriteByte((byte)dtValue.Month); packet.WriteByte((byte)dtValue.Day); if (type == MySqlDbType.Date) { packet.WriteByte(0); packet.WriteByte(0); packet.WriteByte(0); } else { packet.WriteByte((byte)dtValue.Hour); packet.WriteByte((byte)dtValue.Minute); packet.WriteByte((byte)dtValue.Second); } if (type == MySqlDbType.Timestamp) packet.WriteInteger(dtValue.Millisecond, 4); }
internal static MySqlDateTime Parse(string s) { MySqlDateTime dt = new MySqlDateTime(); return dt.ParseMySql(s); }
public void MySqlDateTimeFormatting() { DateTime dt = DateTime.Now; MySqlDateTime mdt = new MySqlDateTime(dt); Assert.AreEqual(dt.ToString(), mdt.ToString()); }
public void TestAllowZeroDateTime() { execSQL("TRUNCATE TABLE Test"); execSQL("INSERT INTO Test (id, d, dt) VALUES (1, '0000-00-00', '0000-00-00 00:00:00')"); using (MySqlConnection c = new MySqlConnection( conn.ConnectionString + ";pooling=false;AllowZeroDatetime=true")) { c.Open(); MySqlCommand cmd = new MySqlCommand("SELECT * FROM Test", c); using (MySqlDataReader reader = cmd.ExecuteReader()) { reader.Read(); Assert.IsTrue(reader.GetValue(1) is MySqlDateTime); Assert.IsTrue(reader.GetValue(2) is MySqlDateTime); Assert.IsFalse(reader.GetMySqlDateTime(1).IsValidDateTime); Assert.IsFalse(reader.GetMySqlDateTime(2).IsValidDateTime); try { reader.GetDateTime(1); Assert.Fail("This should not succeed"); } catch (MySqlConversionException) { } } DataTable dt = new DataTable(); MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Test", c); MySqlCommandBuilder cb = new MySqlCommandBuilder(da); da.Fill(dt); dt.Rows[0]["id"] = 2; DataRow row = dt.NewRow(); row["id"] = 3; row["d"] = new MySqlDateTime("2003-9-24"); row["dt"] = new MySqlDateTime("0000/0/00 00:00:00"); dt.Rows.Add(row); da.Update(dt); dt.Clear(); da.Fill(dt); Assert.AreEqual(2, dt.Rows.Count); MySqlDateTime date = (MySqlDateTime)dt.Rows[1]["d"]; Assert.AreEqual(2003, date.Year); Assert.AreEqual(9, date.Month); Assert.AreEqual(24, date.Day); cb.Dispose(); } }
public void DateTimeInDataTable() { execSQL("INSERT INTO Test VALUES(1, Now(), '0000-00-00', NULL, NULL)"); using (MySqlConnection c = new MySqlConnection( conn.ConnectionString + ";pooling=false;AllowZeroDatetime=true")) { c.Open(); MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Test", c); MySqlCommandBuilder cb = new MySqlCommandBuilder(da); DataTable dt = new DataTable(); da.Fill(dt); DataRow row = dt.NewRow(); row["id"] = 2; row["dt"] = new MySqlDateTime(DateTime.Now); row["d"] = new MySqlDateTime(DateTime.Now); row["t"] = new TimeSpan(1, 1, 1); row["ts"] = DBNull.Value; dt.Rows.Add(row); da.Update(dt); dt.Rows.Clear(); da.Fill(dt); Assert.AreEqual(2, dt.Rows.Count); cb.Dispose(); } }
/// <summary> /// Enables the construction of a <b>MySqlDateTime</b> object by parsing a string. /// </summary> public MySqlDateTime(string dateTime) : this(MySqlDateTime.Parse(dateTime)) { }
int IComparable.CompareTo(object obj) { MySqlDateTime otherDate = (MySqlDateTime)obj; if (Year < otherDate.Year) { return(-1); } else if (Year > otherDate.Year) { return(1); } if (Month < otherDate.Month) { return(-1); } else if (Month > otherDate.Month) { return(1); } if (Day < otherDate.Day) { return(-1); } else if (Day > otherDate.Day) { return(1); } if (Hour < otherDate.Hour) { return(-1); } else if (Hour > otherDate.Hour) { return(1); } if (Minute < otherDate.Minute) { return(-1); } else if (Minute > otherDate.Minute) { return(1); } if (Second < otherDate.Second) { return(-1); } else if (Second > otherDate.Second) { return(1); } if (Millisecond < otherDate.Millisecond) { return(-1); } else if (Millisecond > otherDate.Millisecond) { return(1); } return(0); }
static internal MySqlDateTime Parse(string s, Common.DBVersion version) { MySqlDateTime dt = new MySqlDateTime(); return(dt.ParseMySql(s)); }
static internal MySqlDateTime Parse(string s) { MySqlDateTime dt = new MySqlDateTime(); return(dt.ParseMySql(s)); }