public SqlDateTime(long ticks, SqlDayToSecond offset) : this() { var tsOffset = new TimeSpan(0, offset.Hours, offset.Minutes, 0); value = new DateTimeOffset(ticks, tsOffset); }
private ISqlValue ToBinary(SqlDayToSecond dts, SqlBinaryType destType) { var bytes = dts.ToByArray(); var binary = new SqlBinary(bytes); return(destType.NormalizeValue(binary)); }
public SqlDateTime ToUtc() { var utc = value.ToUniversalTime(); var offset = new SqlDayToSecond(utc.Offset.Days, utc.Offset.Hours, utc.Offset.Minutes); return(new SqlDateTime(utc.Year, utc.Month, utc.Day, utc.Hour, utc.Minute, utc.Second, utc.Millisecond, offset)); }
public static Field Date(DateTimeOffset value) { var offset = new SqlDayToSecond(value.Offset.Days, value.Offset.Hours, value.Offset.Minutes, value.Offset.Seconds, value.Offset.Milliseconds); var sqlDate = new SqlDateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, value.Millisecond, offset); return(Date(sqlDate)); }
public static void Serialize(int days, int hours, int minutes, int seconds, int millis) { var dts = new SqlDayToSecond(days, hours, minutes, seconds, millis); var result = BinarySerializeUtil.Serialize(dts); Assert.Equal(dts, result); }
public static void Negate(int days, int hours, int minutes, int seconds, int millis, string expected) { var dts = new SqlDayToSecond(days, hours, minutes, seconds, millis); var result = -dts; Assert.Equal(expected, result.ToString()); }
public static bool TryParseTimeStamp(string s, TimeZoneInfo timeZone, out SqlDateTime value) { value = new SqlDateTime(); // We delegate parsing DATE and TIME strings to the .NET DateTime object... DateTimeOffset date; if (DateTimeOffset.TryParseExact(s, SqlTimeStampFormats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out date)) { SqlDayToSecond offset; if (timeZone != null) { var utcOffset = timeZone.GetUtcOffset(date); offset = new SqlDayToSecond(utcOffset.Hours, utcOffset.Minutes, 0); } else { offset = new SqlDayToSecond(date.Offset.Hours, date.Offset.Minutes, 0); } value = new SqlDateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Millisecond, offset); return(true); } return(false); }
public static void GetString(int days, int hours, int minutes, int seconds, int millis, string expected) { var dts = new SqlDayToSecond(days, hours, minutes, seconds, millis); var s = dts.ToString(); Assert.Equal(expected, s); }
public static void Compare_ToSqlDayToSecond(string dts1, string dts2, int expected) { var d1 = SqlDayToSecond.Parse(dts1); var d2 = SqlDayToSecond.Parse(dts2); var result = d1.CompareTo(d2); Assert.Equal(expected, result); }
public static void Equal(string dts1, string dts2, bool expected) { var d1 = SqlDayToSecond.Parse(dts1); var d2 = SqlDayToSecond.Parse(dts2); var result = d1 == d2; Assert.Equal(expected, result); }
public static void FromFullForm(int days, int hours, int minutes, int seconds, int millis) { var dts = new SqlDayToSecond(days, hours, minutes, seconds, millis); Assert.Equal(days, dts.Days); Assert.Equal(hours, dts.Hours); Assert.Equal(minutes, dts.Minutes); Assert.Equal(seconds, dts.Seconds); Assert.Equal(millis, dts.Milliseconds); Assert.True(dts.TotalMilliseconds > 0); }
public static void Subtract(string dts1, string dts2, string expected) { var d1 = SqlDayToSecond.Parse(dts1); var d2 = SqlDayToSecond.Parse(dts2); var result = d1 - d2; var expectedResult = SqlDayToSecond.Parse(expected); Assert.Equal(expectedResult, result); }
private ISqlValue ToDayToSecond(SqlString value) { SqlDayToSecond dts; if (!SqlDayToSecond.TryParse(value.Value, out dts)) { return(SqlNull.Value); } return(dts); }
public static void TryParse(string s, int days, int hours, int minutes, int seconds, int millis) { SqlDayToSecond dts; Assert.True(SqlDayToSecond.TryParse(s, out dts)); Assert.Equal(days, dts.Days); Assert.Equal(hours, dts.Hours); Assert.Equal(minutes, dts.Minutes); Assert.Equal(seconds, dts.Seconds); Assert.Equal(millis, dts.Milliseconds); }
private static void BinaryWithInterval(Func<SqlDateTimeType, Func<ISqlValue, ISqlValue, ISqlValue>> selector, string date, string offset, string expected) { var type = new SqlDateTimeType(SqlTypeCode.DateTime); var sqlDate = SqlDateTime.Parse(date); var dts = SqlDayToSecond.Parse(offset); var op = selector(type); var result = op(sqlDate, dts); var expectedResult = SqlDateTime.Parse(expected); Assert.Equal(expectedResult, result); }
public static void CastToString(string value, SqlTypeCode destTypeCode, int size, string expexted) { var dts = SqlDayToSecond.Parse(value); var type = new SqlDayToSecondType(); var destType = PrimitiveTypes.Type(destTypeCode, new { size }); var result = type.Cast(dts, destType); var exp = SqlValueUtil.FromObject(expexted); Assert.NotNull(result); Assert.Equal(exp, result); }
public static void Add(string dts1, string dts2, string expected) { var d1 = SqlDayToSecond.Parse(dts1); var d2 = SqlDayToSecond.Parse(dts2); var result = d1 + d2; Assert.NotNull(result); var expectedResult = SqlDayToSecond.Parse(expected); Assert.Equal(expectedResult, result); }
private static void BinaryOp(Func <SqlDateTime, SqlDayToSecond, SqlDateTime> op, string s1, string s2, string expected) { var date = SqlDateTime.Parse(s1); var date2 = SqlDayToSecond.Parse(s2); var result = op(date, date2); var expectedResult = SqlDateTime.Parse(expected); Assert.Equal(expectedResult, result); }
public static void SerializeValue(string s) { var dts = SqlDayToSecond.Parse(s); var type = new SqlDayToSecondType(); var stream = new MemoryStream(); type.Serialize(stream, dts); stream.Seek(0, SeekOrigin.Begin); var result = type.Deserialize(stream); Assert.Equal(dts, result); }
private static void Binary(Func <SqlType, Func <ISqlValue, ISqlValue, ISqlValue> > selector, string value1, string value2, string expected) { var type = new SqlDayToSecondType(); var a = SqlDayToSecond.Parse(value1); var b = SqlDayToSecond.Parse(value2); var op = selector(type); var result = op(a, b); var exp = SqlDayToSecond.Parse(expected); Assert.Equal(exp, result); }
public static void CastToBinary(string value, SqlTypeCode destTypeCode, int size) { var dts = SqlDayToSecond.Parse(value); var type = new SqlDayToSecondType(); var destType = PrimitiveTypes.Type(destTypeCode, new { size }); var result = type.Cast(dts, destType); Assert.IsType <SqlBinary>(result); var binary = (SqlBinary)result; var bytes = binary.ToByteArray(); var back = new SqlDayToSecond(bytes); Assert.Equal(dts, back); }
public static void GetBinary(int days, int hours, int minutes, int seconds, int millis) { var dts = new SqlDayToSecond(days, hours, minutes, seconds, millis); var bytes = dts.ToByArray(); Assert.NotNull(bytes); Assert.Equal(20, bytes.Length); var back = new SqlDayToSecond(bytes); Assert.Equal(dts.Days, back.Days); Assert.Equal(dts.Hours, back.Hours); Assert.Equal(dts.Minutes, back.Minutes); Assert.Equal(dts.Seconds, back.Seconds); Assert.Equal(dts.Milliseconds, back.Milliseconds); }
public static void Serialize(int year, int month, int day, int hour, int minute, int second, int millis, int offsetHour, int offsetMinute) { var offset = new SqlDayToSecond(offsetHour, offsetMinute, 0); var date = new SqlDateTime(year, month, day, hour, minute, second, millis, offset); var result = BinarySerializeUtil.Serialize(date); Assert.Equal(date, result); }
public static bool TryParseTime(string s, out SqlDateTime value) { value = new SqlDateTime(); // We delegate parsing DATE and TIME strings to the .NET DateTime object... DateTimeOffset date; if (DateTimeOffset.TryParseExact(s, SqlTimeFormats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out date)) { var offset = new SqlDayToSecond(date.Offset.Hours, date.Offset.Minutes, 0); value = new SqlDateTime(1, 1, 1, date.Hour, date.Minute, date.Second, date.Millisecond, offset); return(true); } return(false); }
public static void FromFullForm(int year, int month, int day, int hour, int minute, int second, int millis, int offsetHour, int offsetMinute) { var offset = new SqlDayToSecond(offsetHour, offsetMinute, 0); var date = new SqlDateTime(year, month, day, hour, minute, second, millis, offset); Assert.Equal(year, date.Year); Assert.Equal(month, date.Month); Assert.Equal(day, date.Day); Assert.Equal(hour, date.Hour); Assert.Equal(minute, date.Minute); Assert.Equal(second, date.Second); Assert.Equal(millis, date.Millisecond); Assert.Equal(offset, date.Offset); }
public static void FullFormToBytes(int year, int month, int day, int hour, int minute, int second, int millis, int offsetHour, int offsetMinute) { var offset = new SqlDayToSecond(offsetHour, offsetMinute, 0); var date = new SqlDateTime(year, month, day, hour, minute, second, millis, offset); var bytes = date.ToByteArray(true); Assert.NotNull(bytes); Assert.Equal(13, bytes.Length); var back = new SqlDateTime(bytes); Assert.Equal(date, back); }
/// <summary> /// Constructs a new SQL date-time with full information /// </summary> /// <param name="year">The year part of the date (must between 0 and 9999)</param> /// <param name="month">The month part of the date (must be between 1 and 12)</param> /// <param name="day">The day part of the date (must be between 1 and 31)</param> /// <param name="hour">The hour part of the time (must be between 0 and 23)</param> /// <param name="minute">The minute part of the time (must be between 0 and 59)</param> /// <param name="second">The second part of the time (must be between 0 and 59)</param> /// <param name="millisecond">The milliseconds part of the time (must be between 0 and 999)</param> /// <param name="offset">The offset of the date from the UTC</param> /// <exception cref="ArgumentOutOfRangeException">If either one of the arguments /// is not in a valid range of values</exception> public SqlDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, SqlDayToSecond offset) : this() { if (year <= 0 || year > 9999) { throw new ArgumentOutOfRangeException(nameof(year)); } if (month <= 0 || month > 12) { throw new ArgumentOutOfRangeException(nameof(month)); } if (day <= 0 || day > 31) { throw new ArgumentOutOfRangeException(nameof(day)); } if (hour < 0 || hour > 23) { throw new ArgumentOutOfRangeException(nameof(hour)); } if (minute < 0 || minute > 59) { throw new ArgumentOutOfRangeException(nameof(minute)); } if (second < 0 || second > 59) { throw new ArgumentOutOfRangeException(nameof(second)); } if (millisecond < 0 || millisecond > 999) { throw new ArgumentOutOfRangeException(nameof(millisecond)); } var tsOffset = new TimeSpan(0, offset.Hours, offset.Minutes, 0, 0); value = new DateTimeOffset(year, month, day, hour, minute, second, millisecond, tsOffset); }
private static bool TryParseValue(SqlTypeCode typeCode, string s, IFormatProvider provider, out ISqlValue outValue, out Exception error) { try { switch (typeCode) { case SqlTypeCode.TinyInt: case SqlTypeCode.SmallInt: case SqlTypeCode.Integer: case SqlTypeCode.BigInt: case SqlTypeCode.Real: case SqlTypeCode.Double: case SqlTypeCode.Decimal: case SqlTypeCode.Float: case SqlTypeCode.Numeric: case SqlTypeCode.VarNumeric: { error = null; var result = SqlNumber.TryParse(s, provider, out var value); outValue = value; return(result); } case SqlTypeCode.Bit: case SqlTypeCode.Boolean: { error = null; var result = SqlBoolean.TryParse(s, out var value); outValue = value; return(result); } case SqlTypeCode.Date: case SqlTypeCode.DateTime: case SqlTypeCode.TimeStamp: { error = null; var result = SqlDateTime.TryParse(s, out var value); outValue = value; return(result); } case SqlTypeCode.YearToMonth: { error = null; var result = SqlYearToMonth.TryParse(s, out var value); outValue = value; return(result); } case SqlTypeCode.DayToSecond: { error = null; var result = SqlDayToSecond.TryParse(s, out var value); outValue = value; return(result); } default: { error = new FormatException($"The type {typeCode} does not support parsing"); outValue = null; return(false); } } } catch (Exception ex) { error = ex; outValue = null; return(false); } }
public static void CastToDayToSecond(string s, string expected) { var expectedResult = SqlDayToSecond.Parse(expected); Cast(s, SqlTypeCode.DayToSecond, -1, -1, expectedResult); }
/// <summary> /// Subtracts a given interval of time from this date. /// </summary> /// <param name="interval">The interval to subtract from this date.</param> /// <returns> /// Returns an instance of <see cref="SqlDateTime"/> that is the result /// of the subtraction of the given interval of time from this date value. /// </returns> /// <seealso cref="SqlDayToSecond"/> public SqlDateTime Subtract(SqlDayToSecond interval) { var result = value.AddMilliseconds(-(interval.TotalMilliseconds)); return(new SqlDateTime(result.Ticks)); }