public void CompareTwoDateInDiffTZ () { DateTimeOffset dt1 = new DateTimeOffset (2007, 12, 16, 15, 06, 00, new TimeSpan (1, 0, 0)); DateTimeOffset dt2 = new DateTimeOffset (2007, 12, 16, 9, 06, 00, new TimeSpan (-5, 0, 0)); DateTimeOffset dt3 = new DateTimeOffset (2007, 12, 16, 14, 06, 00, new TimeSpan (1, 0, 0)); object o = dt1; Assert.IsTrue (dt1.CompareTo (dt2) == 0); Assert.IsTrue (DateTimeOffset.Compare (dt1, dt2) == 0); Assert.IsTrue (dt1 == dt2); Assert.IsTrue (dt1.Equals (dt2)); Assert.IsFalse (dt1 == dt3); Assert.IsTrue (dt1 != dt3); Assert.IsFalse (dt1.EqualsExact (dt2)); Assert.IsTrue (dt1.CompareTo (dt3) > 0); Assert.IsTrue (((IComparable)dt1).CompareTo (o) == 0); }
public static DateTimeOffset Max( DateTimeOffset dateTimeOffset, DateTimeOffset otherDateTimeOffset) => dateTimeOffset.CompareTo(otherDateTimeOffset) >= 0 ? dateTimeOffset : otherDateTimeOffset;
public static void Compare(DateTimeOffset dateTimeOffset1, DateTimeOffset dateTimeOffset2, int expected) { Assert.Equal(expected, Math.Sign(dateTimeOffset1.CompareTo(dateTimeOffset2))); Assert.Equal(expected, Math.Sign(DateTimeOffset.Compare(dateTimeOffset1, dateTimeOffset2))); IComparable comparable = dateTimeOffset1; Assert.Equal(expected, Math.Sign(comparable.CompareTo(dateTimeOffset2))); if (expected > 0) { Assert.True(dateTimeOffset1 > dateTimeOffset2); Assert.Equal(expected >= 0, dateTimeOffset1 >= dateTimeOffset2); Assert.False(dateTimeOffset1 < dateTimeOffset2); Assert.Equal(expected == 0, dateTimeOffset1 <= dateTimeOffset2); } else if (expected < 0) { Assert.False(dateTimeOffset1 > dateTimeOffset2); Assert.Equal(expected == 0, dateTimeOffset1 >= dateTimeOffset2); Assert.True(dateTimeOffset1 < dateTimeOffset2); Assert.Equal(expected <= 0, dateTimeOffset1 <= dateTimeOffset2); } else if (expected == 0) { Assert.False(dateTimeOffset1 > dateTimeOffset2); Assert.True(dateTimeOffset1 >= dateTimeOffset2); Assert.False(dateTimeOffset1 < dateTimeOffset2); Assert.True(dateTimeOffset1 <= dateTimeOffset2); } }
private static int Compare(JTokenType valueType, object objA, object objB) { if (objA == null && objB == null) { return(0); } if (objA != null && objB == null) { return(1); } if (objA == null && objB != null) { return(-1); } switch (valueType) { case JTokenType.Integer: if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) { return(Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture))); } else if (objA is float || objB is float || objA is double || objB is double) { return(CompareFloat(objA, objB)); } else { return(Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture))); } case JTokenType.Float: return(CompareFloat(objA, objB)); case JTokenType.Comment: case JTokenType.String: case JTokenType.Raw: string s1 = Convert.ToString(objA, CultureInfo.InvariantCulture); string s2 = Convert.ToString(objB, CultureInfo.InvariantCulture); return(string.CompareOrdinal(s1, s2)); case JTokenType.Boolean: bool b1 = Convert.ToBoolean(objA, CultureInfo.InvariantCulture); bool b2 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture); return(b1.CompareTo(b2)); case JTokenType.Date: if (objA is DateTime) { DateTime date1 = Convert.ToDateTime(objA, CultureInfo.InvariantCulture); DateTime date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture); return(date1.CompareTo(date2)); } else { if (!(objB is DateTimeOffset)) { throw new ArgumentException("Object must be of type DateTimeOffset."); } DateTimeOffset date1 = (DateTimeOffset)objA; DateTimeOffset date2 = (DateTimeOffset)objB; return(date1.CompareTo(date2)); } case JTokenType.Bytes: if (!(objB is byte[])) { throw new ArgumentException("Object must be of type byte[]."); } byte[] bytes1 = objA as byte[]; byte[] bytes2 = objB as byte[]; if (bytes1 == null) { return(-1); } if (bytes2 == null) { return(1); } return(MiscellaneousUtils.ByteArrayCompare(bytes1, bytes2)); case JTokenType.Guid: if (!(objB is Guid)) { Guid guid; if (Guid.TryParse((string)objB, out guid) == false) { throw new ArgumentException("Object must be of type Guid."); } objB = guid; } Guid guid1 = (Guid)objA; Guid guid2 = (Guid)objB; return(guid1.CompareTo(guid2)); case JTokenType.Uri: if (objB is string) { objB = new Uri((string)objB, UriKind.RelativeOrAbsolute); } if (!(objB is Uri)) { throw new ArgumentException("Object must be of type Uri."); } Uri uri1 = (Uri)objA; Uri uri2 = (Uri)objB; return(Comparer <string> .Default.Compare(uri1.ToString(), uri2.ToString())); case JTokenType.TimeSpan: if (!(objB is TimeSpan)) { throw new ArgumentException("Object must be of type TimeSpan."); } TimeSpan ts1 = (TimeSpan)objA; TimeSpan ts2 = (TimeSpan)objB; return(ts1.CompareTo(ts2)); default: throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, StringUtils.FormatWith("Unexpected value type: {0}", CultureInfo.InvariantCulture, valueType)); } }
/// <summary> /// A T extension method that check if the value is between (exclusif) the minValue and maxValue. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="minValue">The minimum value.</param> /// <param name="maxValue">The maximum value.</param> /// <returns>true if the value is between the minValue and maxValue, otherwise false.</returns> /// ### /// <typeparam name="T">Generic type parameter.</typeparam> public static bool Between(this DateTimeOffset @this, DateTimeOffset minValue, DateTimeOffset maxValue) { return minValue.CompareTo(@this) == -1 && @this.CompareTo(maxValue) == -1; }
internal static int Compare(JTokenType valueType, object objA, object objB) { if (objA == objB) { return(0); } if (objB == null) { return(1); } if (objA == null) { return(-1); } switch (valueType) { case JTokenType.Integer: #if HAVE_BIG_INTEGER if (objA is BigInteger) { return(CompareBigInteger((BigInteger)objA, objB)); } if (objB is BigInteger) { return(-CompareBigInteger((BigInteger)objB, objA)); } #endif if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) { return(Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture))); } else if (objA is float || objB is float || objA is double || objB is double) { return(CompareFloat(objA, objB)); } else { return(Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture))); } case JTokenType.Float: #if HAVE_BIG_INTEGER if (objA is BigInteger) { return(CompareBigInteger((BigInteger)objA, objB)); } if (objB is BigInteger) { return(-CompareBigInteger((BigInteger)objB, objA)); } #endif if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) { return(Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture))); } return(CompareFloat(objA, objB)); case JTokenType.Comment: case JTokenType.String: case JTokenType.Raw: string s1 = Convert.ToString(objA, CultureInfo.InvariantCulture); string s2 = Convert.ToString(objB, CultureInfo.InvariantCulture); return(string.CompareOrdinal(s1, s2)); case JTokenType.Boolean: bool b1 = Convert.ToBoolean(objA, CultureInfo.InvariantCulture); bool b2 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture); return(b1.CompareTo(b2)); case JTokenType.Date: #if HAVE_DATE_TIME_OFFSET if (objA is DateTime) { #endif DateTime date1 = (DateTime)objA; DateTime date2; #if HAVE_DATE_TIME_OFFSET if (objB is DateTimeOffset) { date2 = ((DateTimeOffset)objB).DateTime; } else #endif { date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture); } return(date1.CompareTo(date2)); #if HAVE_DATE_TIME_OFFSET } else { DateTimeOffset date1 = (DateTimeOffset)objA; DateTimeOffset date2; if (objB is DateTimeOffset) { date2 = (DateTimeOffset)objB; } else { date2 = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture)); } return(date1.CompareTo(date2)); } #endif case JTokenType.Bytes: byte[] bytes2 = objB as byte[]; if (bytes2 == null) { throw new ArgumentException("Object must be of type byte[]."); } byte[] bytes1 = objA as byte[]; Debug.Assert(bytes1 != null); return(MiscellaneousUtils.ByteArrayCompare(bytes1, bytes2)); case JTokenType.Guid: if (!(objB is Guid)) { throw new ArgumentException("Object must be of type Guid."); } Guid guid1 = (Guid)objA; Guid guid2 = (Guid)objB; return(guid1.CompareTo(guid2)); case JTokenType.Uri: Uri uri2 = objB as Uri; if (uri2 == null) { throw new ArgumentException("Object must be of type Uri."); } Uri uri1 = (Uri)objA; return(Comparer <string> .Default.Compare(uri1.ToString(), uri2.ToString())); case JTokenType.TimeSpan: if (!(objB is TimeSpan)) { throw new ArgumentException("Object must be of type TimeSpan."); } TimeSpan ts1 = (TimeSpan)objA; TimeSpan ts2 = (TimeSpan)objB; return(ts1.CompareTo(ts2)); #if (JSON_SmallDecSupport) case JTokenType.SmallDec: SmallDec Value01 = (SmallDec)objA; SmallDec Value02 = (SmallDec)objB; return(Value01.CompareTo(Value02)); #endif default: throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(valueType), valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType)); } }
int IComparable <JsonDate> .CompareTo(JsonDate other) { return(value.CompareTo(other.value)); }
/// <summary> /// A T extension method that check if the value is between (exclusif) the minValue and maxValue. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="minValue">The minimum value.</param> /// <param name="maxValue">The maximum value.</param> /// <returns>true if the value is between the minValue and maxValue, otherwise false.</returns> public static bool Between(this DateTimeOffset @this, DateTimeOffset minValue, DateTimeOffset maxValue) { return(minValue.CompareTo(@this) == -1 && @this.CompareTo(maxValue) == -1); }
/// <summary> /// A T extension method that check if the value is between inclusively the minValue and maxValue. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="minValue">The minimum value.</param> /// <param name="maxValue">The maximum value.</param> /// <returns>true if the value is between inclusively the minValue and maxValue, otherwise false.</returns> public static bool InRange(this DateTimeOffset @this, DateTimeOffset minValue, DateTimeOffset maxValue) { return(@this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0); }
private static int Compare(JTokenType valueType, object objA, object objB) { if (objA == null && objB == null) { return(0); } if (objA != null && objB == null) { return(1); } if (objA == null && objB != null) { return(-1); } switch (valueType) { case JTokenType.Integer: if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) { return(Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture))); } else if (objA is float || objB is float || objA is double || objB is double) { return(CompareFloat(objA, objB)); } else { return(Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture))); } case JTokenType.Float: return(CompareFloat(objA, objB)); case JTokenType.Comment: case JTokenType.String: case JTokenType.Raw: string s1 = Convert.ToString(objA, CultureInfo.InvariantCulture); string s2 = Convert.ToString(objB, CultureInfo.InvariantCulture); return(s1.CompareTo(s2)); case JTokenType.Boolean: bool b1 = Convert.ToBoolean(objA, CultureInfo.InvariantCulture); bool b2 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture); return(b1.CompareTo(b2)); case JTokenType.Date: if (objA is DateTime) { DateTime date1 = Convert.ToDateTime(objA, CultureInfo.InvariantCulture); DateTime date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture); return(date1.CompareTo(date2)); } else { if (!(objB is DateTimeOffset)) { throw new ArgumentException("Object must be of type DateTimeOffset."); } DateTimeOffset date1 = (DateTimeOffset)objA; DateTimeOffset date2 = (DateTimeOffset)objB; return(date1.CompareTo(date2)); } case JTokenType.Bytes: if (!(objB is byte[])) { throw new ArgumentException("Object must be of type byte[]."); } byte[] bytes1 = objA as byte[]; byte[] bytes2 = objB as byte[]; if (bytes1 == null) { return(-1); } if (bytes2 == null) { return(1); } return(MiscellaneousUtils.ByteArrayCompare(bytes1, bytes2)); default: throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType)); } }
internal static int Compare(JTokenType valueType, object objA, object objB) { if (objA == null && objB == null) { return(0); } if (objA != null && objB == null) { return(1); } if (objA == null && objB != null) { return(-1); } switch (valueType) { case JTokenType.Integer: #if !(NET20 || NET35 || PORTABLE40 || PORTABLE) if (objA is BigInteger) { return(CompareBigInteger((BigInteger)objA, objB)); } if (objB is BigInteger) { return(-CompareBigInteger((BigInteger)objB, objA)); } #endif if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) { return(Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture))); } else if (objA is float || objB is float || objA is double || objB is double) { return(CompareFloat(objA, objB)); } else { return(Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture))); } case JTokenType.Float: #if !(NET20 || NET35 || PORTABLE40 || PORTABLE) if (objA is BigInteger) { return(CompareBigInteger((BigInteger)objA, objB)); } if (objB is BigInteger) { return(-CompareBigInteger((BigInteger)objB, objA)); } #endif return(CompareFloat(objA, objB)); case JTokenType.Comment: case JTokenType.String: case JTokenType.Raw: string s1 = Convert.ToString(objA, CultureInfo.InvariantCulture); string s2 = Convert.ToString(objB, CultureInfo.InvariantCulture); return(string.CompareOrdinal(s1, s2)); case JTokenType.Boolean: bool b1 = Convert.ToBoolean(objA, CultureInfo.InvariantCulture); bool b2 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture); return(b1.CompareTo(b2)); case JTokenType.Date: #if !NET20 if (objA is DateTime) { #endif DateTime date1 = (DateTime)objA; DateTime date2; #if !NET20 if (objB is DateTimeOffset) { date2 = ((DateTimeOffset)objB).DateTime; } else #endif date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture); return(date1.CompareTo(date2)); #if !NET20 } else { DateTimeOffset date1 = (DateTimeOffset)objA; DateTimeOffset date2; if (objB is DateTimeOffset) { date2 = (DateTimeOffset)objB; } else { date2 = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture)); } return(date1.CompareTo(date2)); } #endif case JTokenType.Bytes: if (!(objB is byte[])) { throw new ArgumentException("Object must be of type byte[]."); } byte[] bytes1 = objA as byte[]; byte[] bytes2 = objB as byte[]; if (bytes1 == null) { return(-1); } if (bytes2 == null) { return(1); } return(MiscellaneousUtils.ByteArrayCompare(bytes1, bytes2)); case JTokenType.Guid: if (!(objB is Guid)) { throw new ArgumentException("Object must be of type Guid."); } Guid guid1 = (Guid)objA; Guid guid2 = (Guid)objB; return(guid1.CompareTo(guid2)); case JTokenType.Uri: if (!(objB is Uri)) { throw new ArgumentException("Object must be of type Uri."); } Uri uri1 = (Uri)objA; Uri uri2 = (Uri)objB; return(Comparer <string> .Default.Compare(uri1.ToString(), uri2.ToString())); case JTokenType.TimeSpan: if (!(objB is TimeSpan)) { throw new ArgumentException("Object must be of type TimeSpan."); } TimeSpan ts1 = (TimeSpan)objA; TimeSpan ts2 = (TimeSpan)objB; return(ts1.CompareTo(ts2)); default: throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType)); } }
/// <summary> /// https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.compare?view=netcore-3.1. /// < (less than) 0 - the first is earlier than the second. /// = (equal to) 0 - the first is equal the second. /// > (greater than) 0 - the first is later than the second. /// </summary> /// <param name="first">First.</param> /// <param name="second">Second.</param> /// <returns>Compare result.</returns> private static int Compare(DateTimeOffset first, DateTimeOffset second) { return(first.CompareTo(second)); }
/// <summary>Compares to.</summary> /// <param name="other">The other.</param> /// <returns>System.Int32.</returns> public int CompareTo(PersonProper other) { if (other == null) { return(1); } int result = 0; result = _address1.CompareTo(other._address1); if (result != 0) { return(result); } result = _address2.CompareTo(other._address2); if (result != 0) { return(result); } result = _bornOn.CompareTo(other._bornOn); if (result != 0) { return(result); } result = _cellPhone.CompareTo(other._cellPhone); if (result != 0) { return(result); } result = _city.CompareTo(other._city); if (result != 0) { return(result); } result = _country.CompareTo(other._country); if (result != 0) { return(result); } result = _email.CompareTo(other._email); if (result != 0) { return(result); } result = _firstName.CompareTo(other._firstName); if (result != 0) { return(result); } result = _homePhone.CompareTo(other._homePhone); if (result != 0) { return(result); } result = _id.CompareTo(other._id); if (result != 0) { return(result); } result = _lastName.CompareTo(other._lastName); if (result != 0) { return(result); } result = _postalCode.CompareTo(other._postalCode); if (result != 0) { return(result); } return(result); }
internal static int Compare(JTokenType valueType, object objA, object objB) { if (objA == objB) { return 0; } if (objB == null) { return 1; } if (objA == null) { return -1; } switch (valueType) { case JTokenType.Integer: { #if HAVE_BIG_INTEGER if (objA is BigInteger integerA) { return CompareBigInteger(integerA, objB); } if (objB is BigInteger integerB) { return -CompareBigInteger(integerB, objA); } #endif if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) { return Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture)); } else if (objA is float || objB is float || objA is double || objB is double) { return CompareFloat(objA, objB); } else { return Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture)); } } case JTokenType.Float: { #if HAVE_BIG_INTEGER if (objA is BigInteger integerA) { return CompareBigInteger(integerA, objB); } if (objB is BigInteger integerB) { return -CompareBigInteger(integerB, objA); } #endif if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) { return Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture)); } return CompareFloat(objA, objB); } case JTokenType.Comment: case JTokenType.String: case JTokenType.Raw: string s1 = Convert.ToString(objA, CultureInfo.InvariantCulture); string s2 = Convert.ToString(objB, CultureInfo.InvariantCulture); return string.CompareOrdinal(s1, s2); case JTokenType.Boolean: bool b1 = Convert.ToBoolean(objA, CultureInfo.InvariantCulture); bool b2 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture); return b1.CompareTo(b2); case JTokenType.Date: #if HAVE_DATE_TIME_OFFSET if (objA is DateTime dateA) { #else DateTime dateA = (DateTime)objA; #endif DateTime dateB; #if HAVE_DATE_TIME_OFFSET if (objB is DateTimeOffset offsetB) { dateB = offsetB.DateTime; } else #endif { dateB = Convert.ToDateTime(objB, CultureInfo.InvariantCulture); } return dateA.CompareTo(dateB); #if HAVE_DATE_TIME_OFFSET } else { DateTimeOffset offsetA = (DateTimeOffset)objA; if (!(objB is DateTimeOffset offsetB)) { offsetB = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture)); } return offsetA.CompareTo(offsetB); } #endif case JTokenType.Bytes: if (!(objB is byte[] bytesB)) { throw new ArgumentException("Object must be of type byte[]."); } byte[] bytesA = objA as byte[]; Debug.Assert(bytesA != null); return MiscellaneousUtils.ByteArrayCompare(bytesA, bytesB); case JTokenType.Guid: if (!(objB is Guid)) { throw new ArgumentException("Object must be of type Guid."); } Guid guid1 = (Guid)objA; Guid guid2 = (Guid)objB; return guid1.CompareTo(guid2); case JTokenType.Uri: Uri uri2 = objB as Uri; if (uri2 == null) { throw new ArgumentException("Object must be of type Uri."); } Uri uri1 = (Uri)objA; return Comparer<string>.Default.Compare(uri1.ToString(), uri2.ToString()); case JTokenType.TimeSpan: if (!(objB is TimeSpan)) { throw new ArgumentException("Object must be of type TimeSpan."); } TimeSpan ts1 = (TimeSpan)objA; TimeSpan ts2 = (TimeSpan)objB; return ts1.CompareTo(ts2); default: throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(valueType), valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType)); } }