// valid for DateTime, SmallDateTime, Date, DateTime2 internal void SetDateTime(DateTime value) { Debug.Assert( SmiXetterAccessMap.IsSetterAccessValid(_metaData, SmiXetterTypeCode.XetDateTime)); if (SqlDbType.Variant == _metaData.SqlDbType) { if ((_variantType != null) && (_variantType.SqlDbType == SqlDbType.DateTime2)) { _stateObj.Parser.WriteSqlVariantDateTime2(value, _stateObj); } else if ((_variantType != null) && (_variantType.SqlDbType == SqlDbType.Date)) { _stateObj.Parser.WriteSqlVariantDate(value, _stateObj); } else { TdsDateTime dt = MetaType.FromDateTime(value, 8); _stateObj.Parser.WriteSqlVariantHeader(10, TdsEnums.SQLDATETIME, 0, _stateObj); _stateObj.Parser.WriteInt(dt.days, _stateObj); _stateObj.Parser.WriteInt(dt.time, _stateObj); } // Clean the variant metadata to prevent sharing it with next row. // As a reminder, SetVariantType raises an assert if _variantType is not clean _variantType = null; } else { _stateObj.WriteByte((byte)_metaData.MaxLength); if (SqlDbType.SmallDateTime == _metaData.SqlDbType) { TdsDateTime dt = MetaType.FromDateTime(value, (byte)_metaData.MaxLength); Debug.Assert(0 <= dt.days && dt.days <= ushort.MaxValue, "Invalid DateTime '" + value + "' for SmallDateTime"); _stateObj.Parser.WriteShort(dt.days, _stateObj); _stateObj.Parser.WriteShort(dt.time, _stateObj); } else if (SqlDbType.DateTime == _metaData.SqlDbType) { TdsDateTime dt = MetaType.FromDateTime(value, (byte)_metaData.MaxLength); _stateObj.Parser.WriteInt(dt.days, _stateObj); _stateObj.Parser.WriteInt(dt.time, _stateObj); } else { // date and datetime2 int days = value.Subtract(DateTime.MinValue).Days; if (SqlDbType.DateTime2 == _metaData.SqlDbType) { long time = value.TimeOfDay.Ticks / TdsEnums.TICKS_FROM_SCALE[_metaData.Scale]; _stateObj.WriteByteArray(BitConverter.GetBytes(time), (int)_metaData.MaxLength - 3, 0); } _stateObj.WriteByteArray(BitConverter.GetBytes(days), 3, 0); } } }
public static TdsDateTime FromDateTime(DateTime dateTime, byte cb) { SqlDateTime time2; TdsDateTime time = new TdsDateTime(); if (cb == 8) { time2 = new SqlDateTime(dateTime); time.time = time2.TimeTicks; } else { time2 = new SqlDateTime(dateTime.AddSeconds(30.0)); time.time = time2.TimeTicks / SqlDateTime.SQLTicksPerMinute; } time.days = time2.DayTicks; return(time); }
public static TdsDateTime FromDateTime(DateTime comDateTime, byte cb) { SqlDateTime sqlDateTime = new SqlDateTime(comDateTime); TdsDateTime tds = new TdsDateTime(); Debug.Assert(cb == 8 || cb == 4, "Invalid date time size!"); tds.days = sqlDateTime.DayTicks; if (cb == 8) { tds.time = sqlDateTime.TimeTicks; } else { tds.time = sqlDateTime.TimeTicks / SqlDateTime.SQLTicksPerMinute; } return(tds); }
public static TdsDateTime FromDateTime(DateTime dateTime, byte cb) { SqlDateTime sqlDateTime; TdsDateTime tdsDateTime = new TdsDateTime(); Debug.Assert(cb == 8 || cb == 4, "Invalid date time size!"); if (cb == 8) { sqlDateTime = new SqlDateTime(dateTime); tdsDateTime.time = sqlDateTime.TimeTicks; } else { // note that smalldatetime is days & minutes. // Adding 30 seconds ensures proper roundup if the seconds are >= 30 // The AddSeconds function handles eventual carryover sqlDateTime = new SqlDateTime(dateTime.AddSeconds(30)); tdsDateTime.time = sqlDateTime.TimeTicks / SqlDateTime.SQLTicksPerMinute; } tdsDateTime.days = sqlDateTime.DayTicks; return(tdsDateTime); }
internal void SetDateTime(DateTime value) { if (SqlDbType.Variant == this._metaData.SqlDbType) { TdsDateTime time3 = MetaType.FromDateTime(value, 8); this._stateObj.Parser.WriteSqlVariantHeader(10, 0x3d, 0, this._stateObj); this._stateObj.Parser.WriteInt(time3.days, this._stateObj); this._stateObj.Parser.WriteInt(time3.time, this._stateObj); } else { this._stateObj.Parser.WriteByte((byte)this._metaData.MaxLength, this._stateObj); if (SqlDbType.SmallDateTime == this._metaData.SqlDbType) { TdsDateTime time2 = MetaType.FromDateTime(value, (byte)this._metaData.MaxLength); this._stateObj.Parser.WriteShort(time2.days, this._stateObj); this._stateObj.Parser.WriteShort(time2.time, this._stateObj); } else if (SqlDbType.DateTime == this._metaData.SqlDbType) { TdsDateTime time = MetaType.FromDateTime(value, (byte)this._metaData.MaxLength); this._stateObj.Parser.WriteInt(time.days, this._stateObj); this._stateObj.Parser.WriteInt(time.time, this._stateObj); } else { int days = value.Subtract(DateTime.MinValue).Days; if (SqlDbType.DateTime2 == this._metaData.SqlDbType) { long num = value.TimeOfDay.Ticks / TdsEnums.TICKS_FROM_SCALE[this._metaData.Scale]; this._stateObj.Parser.WriteByteArray(BitConverter.GetBytes(num), ((int)this._metaData.MaxLength) - 3, 0, this._stateObj); } this._stateObj.Parser.WriteByteArray(BitConverter.GetBytes(days), 3, 0, this._stateObj); } } }
public static TdsDateTime FromDateTime(DateTime dateTime, byte cb) { SqlDateTime time2; TdsDateTime time = new TdsDateTime(); if (cb == 8) { time2 = new SqlDateTime(dateTime); time.time = time2.TimeTicks; } else { time2 = new SqlDateTime(dateTime.AddSeconds(30.0)); time.time = time2.TimeTicks / SqlDateTime.SQLTicksPerMinute; } time.days = time2.DayTicks; return time; }
public static TdsDateTime FromDateTime(DateTime dateTime, byte cb) { SqlDateTime sqlDateTime; TdsDateTime tdsDateTime = new TdsDateTime(); Debug.Assert(cb == 8 || cb == 4, "Invalid date time size!"); if (cb == 8) { sqlDateTime = new SqlDateTime(dateTime); tdsDateTime.time = sqlDateTime.TimeTicks; } else { // note that smalldatetime is days&minutes. // Adding 30 seconds ensures proper roundup if the seconds are >= 30 // The AddSeconds function handles eventual carryover sqlDateTime = new SqlDateTime(dateTime.AddSeconds(30)); tdsDateTime.time = sqlDateTime.TimeTicks / SqlDateTime.SQLTicksPerMinute; } tdsDateTime.days = sqlDateTime.DayTicks; return tdsDateTime; }