Search in sources :

Example 41 with Duration

use of org.joda.time.Duration in project incubator-gobblin by apache.

the class PartitionAwareFileRetrieverUtils method getLeadTimeDurationFromConfig.

/**
 * Retrieve the lead time duration from the LEAD_TIME and LEAD_TIME granularity config settings.
 */
public static Duration getLeadTimeDurationFromConfig(State state) {
    String leadTimeProp = state.getProp(DATE_PARTITIONED_SOURCE_PARTITION_LEAD_TIME);
    if (leadTimeProp == null || leadTimeProp.length() == 0) {
        return DEFAULT_PARTITIONED_SOURCE_PARTITION_LEAD_TIME;
    }
    int leadTime = Integer.parseInt(leadTimeProp);
    DatePartitionType leadTimeGranularity = DEFAULT_DATE_PARTITIONED_SOURCE_PARTITION_LEAD_TIME_GRANULARITY;
    String leadTimeGranularityProp = state.getProp(DATE_PARTITIONED_SOURCE_PARTITION_LEAD_TIME_GRANULARITY);
    if (leadTimeGranularityProp != null) {
        leadTimeGranularity = DatePartitionType.valueOf(leadTimeGranularityProp);
    }
    return new Duration(leadTime * leadTimeGranularity.getUnitMilliseconds());
}
Also used : Duration(org.joda.time.Duration) DatePartitionType(org.apache.gobblin.util.DatePartitionType)

Example 42 with Duration

use of org.joda.time.Duration in project iep by Netflix.

the class ConfigurationTests method defaultDuration.

@Test
public void defaultDuration() {
    TestConfig config = mkConfig();
    Duration v = config.getDuration();
    assertEquals("getDuration", v.getMillis(), 300000L);
}
Also used : Duration(org.joda.time.Duration) Test(org.junit.Test)

Example 43 with Duration

use of org.joda.time.Duration in project tests by datanucleus.

the class JodaTimeTest method testDuration.

/**
 * Test for Duration persistence and retrieval.
 */
public void testDuration() {
    try {
        // Create some data we can use for access
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        Duration duration1 = new Duration(20011986);
        Duration duration2 = new Duration(1234567890);
        try {
            tx.begin();
            JodaSample4 s = new JodaSample4(1, duration1, duration2);
            JodaSample4 s2 = new JodaSample4(2, null, null);
            em.persist(s);
            em.persist(s2);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Error persisting Duration sample", e);
            fail("Error persisting Duration sample");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Retrieve the data
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            JodaSample4 s = em.find(JodaSample4.class, 1);
            Duration ld1 = s.getDuration1();
            assertNotNull("Retrieved Duration was null!", ld1);
            assertEquals("Duration was wrong", 20011986, ld1.getMillis());
            Duration ld2 = s.getDuration2();
            assertNotNull("Retrieved Duration was null!", ld2);
            assertEquals("Duration was wrong", 1234567890, ld2.getMillis());
            JodaSample4 s2 = em.find(JodaSample4.class, 2);
            assertNull(s2.getDuration1());
            assertNull(s2.getDuration2());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Error retrieving Duration data", e);
            fail("Error retrieving Duration data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(JodaSample4.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JodaSample4(org.datanucleus.samples.types.jodatime.JodaSample4) Duration(org.joda.time.Duration)

Example 44 with Duration

use of org.joda.time.Duration in project joda-time-android by dlew.

the class DateUtils method formatDuration.

/**
 * Return given duration in a human-friendly format. For example, "4
 * minutes" or "1 second". Returns only largest meaningful unit of time,
 * from seconds up to hours.
 *
 * The longest duration it supports is hours.
 *
 * This method assumes that there are 60 minutes in an hour,
 * 60 seconds in a minute and 1000 milliseconds in a second.
 * All currently supplied chronologies use this definition.
 */
public static CharSequence formatDuration(Context context, ReadableDuration readableDuration) {
    Resources res = context.getResources();
    Duration duration = readableDuration.toDuration();
    final int hours = (int) duration.getStandardHours();
    if (hours != 0) {
        return res.getQuantityString(R.plurals.joda_time_android_duration_hours, hours, hours);
    }
    final int minutes = (int) duration.getStandardMinutes();
    if (minutes != 0) {
        return res.getQuantityString(R.plurals.joda_time_android_duration_minutes, minutes, minutes);
    }
    final int seconds = (int) duration.getStandardSeconds();
    return res.getQuantityString(R.plurals.joda_time_android_duration_seconds, seconds, seconds);
}
Also used : ReadableDuration(org.joda.time.ReadableDuration) Duration(org.joda.time.Duration) Resources(android.content.res.Resources)

Example 45 with Duration

use of org.joda.time.Duration in project joda-time-android by dlew.

the class DateUtils method getRelativeDateTimeString.

/**
 * Return string describing the time until/elapsed time since 'time' formatted like
 * "[relative time/date], [time]".
 *
 * See {@link android.text.format.DateUtils#getRelativeDateTimeString} for full docs.
 *
 * @param context the context
 * @param time some time
 * @param transitionResolution the elapsed time (period) at which
 * to stop reporting relative measurements. Periods greater
 * than this resolution will default to normal date formatting.
 * For example, will transition from "6 days ago" to "Dec 12"
 * when using Weeks.ONE.  If null, defaults to Days.ONE.
 * Clamps to min value of Days.ONE, max of Weeks.ONE.
 * @param flags flags for getRelativeTimeSpanString() (if duration is less than transitionResolution)
 */
public static CharSequence getRelativeDateTimeString(Context context, ReadableInstant time, ReadablePeriod transitionResolution, int flags) {
    Resources r = context.getResources();
    // We set the millis to 0 so we aren't off by a fraction of a second when counting duration
    DateTime now = DateTime.now(time.getZone()).withMillisOfSecond(0);
    DateTime timeDt = new DateTime(time).withMillisOfSecond(0);
    boolean past = !now.isBefore(timeDt);
    Duration duration = past ? new Duration(timeDt, now) : new Duration(now, timeDt);
    // getRelativeTimeSpanString() doesn't correctly format relative dates
    // above a week or exact dates below a day, so clamp
    // transitionResolution as needed.
    Duration transitionDuration;
    Duration minDuration = Days.ONE.toPeriod().toDurationTo(timeDt);
    if (transitionResolution == null) {
        transitionDuration = minDuration;
    } else {
        transitionDuration = past ? transitionResolution.toPeriod().toDurationTo(now) : transitionResolution.toPeriod().toDurationFrom(now);
        Duration maxDuration = Weeks.ONE.toPeriod().toDurationTo(timeDt);
        if (transitionDuration.isLongerThan(maxDuration)) {
            transitionDuration = maxDuration;
        } else if (transitionDuration.isShorterThan(minDuration)) {
            transitionDuration = minDuration;
        }
    }
    CharSequence timeClause = formatDateRange(context, time, time, FORMAT_SHOW_TIME);
    String result;
    if (!duration.isLongerThan(transitionDuration)) {
        CharSequence relativeClause = getRelativeTimeSpanString(context, time, flags);
        result = r.getString(R.string.joda_time_android_relative_time, relativeClause, timeClause);
    } else {
        CharSequence dateClause = getRelativeTimeSpanString(context, time, false);
        result = r.getString(R.string.joda_time_android_date_time, dateClause, timeClause);
    }
    return result;
}
Also used : ReadableDuration(org.joda.time.ReadableDuration) Duration(org.joda.time.Duration) Resources(android.content.res.Resources) DateTime(org.joda.time.DateTime)

Aggregations

Duration (org.joda.time.Duration)272 Test (org.junit.Test)148 Instant (org.joda.time.Instant)66 DateTime (org.joda.time.DateTime)32 Period (org.joda.time.Period)27 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)24 TestDruidCoordinatorConfig (org.apache.druid.server.coordinator.TestDruidCoordinatorConfig)22 HashMap (java.util.HashMap)18 IOException (java.io.IOException)17 Category (org.junit.experimental.categories.Category)16 ArrayList (java.util.ArrayList)15 Map (java.util.Map)15 KV (org.apache.beam.sdk.values.KV)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 IndexSpec (org.apache.druid.segment.IndexSpec)12 Set (java.util.Set)10 GlobalWindows (org.apache.beam.sdk.transforms.windowing.GlobalWindows)10 DynamicPartitionsSpec (org.apache.druid.indexer.partitions.DynamicPartitionsSpec)10 Interval (org.joda.time.Interval)10 Request (com.metamx.http.client.Request)9