public static void GetHashCodeTest() { TimeOnly timeOnly1 = TimeOnly.FromDateTime(DateTime.Now); TimeOnly timeOnly2 = timeOnly1.Add(new TimeSpan(1)); TimeOnly timeOnly3 = new TimeOnly(timeOnly1.Ticks); Assert.True(timeOnly1.GetHashCode() == timeOnly3.GetHashCode()); Assert.False(timeOnly1.GetHashCode() == timeOnly2.GetHashCode()); }
/// <summary> /// Get a random <see cref="TimeOnly"/> between <paramref name="start"/> and <paramref name="end"/>. /// </summary> /// <param name="start">Start time</param> /// <param name="end">End time</param> public TimeOnly BetweenTimeOnly(TimeOnly start, TimeOnly end) { var diff = end - start; var diffTicks = diff.Ticks; var part = RandomTimeSpanFromTicks(diffTicks); return(start.Add(part)); }
public static void IsBetweenTest() { TimeOnly to1 = new TimeOnly(14, 30); TimeOnly to2 = new TimeOnly(2, 0); TimeOnly to3 = new TimeOnly(12, 0); Assert.True(to3.IsBetween(to2, to1)); Assert.True(to1.IsBetween(to3, to2)); Assert.True(to3.IsBetween(to3, to1)); Assert.True(to1.IsBetween(to1, to2)); Assert.False(to1.IsBetween(to3, to1)); Assert.False(to2.IsBetween(to3, to2)); Assert.True(to1.IsBetween(to3, to1.Add(new TimeSpan(1)))); Assert.True(to2.IsBetween(to3, to2.Add(new TimeSpan(1)))); }
public static void ComparisonsTest() { TimeOnly timeOnly1 = TimeOnly.FromDateTime(DateTime.Now); TimeOnly timeOnly2 = timeOnly1.Add(new TimeSpan(1)); TimeOnly timeOnly3 = new TimeOnly(timeOnly1.Ticks); Assert.Equal(-1, timeOnly1.CompareTo(timeOnly2)); Assert.Equal(1, timeOnly2.CompareTo(timeOnly1)); Assert.Equal(-1, timeOnly1.CompareTo(timeOnly2)); Assert.Equal(0, timeOnly1.CompareTo(timeOnly3)); Assert.Equal(-1, timeOnly1.CompareTo((object)timeOnly2)); Assert.Equal(1, timeOnly2.CompareTo((object)timeOnly1)); Assert.Equal(-1, timeOnly1.CompareTo((object)timeOnly2)); Assert.Equal(0, timeOnly1.CompareTo((object)timeOnly3)); Assert.True(timeOnly1.Equals(timeOnly3)); Assert.True(timeOnly1.Equals((object)timeOnly3)); Assert.False(timeOnly2.Equals(timeOnly3)); Assert.False(timeOnly2.Equals((object)timeOnly3)); Assert.False(timeOnly2.Equals(null)); Assert.False(timeOnly2.Equals(new object())); }
public static void AddTest() { TimeOnly to = new TimeOnly(1, 10, 20, 900); to = to.Add(new TimeSpan(1)); Assert.Equal(TimeSpan.NanosecondsPerTick, to.Nanosecond); to = to.Add(new TimeSpan(TimeSpan.TicksPerMicrosecond)); Assert.Equal(1, to.Microsecond); to = to.Add(new TimeSpan(TimeSpan.TicksPerMillisecond)); Assert.Equal(901, to.Millisecond); to = to.Add(new TimeSpan(TimeSpan.TicksPerSecond)); Assert.Equal(21, to.Second); to = to.Add(new TimeSpan(TimeSpan.TicksPerMinute)); Assert.Equal(11, to.Minute); to = to.Add(new TimeSpan(TimeSpan.TicksPerHour)); Assert.Equal(2, to.Hour); to = TimeOnly.MinValue.Add(new TimeSpan(-1), out int wrappedDays); Assert.Equal(23, to.Hour); Assert.Equal(59, to.Minute); Assert.Equal(59, to.Second); Assert.Equal(999, to.Millisecond); Assert.Equal(-1, wrappedDays); to = TimeOnly.MinValue.Add(new TimeSpan(48, 0, 0), out wrappedDays); Assert.Equal(0, to.Hour); Assert.Equal(0, to.Minute); Assert.Equal(0, to.Second); Assert.Equal(0, to.Millisecond); Assert.Equal(2, wrappedDays); to = to.Add(new TimeSpan(1, 0, 0), out wrappedDays); Assert.Equal(0, wrappedDays); to = TimeOnly.MinValue.AddHours(1.5); Assert.Equal(1, to.Hour); Assert.Equal(30, to.Minute); Assert.Equal(0, to.Second); Assert.Equal(0, to.Millisecond); Assert.Equal(0, to.Microsecond); Assert.Equal(0, to.Nanosecond); to = to.AddHours(1.5, out wrappedDays); Assert.Equal(3, to.Hour); Assert.Equal(0, to.Minute); Assert.Equal(0, to.Second); Assert.Equal(0, to.Microsecond); Assert.Equal(0, to.Nanosecond); Assert.Equal(0, wrappedDays); to = to.AddHours(-28, out wrappedDays); Assert.Equal(23, to.Hour); Assert.Equal(0, to.Minute); Assert.Equal(-2, wrappedDays); to = to.AddHours(1, out wrappedDays); Assert.Equal(1, wrappedDays); Assert.Equal(0, to.Hour); Assert.Equal(0, to.Minute); to = to.AddMinutes(190.5); Assert.Equal(3, to.Hour); Assert.Equal(10, to.Minute); Assert.Equal(30, to.Second); to = to.AddMinutes(-4 * 60, out wrappedDays); Assert.Equal(23, to.Hour); Assert.Equal(10, to.Minute); Assert.Equal(30, to.Second); Assert.Equal(-1, wrappedDays); to = to.AddMinutes(60.5, out wrappedDays); Assert.Equal(0, to.Hour); Assert.Equal(11, to.Minute); Assert.Equal(0, to.Second); Assert.Equal(1, wrappedDays); }