Search in sources :

Example 31 with MutableDateTime

use of org.joda.time.MutableDateTime in project components by Talend.

the class XMLGregorianCalendarToDateTimeConverter method convertToAvro.

@Override
public Object convertToAvro(XMLGregorianCalendar xts) {
    if (xts == null) {
        return null;
    }
    MutableDateTime dateTime = new MutableDateTime();
    try {
        dateTime.setYear(xts.getYear());
        dateTime.setMonthOfYear(xts.getMonth());
        dateTime.setDayOfMonth(xts.getDay());
        dateTime.setHourOfDay(xts.getHour());
        dateTime.setMinuteOfHour(xts.getMinute());
        dateTime.setSecondOfMinute(xts.getSecond());
        dateTime.setMillisOfSecond(xts.getMillisecond());
        DateTimeZone tz = DateTimeZone.forOffsetMillis(xts.getTimezone() * 60000);
        if (tz != null) {
            dateTime.setZoneRetainFields(tz);
        }
        return Long.valueOf(dateTime.getMillis());
    } catch (IllegalArgumentException e) {
        throw new ComponentException(e);
    }
}
Also used : ComponentException(org.talend.components.api.exception.ComponentException) MutableDateTime(org.joda.time.MutableDateTime) DateTimeZone(org.joda.time.DateTimeZone)

Example 32 with MutableDateTime

use of org.joda.time.MutableDateTime in project ma-core-public by infiniteautomation.

the class Interpolation method updateYear.

public static List<PointValueTime> updateYear(List<PointValueTime> pvts, int year) {
    List<PointValueTime> result = new ArrayList<PointValueTime>(pvts.size());
    MutableDateTime mdt = new MutableDateTime();
    for (PointValueTime pvt : pvts) {
        mdt.setMillis(pvt.getTime());
        mdt.setYear(year);
        result.add(new PointValueTime(pvt.getValue(), mdt.getMillis()));
    }
    return result;
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) MutableDateTime(org.joda.time.MutableDateTime)

Example 33 with MutableDateTime

use of org.joda.time.MutableDateTime in project actframework by actframework.

the class CronExpression method nextTimeAfter.

public DateTime nextTimeAfter(DateTime afterTime, DateTime dateTimeBarrier) {
    MutableDateTime nextTime = new MutableDateTime(afterTime);
    nextTime.setMillisOfSecond(0);
    nextTime.secondOfDay().add(1);
    while (true) {
        // day of week
        while (true) {
            // month
            while (true) {
                // day of month
                while (true) {
                    // hour
                    while (true) {
                        // minute
                        while (true) {
                            // second
                            if (secondField.matches(nextTime.getSecondOfMinute())) {
                                break;
                            }
                            nextTime.secondOfDay().add(1);
                        }
                        if (minuteField.matches(nextTime.getMinuteOfHour())) {
                            break;
                        }
                        nextTime.minuteOfDay().add(1);
                        nextTime.secondOfMinute().set(0);
                    }
                    if (hourField.matches(nextTime.getHourOfDay())) {
                        break;
                    }
                    nextTime.hourOfDay().add(1);
                    nextTime.minuteOfHour().set(0);
                    nextTime.secondOfMinute().set(0);
                }
                if (dayOfMonthField.matches(new LocalDate(nextTime))) {
                    break;
                }
                nextTime.addDays(1);
                nextTime.setTime(0, 0, 0, 0);
                checkIfDateTimeBarrierIsReached(nextTime, dateTimeBarrier);
            }
            if (monthField.matches(nextTime.getMonthOfYear())) {
                break;
            }
            nextTime.addMonths(1);
            nextTime.setDayOfMonth(1);
            nextTime.setTime(0, 0, 0, 0);
            checkIfDateTimeBarrierIsReached(nextTime, dateTimeBarrier);
        }
        if (dayOfWeekField.matches(new LocalDate(nextTime))) {
            break;
        }
        nextTime.addDays(1);
        nextTime.setTime(0, 0, 0, 0);
        checkIfDateTimeBarrierIsReached(nextTime, dateTimeBarrier);
    }
    return nextTime.toDateTime();
}
Also used : MutableDateTime(org.joda.time.MutableDateTime) LocalDate(org.joda.time.LocalDate)

Example 34 with MutableDateTime

use of org.joda.time.MutableDateTime in project beam by apache.

the class SplittableDoFnTest method testPairWithIndexWindowedTimestamped.

private void testPairWithIndexWindowedTimestamped(IsBounded bounded) {
    // Tests that Splittable DoFn correctly propagates windowing strategy, windows and timestamps
    // of elements in the input collection.
    MutableDateTime mutableNow = Instant.now().toMutableDateTime();
    mutableNow.setMillisOfSecond(0);
    Instant now = mutableNow.toInstant();
    Instant nowP1 = now.plus(Duration.standardSeconds(1));
    Instant nowP2 = now.plus(Duration.standardSeconds(2));
    SlidingWindows windowFn = SlidingWindows.of(Duration.standardSeconds(5)).every(Duration.standardSeconds(1));
    PCollection<KV<String, Integer>> res = p.apply(Create.timestamped(TimestampedValue.of("a", now), TimestampedValue.of("bb", nowP1), TimestampedValue.of("ccccc", nowP2))).apply(Window.into(windowFn)).apply(ParDo.of(pairStringWithIndexToLengthFn(bounded))).setCoder(KvCoder.of(StringUtf8Coder.of(), BigEndianIntegerCoder.of()));
    assertEquals(windowFn, res.getWindowingStrategy().getWindowFn());
    PCollection<TimestampedValue<KV<String, Integer>>> timestamped = res.apply(Reify.timestamps());
    for (int i = 0; i < 4; ++i) {
        Instant base = now.minus(Duration.standardSeconds(i));
        IntervalWindow window = new IntervalWindow(base, base.plus(Duration.standardSeconds(5)));
        List<TimestampedValue<KV<String, Integer>>> expectedUnfiltered = Arrays.asList(TimestampedValue.of(KV.of("a", 0), now), TimestampedValue.of(KV.of("bb", 0), nowP1), TimestampedValue.of(KV.of("bb", 1), nowP1), TimestampedValue.of(KV.of("ccccc", 0), nowP2), TimestampedValue.of(KV.of("ccccc", 1), nowP2), TimestampedValue.of(KV.of("ccccc", 2), nowP2), TimestampedValue.of(KV.of("ccccc", 3), nowP2), TimestampedValue.of(KV.of("ccccc", 4), nowP2));
        List<TimestampedValue<KV<String, Integer>>> expected = new ArrayList<>();
        for (TimestampedValue<KV<String, Integer>> tv : expectedUnfiltered) {
            if (!window.start().isAfter(tv.getTimestamp()) && !tv.getTimestamp().isAfter(window.maxTimestamp())) {
                expected.add(tv);
            }
        }
        assertFalse(expected.isEmpty());
        PAssert.that(timestamped).inWindow(window).containsInAnyOrder(expected);
    }
    p.run();
}
Also used : Instant(org.joda.time.Instant) ArrayList(java.util.ArrayList) MutableDateTime(org.joda.time.MutableDateTime) KV(org.apache.beam.sdk.values.KV) TimestampedValue(org.apache.beam.sdk.values.TimestampedValue) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) SlidingWindows(org.apache.beam.sdk.transforms.windowing.SlidingWindows)

Example 35 with MutableDateTime

use of org.joda.time.MutableDateTime in project druid by druid-io.

the class FileRequestLogger method start.

@LifecycleStart
@Override
public void start() {
    try {
        FileUtils.mkdirp(baseDir);
        MutableDateTime mutableDateTime = DateTimes.nowUtc().toMutableDateTime(ISOChronology.getInstanceUTC());
        mutableDateTime.setMillisOfDay(0);
        synchronized (lock) {
            currentDay = mutableDateTime.toDateTime(ISOChronology.getInstanceUTC());
            fileWriter = getFileWriter();
        }
        long nextDay = currentDay.plusDays(1).getMillis();
        Duration initialDelay = new Duration(nextDay - System.currentTimeMillis());
        ScheduledExecutors.scheduleWithFixedDelay(exec, initialDelay, Duration.standardDays(1), new Callable<ScheduledExecutors.Signal>() {

            @Override
            public ScheduledExecutors.Signal call() {
                try {
                    synchronized (lock) {
                        currentDay = currentDay.plusDays(1);
                        CloseableUtils.closeAndSuppressExceptions(fileWriter, e -> log.warn("Could not close log file for %s. Creating new log file anyway.", currentDay));
                        fileWriter = getFileWriter();
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return ScheduledExecutors.Signal.REPEAT;
            }
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Logger(org.apache.druid.java.util.common.logger.Logger) DateTimeFormat(org.joda.time.format.DateTimeFormat) DateTimes(org.apache.druid.java.util.common.DateTimes) ScheduledExecutors(org.apache.druid.java.util.common.concurrent.ScheduledExecutors) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) Duration(org.joda.time.Duration) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart) MutableDateTime(org.joda.time.MutableDateTime) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) RequestLogLine(org.apache.druid.server.RequestLogLine) LifecycleStop(org.apache.druid.java.util.common.lifecycle.LifecycleStop) ISOChronology(org.joda.time.chrono.ISOChronology) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) OutputStreamWriter(java.io.OutputStreamWriter) CloseableUtils(org.apache.druid.utils.CloseableUtils) FileUtils(org.apache.druid.java.util.common.FileUtils) MutableDateTime(org.joda.time.MutableDateTime) Duration(org.joda.time.Duration) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart)

Aggregations

MutableDateTime (org.joda.time.MutableDateTime)76 DateTime (org.joda.time.DateTime)12 Instant (org.joda.time.Instant)6 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)6 Date (java.util.Date)5 Chronology (org.joda.time.Chronology)4 DateTimeZone (org.joda.time.DateTimeZone)4 ISOChronology (org.joda.time.chrono.ISOChronology)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)3 SlidingWindows (org.apache.beam.sdk.transforms.windowing.SlidingWindows)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)2 KV (org.apache.beam.sdk.values.KV)2 TimestampedValue (org.apache.beam.sdk.values.TimestampedValue)2 Duration (org.joda.time.Duration)2 Category (org.junit.experimental.categories.Category)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1