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 SqlDateTime(long ticks, SqlDayToSecond offset)
            : this()
        {
            var tsOffset = new TimeSpan(0, offset.Hours, offset.Minutes, 0);

            value = new DateTimeOffset(ticks, tsOffset);
        }
        public void DayToSecondToString(int days, int hours, int minutes, int seconds, int millis, string expected)
        {
            var dayToSecond = new SqlDayToSecond(days, hours, minutes, seconds, millis);
            var s           = dayToSecond.ToString();

            Assert.IsNotNullOrEmpty(s);
            Assert.AreEqual(expected, s);
        }
        public void CompareTwoDayToSecond()
        {
            var dts1 = new SqlDayToSecond(22, 11, 23, 0, 678);
            var dts2 = new SqlDayToSecond(0, 3, 22, 15, 877);

            var result = dts1.CompareTo(dts2);

            Assert.AreEqual(1, result);
        }
        public void AddNullDayToSecond()
        {
            var dts1 = new SqlDayToSecond(22, 11, 23, 0, 678);
            var dts2 = SqlDayToSecond.Null;

            var result = dts1.Add(dts2);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.IsNull);
        }
        public SqlDateTime ToUtc()
        {
            if (value == null)
            {
                return(Null);
            }

            var utc    = value.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 void ParseDayToSecond(string s, int days, int hours, int minutes, int seconds, int millis)
        {
            SqlDayToSecond dayToSecond;

            Assert.IsTrue(SqlDayToSecond.TryParse(s, out dayToSecond));
            Assert.IsFalse(dayToSecond.IsNull);

            Assert.AreEqual(days, dayToSecond.Days);
            Assert.AreEqual(hours, dayToSecond.Hours);
            Assert.AreEqual(minutes, dayToSecond.Minutes);
            Assert.AreEqual(seconds, dayToSecond.Seconds);
            Assert.AreEqual(millis, dayToSecond.Milliseconds);
        }
        /// <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)
        {
            if (IsNull)
            {
                return(Null);
            }
            if (interval.IsNull)
            {
                return(this);
            }

            var result = value.Value.AddMilliseconds(-(interval.TotalMilliseconds));

            return(new SqlDateTime(result.Ticks));
        }
Beispiel #9
0
        public void Subtract_TimeSpan_NoDays()
        {
            var value = new SqlDateTime(2001, 01, 03, 10, 22, 03, 0);
            var ts    = new SqlDayToSecond(0, 2, 03, 0);

            var result = new SqlDateTime();

            Assert.DoesNotThrow(() => result = value.Subtract(ts));
            Assert.IsFalse(result.IsNull);
            Assert.AreEqual(2001, result.Year);
            Assert.AreEqual(01, result.Month);
            Assert.AreEqual(03, result.Day);
            Assert.AreEqual(8, result.Hour);
            Assert.AreEqual(19, result.Minute);
        }
Beispiel #10
0
        public void Operator_Add_TimeSpan()
        {
            var value = new SqlDateTime(2001, 01, 03, 10, 22, 03, 0);
            var ts    = new SqlDayToSecond(0, 2, 03, 0);

            var result = new SqlDateTime();

            Assert.DoesNotThrow(() => result = value + ts);
            Assert.IsFalse(result.IsNull);
            Assert.AreEqual(2001, result.Year);
            Assert.AreEqual(01, result.Month);
            Assert.AreEqual(03, result.Day);
            Assert.AreEqual(12, result.Hour);
            Assert.AreEqual(25, result.Minute);
        }
        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 void SubtractDayToSeconds()
        {
            var dts1 = new SqlDayToSecond(22, 11, 23, 0, 678);
            var dts2 = new SqlDayToSecond(0, 3, 22, 15, 877);

            var result = dts1.Subtract(dts2);

            Assert.IsNotNull(result);
            Assert.IsFalse(result.IsNull);

            Assert.AreEqual(22, result.Days);
            Assert.AreEqual(8, result.Hours);
            Assert.AreEqual(0, result.Minutes);
            Assert.AreEqual(44, result.Seconds);
            Assert.AreEqual(801, result.Milliseconds);
        }
        public void AddDayToSecond()
        {
            var dts1 = new SqlDayToSecond(22, 11, 23, 0, 678);
            var dts2 = new SqlDayToSecond(0, 3, 22, 15, 877);

            var result = dts1.Add(dts2);

            Assert.IsNotNull(result);
            Assert.IsFalse(result.IsNull);

            Assert.AreEqual(22, result.Days);
            Assert.AreEqual(14, result.Hours);
            Assert.AreEqual(45, result.Minutes);
            Assert.AreEqual(16, result.Seconds);
            Assert.AreEqual(555, result.Milliseconds);
        }
        public void AddDayToSecondToDate()
        {
            var date = new SqlDateTime(2010, 11, 03, 05, 22, 43, 0);
            var dts  = new SqlDayToSecond(19, 08, 23, 1);

            var result = date.Add(dts);

            Assert.IsNotNull(result);
            Assert.IsFalse(result.IsNull);
            Assert.AreEqual(2010, result.Year);
            Assert.AreEqual(11, result.Month);
            Assert.AreEqual(22, result.Day);
            Assert.AreEqual(13, result.Hour);
            Assert.AreEqual(45, result.Minute);
            Assert.AreEqual(44, result.Second);
            Assert.AreEqual(0, result.Millisecond);
        }
        public void SubtractDayToSecondFromDate()
        {
            var date = new SqlDateTime(2010, 11, 03, 05, 22, 43, 0);
            var dts  = new SqlDayToSecond(19, 08, 23, 1);

            var result = date.Subtract(dts);

            Assert.IsNotNull(result);
            Assert.IsFalse(result.IsNull);

            Assert.AreEqual(2010, result.Year);
            Assert.AreEqual(10, result.Month);
            Assert.AreEqual(14, result.Day);
            Assert.AreEqual(20, result.Hour);
            Assert.AreEqual(59, result.Minute);
            Assert.AreEqual(42, result.Second);
            Assert.AreEqual(0, result.Millisecond);
        }
        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("year");
            }
            if (month <= 0 || month > 12)
            {
                throw new ArgumentOutOfRangeException("month");
            }
            if (day <= 0 || day > 31)
            {
                throw new ArgumentOutOfRangeException("day");
            }

            if (hour < 0 || hour > 23)
            {
                throw new ArgumentOutOfRangeException("hour");
            }
            if (minute < 0 || minute > 59)
            {
                throw new ArgumentOutOfRangeException("minute");
            }
            if (second < 0 || second > 59)
            {
                throw new ArgumentOutOfRangeException("second");
            }
            if (millisecond < 0 || millisecond > 999)
            {
                throw new ArgumentOutOfRangeException("millisecond");
            }

            var tsOffset = new TimeSpan(0, offset.Hours, offset.Minutes, 0, 0);

            value = new DateTimeOffset(year, month, day, hour, minute, second, millisecond, tsOffset);
        }