Search in sources :

Example 6 with TimeDuration

use of org.apache.ofbiz.base.util.TimeDuration in project ofbiz-framework by apache.

the class TimeDurationTests method testDuration.

public void testDuration() throws Exception {
    Calendar now = Calendar.getInstance();
    TimeDuration zeroDuration = TimeDuration.ZeroTimeDuration;
    assertFalse("zero equals null", zeroDuration.equals(null));
    Calendar newTime = (Calendar) now.clone();
    zeroDuration.addToCalendar(newTime);
    assertEquals("zero same calendar", now, newTime);
    assertDurationFields("zero(same zero calendar)", 0, 0, 0, 0, 0, 0, 0, "0:0:0:0:0:0:0", new TimeDuration(zero, zero), false, true);
    assertDurationFields("zero(same now calendar)", 0, 0, 0, 0, 0, 0, 0, "0:0:0:0:0:0:0", new TimeDuration(now, now), false, true);
    assertDurationFields("zero(empty parse)", 0, 0, 0, 0, 0, 0, 0, "0:0:0:0:0:0:0", TimeDuration.parseDuration(""), false, true);
    assertDurationFields("zero(zero parse)", 0, 0, 0, 0, 0, 0, 0, "0:0:0:0:0:0:0", TimeDuration.parseDuration("0:0:0:0:0:0:0"), false, true);
    assertDurationFields("zero(from null number)", 0, 0, 0, 0, 0, 0, 0, "0:0:0:0:0:0:0", TimeDuration.fromNumber(null), false, true);
    assertDurationFields("zero(from null number)", 0, 0, 0, 0, 0, 0, 0, "0:0:0:0:0:0:0", TimeDuration.fromNumber(null), false, true);
    assertDuration("millisecond", 0, 0, 0, 0, 0, 0, 1);
    assertDuration("second", 0, 0, 0, 0, 0, 1, 0);
    assertDuration("minute", 0, 0, 0, 0, 1, 0, 0);
    assertDuration("hour", 0, 0, 0, 1, 0, 0, 0);
    assertDuration("day", 0, 0, 1, 0, 0, 0, 0);
    assertDuration("month", 0, 1, 0, 0, 0, 0, 0);
    assertDuration("year", 1, 0, 0, 0, 0, 0, 0);
    Calendar start = new com.ibm.icu.util.GregorianCalendar(1967, 1, 1, 0, 0, 0);
    start.set(Calendar.MILLISECOND, 0);
    Calendar end = (Calendar) start.clone();
    end.add(Calendar.MILLISECOND, 1);
    end.add(Calendar.SECOND, 1);
    end.add(Calendar.MINUTE, 1);
    end.add(Calendar.HOUR_OF_DAY, 1);
    end.add(Calendar.DAY_OF_MONTH, 1);
    end.add(Calendar.MONTH, 1);
    end.add(Calendar.YEAR, 1);
    assertDurationFields("pre-epoch elapsed time", 1, 1, 1, 1, 1, 1, 1, "1:1:1:1:1:1:1", new TimeDuration(start, end), false, false);
}
Also used : Calendar(com.ibm.icu.util.Calendar) TimeDuration(org.apache.ofbiz.base.util.TimeDuration)

Example 7 with TimeDuration

use of org.apache.ofbiz.base.util.TimeDuration in project ofbiz-framework by apache.

the class TimeDurationTests method assertDurationLoop.

private static TimeDuration assertDurationLoop(String label, Calendar right, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds, TimeDuration lastString, boolean isNegative) {
    StringBuilder sb = new StringBuilder();
    sb.append(years != 0 ? years : "");
    sb.append(':').append(months != 0 ? months : "");
    sb.append(':').append(days != 0 ? days : "");
    sb.append(':').append(hours != 0 ? hours : "");
    sb.append(':').append(minutes != 0 ? minutes : "");
    sb.append(':').append(seconds != 0 ? seconds : "");
    sb.append(':').append(milliseconds != 0 ? milliseconds : "");
    String durationString = years + ":" + months + ":" + days + ":" + hours + ":" + minutes + ":" + seconds + ":" + milliseconds;
    TimeDuration stringDuration = TimeDuration.parseDuration(sb.toString());
    right.setTimeInMillis(0);
    if (years != 0) {
        right.set(Calendar.YEAR, 1970 + Math.abs(years));
    }
    if (months != 0) {
        right.set(Calendar.MONTH, Math.abs(months));
    }
    right.set(Calendar.DAY_OF_MONTH, Math.abs(days) + 1);
    if (hours != 0) {
        right.set(Calendar.HOUR, Math.abs(hours));
    }
    if (minutes != 0) {
        right.set(Calendar.MINUTE, Math.abs(minutes));
    }
    if (seconds != 0) {
        right.set(Calendar.SECOND, Math.abs(seconds));
    }
    if (milliseconds != 0) {
        right.set(Calendar.MILLISECOND, Math.abs(milliseconds));
    }
    TimeDuration calDuration = isNegative ? new TimeDuration(right, zero) : new TimeDuration(zero, right);
    assertDurationFields(label + "(parseString[0])", years, months, days, hours, minutes, seconds, milliseconds, durationString, TimeDuration.parseDuration(durationString), isNegative, false);
    assertDurationFields(label + "(parseString)", years, months, days, hours, minutes, seconds, milliseconds, durationString, stringDuration, isNegative, false);
    assertDurationFields(label + "(cal)", years, months, days, hours, minutes, seconds, milliseconds, durationString, calDuration, isNegative, false);
    Calendar added = calDuration.addToCalendar((Calendar) zero.clone());
    TimeDuration addDuration = new TimeDuration(zero, added);
    assertDurationFields(label + "(cal[add])", years, months, days, hours, minutes, seconds, milliseconds, durationString, addDuration, isNegative, false);
    assertEquals(label + ".compareTo(string, cal)", 0, doCompare(stringDuration, calDuration));
    assertEquals(label + ".compareTo(string, string)", 0, doCompare(stringDuration, stringDuration));
    assertEquals(label + ".compareTo(cal, cal)", 0, doCompare(calDuration, calDuration));
    assertEquals(label + ".compareTo(cal, string)", 0, doCompare(calDuration, stringDuration));
    assertEquals(label + ".equals(cal, cal)", calDuration, calDuration);
    assertEquals(label + ".equals(cal, string)", calDuration, stringDuration);
    assertEquals(label + ".equals(string, cal)", stringDuration, calDuration);
    assertEquals(label + ".equals(string, string)", stringDuration, stringDuration);
    assertFalse(label + ".not-equals(string, this)", stringDuration.equals(TimeDurationTests.class));
    if (lastString != null) {
        assertFalse(label + ".not-equals(string, lastString)", stringDuration.equals(lastString));
    }
    return stringDuration;
}
Also used : Calendar(com.ibm.icu.util.Calendar) TimeDuration(org.apache.ofbiz.base.util.TimeDuration)

Example 8 with TimeDuration

use of org.apache.ofbiz.base.util.TimeDuration in project ofbiz-framework by apache.

the class TimeDurationTests method assertDuration.

public static void assertDuration(String label, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds) {
    TimeDuration lastString = null;
    Calendar right = (Calendar) zero.clone();
    for (int i = 1; i < 12; i++) {
        lastString = assertDurationLoop(i + " " + label, right, i * years, i * months, i * days, i * hours, i * minutes, i * seconds, i * milliseconds, lastString, false);
    }
    lastString = null;
    for (int i = -2; i > -12; i--) {
        lastString = assertDurationLoop(i + " " + label, right, i * years, i * months, i * days, i * hours, i * minutes, i * seconds, i * milliseconds, lastString, true);
    }
}
Also used : Calendar(com.ibm.icu.util.Calendar) TimeDuration(org.apache.ofbiz.base.util.TimeDuration)

Aggregations

TimeDuration (org.apache.ofbiz.base.util.TimeDuration)8 Calendar (com.ibm.icu.util.Calendar)5 Timestamp (java.sql.Timestamp)3 Date (java.util.Date)2 Locale (java.util.Locale)2 TimeZone (java.util.TimeZone)2 Dur (net.fortuna.ical4j.model.Dur)2 Delegator (org.apache.ofbiz.entity.Delegator)2 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)2 GenericValue (org.apache.ofbiz.entity.GenericValue)2 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)2 TemporalExpression (org.apache.ofbiz.service.calendar.TemporalExpression)2 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 MissingResourceException (java.util.MissingResourceException)1 TreeMap (java.util.TreeMap)1 DateTime (net.fortuna.ical4j.model.DateTime)1