Add support to compare a date against a date time
This commit is contained in:
@@ -65,6 +65,10 @@ public with sharing class fflib_Comparator
|
|||||||
return compare((Date) object1, (Date) object2);
|
return compare((Date) object1, (Date) object2);
|
||||||
else if (object1 instanceof Datetime && object2 instanceof Datetime)
|
else if (object1 instanceof Datetime && object2 instanceof Datetime)
|
||||||
return compare((Datetime) object1, (Datetime) object2);
|
return compare((Datetime) object1, (Datetime) object2);
|
||||||
|
else if (object1 instanceof Datetime && object2 instanceof Date)
|
||||||
|
return compare((Datetime) object1, (Date) object2);
|
||||||
|
else if (object1 instanceof Date && object2 instanceof Datetime)
|
||||||
|
return compare((Date) object1, (Datetime) object2);
|
||||||
else if (object1 instanceof Integer && object2 instanceof Integer)
|
else if (object1 instanceof Integer && object2 instanceof Integer)
|
||||||
return compare((Integer) object1, (Integer) object2);
|
return compare((Integer) object1, (Integer) object2);
|
||||||
else if (object1 instanceof Long && object2 instanceof Long)
|
else if (object1 instanceof Long && object2 instanceof Long)
|
||||||
@@ -101,6 +105,20 @@ public with sharing class fflib_Comparator
|
|||||||
else return 1;
|
else return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Integer compare(Datetime d1, Date d2)
|
||||||
|
{
|
||||||
|
if (d1 < d2) return -1;
|
||||||
|
else if (d1 == d2) return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer compare(Date d1, Datetime d2)
|
||||||
|
{
|
||||||
|
if (d1 < d2) return -1;
|
||||||
|
else if (d1 == d2) return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
|
||||||
public static Integer compare(Double d1, Double d2)
|
public static Integer compare(Double d1, Double d2)
|
||||||
{
|
{
|
||||||
if (d1 < d2) return -1;
|
if (d1 < d2) return -1;
|
||||||
|
|||||||
@@ -85,8 +85,17 @@ private with sharing class fflib_ComparatorTest
|
|||||||
|
|
||||||
private static testmethod void testDatetime()
|
private static testmethod void testDatetime()
|
||||||
{
|
{
|
||||||
Object d1 = datetime.newInstance(2008, 12, 1, 12, 30, 1);
|
Object d1 = Datetime.newInstance(2010, 01, 1, 12, 30, 0);
|
||||||
Object d2 = datetime.newInstance(2008, 12, 1, 12, 30, 2);
|
Object d2 = Datetime.newInstance(2020, 01, 1, 12, 30, 0);
|
||||||
|
assertCompareDatetime(d1, d2, -1);
|
||||||
|
assertCompareDatetime(d1, d1, 0);
|
||||||
|
assertCompareDatetime(d2, d1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static testmethod void testDatetimeToDate()
|
||||||
|
{
|
||||||
|
Object d1 = Date.newInstance(2010, 01, 1);
|
||||||
|
Object d2 = Datetime.newInstance(2020, 01, 1, 12, 30, 0);
|
||||||
assertCompareDatetime(d1, d2, -1);
|
assertCompareDatetime(d1, d2, -1);
|
||||||
assertCompareDatetime(d1, d1, 0);
|
assertCompareDatetime(d1, d1, 0);
|
||||||
assertCompareDatetime(d2, d1, 1);
|
assertCompareDatetime(d2, d1, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user