Пример #1
0
 /// <summary>
 /// Enables the contruction of a <b>MySqlDateTime</b> object by parsing a string.
 /// </summary>
 public MySqlDateTime(string dateTime)
     : this(MySqlDateTime.Parse(dateTime))
 {
 }
Пример #2
0
        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 = MySqlDateTime.Parse(valueAsString);
            }
            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 (dtValue.Microsecond > 0)
            {
                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 (dtValue.Microsecond > 0)
            {
                long val = dtValue.Microsecond;
                for (int x = 0; x < 4; x++)
                {
                    packet.WriteByte((byte)(val & 0xff));
                    val >>= 8;
                }
            }
        }