Example usage for org.joda.time DateTime getMillisOfSecond

List of usage examples for org.joda.time DateTime getMillisOfSecond

Introduction

In this page you can find the example usage for org.joda.time DateTime getMillisOfSecond.

Prototype

public int getMillisOfSecond() 

Source Link

Document

Get the millis of second field value.

Usage

From source file:org.jruby.ext.date.TimeExt.java

License:LGPL

private static RubyDateTime calcAjdFromCivil(ThreadContext context, final DateTime dt, final int off,
        final long subMillisNum, final long subMillisDen) {
    final Ruby runtime = context.runtime;

    long jd = civil_to_jd(dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth(), ITALY);
    RubyNumeric fr = timeToDayFraction(context, dt.getHourOfDay(), dt.getMinuteOfHour(),
            dt.getSecondOfMinute());//from   w w w .  j  ava 2 s .  c  o m

    final RubyNumeric ajd = jd_to_ajd(context, jd, fr, off);
    RubyDateTime dateTime = new RubyDateTime(context, getDateTime(runtime), ajd, off, ITALY);
    dateTime.dt = dateTime.dt.withMillisOfSecond(dt.getMillisOfSecond());
    dateTime.subMillisNum = subMillisNum;
    dateTime.subMillisDen = subMillisDen;
    return dateTime;
}

From source file:org.jruby.CompatVersion.java

License:LGPL

public RubyObject mdump(final IRubyObject[] args) {
        RubyTime obj = (RubyTime) args[0];
        DateTime dateTime = obj.dt.withZone(DateTimeZone.UTC);
        byte dumpValue[] = new byte[8];
        int pe = 0x1 << 31 | (dateTime.getYear() - 1900) << 14 | (dateTime.getMonthOfYear() - 1) << 10
                | dateTime.getDayOfMonth() << 5 | dateTime.getHourOfDay();
        int se = dateTime.getMinuteOfHour() << 26 | dateTime.getSecondOfMinute() << 20
                | (dateTime.getMillisOfSecond() * 1000 + (int) usec); // dump usec, not msec

        for (int i = 0; i < 4; i++) {
            dumpValue[i] = (byte) (pe & 0xFF);
            pe >>>= 8;//from w ww .  j a v a2  s. c  o m
        }
        for (int i = 4; i < 8; i++) {
            dumpValue[i] = (byte) (se & 0xFF);
            se >>>= 8;
        }
        return RubyString.newString(obj.getRuntime(), new ByteList(dumpValue, false));
    }

From source file:org.jruby.RubyTime.java

License:LGPL

public RubyObject mdump() {
    Ruby runtime = getRuntime();//from w  ww. ja  v a2  s.c  o  m
    RubyTime obj = this;
    DateTime dateTime = obj.dt.toDateTime(DateTimeZone.UTC);
    byte dumpValue[] = new byte[8];
    long nanos = this.nsec;
    long usec = this.nsec / 1000;
    long nanosec = this.nsec % 1000;

    int pe = 0x1 << 31 | ((obj.gmt().isTrue()) ? 0x1 : 0x0) << 30 | (dateTime.getYear() - 1900) << 14
            | (dateTime.getMonthOfYear() - 1) << 10 | dateTime.getDayOfMonth() << 5 | dateTime.getHourOfDay();
    int se = dateTime.getMinuteOfHour() << 26 | dateTime.getSecondOfMinute() << 20
            | (dateTime.getMillisOfSecond() * 1000 + (int) usec); // dump usec, not msec

    for (int i = 0; i < 4; i++) {
        dumpValue[i] = (byte) (pe & 0xFF);
        pe >>>= 8;
    }
    for (int i = 4; i < 8; i++) {
        dumpValue[i] = (byte) (se & 0xFF);
        se >>>= 8;
    }

    RubyString string = RubyString.newString(obj.getRuntime(), new ByteList(dumpValue));

    // 1.9 includes more nsecs
    copyInstanceVariablesInto(string);

    // nanos in numerator/denominator form
    if (nanosec != 0) {
        string.setInternalVariable("nano_num", runtime.newFixnum(nanosec));
        string.setInternalVariable("nano_den", runtime.newFixnum(1));
    }

    // submicro for 1.9.1 compat
    byte[] submicro = new byte[2];
    int len = 2;
    submicro[1] = (byte) ((nanosec % 10) << 4);
    nanosec /= 10;
    submicro[0] = (byte) (nanosec % 10);
    nanosec /= 10;
    submicro[0] |= (byte) ((nanosec % 10) << 4);
    if (submicro[1] == 0)
        len = 1;
    string.setInternalVariable("submicro", RubyString.newString(runtime, submicro, 0, len));

    // time zone
    if (dt.getZone() != DateTimeZone.UTC) {
        long offset = dt.getZone().getOffset(dt.getMillis());
        string.setInternalVariable("offset", runtime.newFixnum(offset / 1000));

        String zone = dt.getZone().getShortName(dt.getMillis());
        if (!TIME_OFFSET_PATTERN.matcher(zone).matches()) {
            string.setInternalVariable("zone", runtime.newString(zone));
        }
    }

    return string;
}

From source file:org.jruby.truffle.core.time.RubyDateFormatter.java

License:LGPL

public ByteList formatToByteList(List<Token> compiledPattern, DateTime dt, long nsec) {
    RubyTimeOutputFormatter formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER;
    ByteList toAppendTo = new ByteList();

    for (Token token : compiledPattern) {
        String output = null;// w w  w  .  ja  v a2 s. c o  m
        long value = 0;
        FieldType type = TEXT;
        Format format = token.getFormat();

        switch (format) {
        case FORMAT_ENCODING:
            toAppendTo.setEncoding((Encoding) token.getData());
            continue; // go to next token
        case FORMAT_OUTPUT:
            formatter = (RubyTimeOutputFormatter) token.getData();
            continue; // go to next token
        case FORMAT_STRING:
            output = token.getData().toString();
            break;
        case FORMAT_WEEK_LONG:
            // This is GROSS, but Java API's aren't ISO 8601 compliant at all
            int v = (dt.getDayOfWeek() + 1) % 8;
            if (v == 0) {
                v++;
            }
            output = FORMAT_SYMBOLS.getWeekdays()[v];
            break;
        case FORMAT_WEEK_SHORT:
            // This is GROSS, but Java API's aren't ISO 8601 compliant at all
            v = (dt.getDayOfWeek() + 1) % 8;
            if (v == 0) {
                v++;
            }
            output = FORMAT_SYMBOLS.getShortWeekdays()[v];
            break;
        case FORMAT_MONTH_LONG:
            output = FORMAT_SYMBOLS.getMonths()[dt.getMonthOfYear() - 1];
            break;
        case FORMAT_MONTH_SHORT:
            output = FORMAT_SYMBOLS.getShortMonths()[dt.getMonthOfYear() - 1];
            break;
        case FORMAT_DAY:
            type = NUMERIC2;
            value = dt.getDayOfMonth();
            break;
        case FORMAT_DAY_S:
            type = NUMERIC2BLANK;
            value = dt.getDayOfMonth();
            break;
        case FORMAT_HOUR:
            type = NUMERIC2;
            value = dt.getHourOfDay();
            break;
        case FORMAT_HOUR_BLANK:
            type = NUMERIC2BLANK;
            value = dt.getHourOfDay();
            break;
        case FORMAT_HOUR_M:
        case FORMAT_HOUR_S:
            value = dt.getHourOfDay();
            if (value == 0) {
                value = 12;
            } else if (value > 12) {
                value -= 12;
            }

            type = (format == Format.FORMAT_HOUR_M) ? NUMERIC2 : NUMERIC2BLANK;
            break;
        case FORMAT_DAY_YEAR:
            type = NUMERIC3;
            value = dt.getDayOfYear();
            break;
        case FORMAT_MINUTES:
            type = NUMERIC2;
            value = dt.getMinuteOfHour();
            break;
        case FORMAT_MONTH:
            type = NUMERIC2;
            value = dt.getMonthOfYear();
            break;
        case FORMAT_MERIDIAN:
            output = dt.getHourOfDay() < 12 ? "AM" : "PM";
            break;
        case FORMAT_MERIDIAN_LOWER_CASE:
            output = dt.getHourOfDay() < 12 ? "am" : "pm";
            break;
        case FORMAT_SECONDS:
            type = NUMERIC2;
            value = dt.getSecondOfMinute();
            break;
        case FORMAT_WEEK_YEAR_M:
            type = NUMERIC2;
            value = formatWeekYear(dt, Calendar.MONDAY);
            break;
        case FORMAT_WEEK_YEAR_S:
            type = NUMERIC2;
            value = formatWeekYear(dt, Calendar.SUNDAY);
            break;
        case FORMAT_DAY_WEEK:
            type = NUMERIC;
            value = dt.getDayOfWeek() % 7;
            break;
        case FORMAT_DAY_WEEK2:
            type = NUMERIC;
            value = dt.getDayOfWeek();
            break;
        case FORMAT_YEAR_LONG:
            value = year(dt, dt.getYear());
            type = (value >= 0) ? NUMERIC4 : NUMERIC5;
            break;
        case FORMAT_YEAR_SHORT:
            type = NUMERIC2;
            value = year(dt, dt.getYear()) % 100;
            break;
        case FORMAT_COLON_ZONE_OFF:
            // custom logic because this is so weird
            value = dt.getZone().getOffset(dt.getMillis()) / 1000;
            int colons = (Integer) token.getData();
            output = formatZone(colons, (int) value, formatter);
            break;
        case FORMAT_ZONE_ID:
            output = getRubyTimeZoneName(dt);
            break;
        case FORMAT_CENTURY:
            type = NUMERIC;
            value = year(dt, dt.getYear()) / 100;
            break;
        case FORMAT_EPOCH:
            type = NUMERIC;
            value = dt.getMillis() / 1000;
            break;
        case FORMAT_WEEK_WEEKYEAR:
            type = NUMERIC2;
            value = dt.getWeekOfWeekyear();
            break;
        case FORMAT_MILLISEC:
        case FORMAT_NANOSEC:
            int defaultWidth = (format == Format.FORMAT_NANOSEC) ? 9 : 3;
            int width = formatter.getWidth(defaultWidth);

            output = RubyTimeOutputFormatter.formatNumber(dt.getMillisOfSecond(), 3, '0');
            if (width > 3) {
                output += RubyTimeOutputFormatter.formatNumber(nsec, 6, '0');
            }

            if (width < output.length()) {
                output = output.substring(0, width);
            } else {
                // Not enough precision, fill with 0
                while (output.length() < width)
                    output += "0";
            }
            formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER; // no more formatting
            break;
        case FORMAT_WEEKYEAR:
            value = year(dt, dt.getWeekyear());
            type = (value >= 0) ? NUMERIC4 : NUMERIC5;
            break;
        case FORMAT_WEEKYEAR_SHORT:
            type = NUMERIC2;
            value = year(dt, dt.getWeekyear()) % 100;
            break;
        case FORMAT_MICROSEC_EPOCH:
            // only available for Date
            type = NUMERIC;
            value = dt.getMillis();
            break;
        case FORMAT_SPECIAL:
            throw new Error("FORMAT_SPECIAL is a special token only for the lexer.");
        }

        try {
            output = formatter.format(output, value, type);
        } catch (IndexOutOfBoundsException ioobe) {
            throw new RaiseException(
                    context.getCoreExceptions().errnoError(Errno.ERANGE.intValue(), "strftime", currentNode));
        }

        // reset formatter
        formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER;

        toAppendTo.append(
                output.getBytes(context.getEncodingManager().charsetForEncoding(toAppendTo.getEncoding())));
    }

    return toAppendTo;
}

From source file:org.jruby.util.RubyDateFormatter.java

License:LGPL

public ByteList formatToByteList(List<Token> compiledPattern, DateTime dt, long nsec, IRubyObject sub_millis) {
    RubyTimeOutputFormatter formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER;
    ByteList toAppendTo = new ByteList();

    for (Token token : compiledPattern) {
        String output = null;/*from  w  w w.  ja  va2 s .  c  om*/
        long value = 0;
        FieldType type = TEXT;
        Format format = token.getFormat();

        switch (format) {
        case FORMAT_ENCODING:
            toAppendTo.setEncoding((Encoding) token.getData());
            continue; // go to next token
        case FORMAT_OUTPUT:
            formatter = (RubyTimeOutputFormatter) token.getData();
            continue; // go to next token
        case FORMAT_STRING:
            output = token.getData().toString();
            break;
        case FORMAT_WEEK_LONG:
            // This is GROSS, but Java API's aren't ISO 8601 compliant at all
            int v = (dt.getDayOfWeek() + 1) % 8;
            if (v == 0) {
                v++;
            }
            output = FORMAT_SYMBOLS.getWeekdays()[v];
            break;
        case FORMAT_WEEK_SHORT:
            // This is GROSS, but Java API's aren't ISO 8601 compliant at all
            v = (dt.getDayOfWeek() + 1) % 8;
            if (v == 0) {
                v++;
            }
            output = FORMAT_SYMBOLS.getShortWeekdays()[v];
            break;
        case FORMAT_MONTH_LONG:
            output = FORMAT_SYMBOLS.getMonths()[dt.getMonthOfYear() - 1];
            break;
        case FORMAT_MONTH_SHORT:
            output = FORMAT_SYMBOLS.getShortMonths()[dt.getMonthOfYear() - 1];
            break;
        case FORMAT_DAY:
            type = NUMERIC2;
            value = dt.getDayOfMonth();
            break;
        case FORMAT_DAY_S:
            type = NUMERIC2BLANK;
            value = dt.getDayOfMonth();
            break;
        case FORMAT_HOUR:
            type = NUMERIC2;
            value = dt.getHourOfDay();
            break;
        case FORMAT_HOUR_BLANK:
            type = NUMERIC2BLANK;
            value = dt.getHourOfDay();
            break;
        case FORMAT_HOUR_M:
        case FORMAT_HOUR_S:
            value = dt.getHourOfDay();
            if (value == 0) {
                value = 12;
            } else if (value > 12) {
                value -= 12;
            }

            type = (format == Format.FORMAT_HOUR_M) ? NUMERIC2 : NUMERIC2BLANK;
            break;
        case FORMAT_DAY_YEAR:
            type = NUMERIC3;
            value = dt.getDayOfYear();
            break;
        case FORMAT_MINUTES:
            type = NUMERIC2;
            value = dt.getMinuteOfHour();
            break;
        case FORMAT_MONTH:
            type = NUMERIC2;
            value = dt.getMonthOfYear();
            break;
        case FORMAT_MERIDIAN:
            output = dt.getHourOfDay() < 12 ? "AM" : "PM";
            break;
        case FORMAT_MERIDIAN_LOWER_CASE:
            output = dt.getHourOfDay() < 12 ? "am" : "pm";
            break;
        case FORMAT_SECONDS:
            type = NUMERIC2;
            value = dt.getSecondOfMinute();
            break;
        case FORMAT_WEEK_YEAR_M:
            type = NUMERIC2;
            value = formatWeekYear(dt, java.util.Calendar.MONDAY);
            break;
        case FORMAT_WEEK_YEAR_S:
            type = NUMERIC2;
            value = formatWeekYear(dt, java.util.Calendar.SUNDAY);
            break;
        case FORMAT_DAY_WEEK:
            type = NUMERIC;
            value = dt.getDayOfWeek() % 7;
            break;
        case FORMAT_DAY_WEEK2:
            type = NUMERIC;
            value = dt.getDayOfWeek();
            break;
        case FORMAT_YEAR_LONG:
            value = year(dt, dt.getYear());
            type = (value >= 0) ? NUMERIC4 : NUMERIC5;
            break;
        case FORMAT_YEAR_SHORT:
            type = NUMERIC2;
            value = year(dt, dt.getYear()) % 100;
            break;
        case FORMAT_COLON_ZONE_OFF:
            // custom logic because this is so weird
            value = dt.getZone().getOffset(dt.getMillis()) / 1000;
            int colons = (Integer) token.getData();
            output = formatZone(colons, (int) value, formatter);
            break;
        case FORMAT_ZONE_ID:
            output = dt.getZone().getShortName(dt.getMillis());
            break;
        case FORMAT_CENTURY:
            type = NUMERIC;
            value = year(dt, dt.getYear()) / 100;
            break;
        case FORMAT_EPOCH:
            type = NUMERIC;
            value = dt.getMillis() / 1000;
            break;
        case FORMAT_WEEK_WEEKYEAR:
            type = NUMERIC2;
            value = dt.getWeekOfWeekyear();
            break;
        case FORMAT_MILLISEC:
        case FORMAT_NANOSEC:
            int defaultWidth = (format == Format.FORMAT_NANOSEC) ? 9 : 3;
            int width = formatter.getWidth(defaultWidth);

            output = RubyTimeOutputFormatter.formatNumber(dt.getMillisOfSecond(), 3, '0');
            if (width > 3) {
                if (sub_millis == null || sub_millis.isNil()) { // Time
                    output += RubyTimeOutputFormatter.formatNumber(nsec, 6, '0');
                } else { // Date, DateTime
                    int prec = width - 3;
                    IRubyObject power = context.runtime.newFixnum(10).callMethod("**",
                            context.runtime.newFixnum(prec));
                    IRubyObject truncated = sub_millis.callMethod(context, "numerator").callMethod(context, "*",
                            power);
                    truncated = truncated.callMethod(context, "/",
                            sub_millis.callMethod(context, "denominator"));
                    long decimals = truncated.convertToInteger().getLongValue();
                    output += RubyTimeOutputFormatter.formatNumber(decimals, prec, '0');
                }
            }

            if (width < output.length()) {
                output = output.substring(0, width);
            } else {
                // Not enough precision, fill with 0
                while (output.length() < width)
                    output += "0";
            }
            formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER; // no more formatting
            break;
        case FORMAT_WEEKYEAR:
            value = year(dt, dt.getWeekyear());
            type = (value >= 0) ? NUMERIC4 : NUMERIC5;
            break;
        case FORMAT_WEEKYEAR_SHORT:
            type = NUMERIC2;
            value = year(dt, dt.getWeekyear()) % 100;
            break;
        case FORMAT_MICROSEC_EPOCH:
            // only available for Date
            type = NUMERIC;
            value = dt.getMillis();
            break;
        case FORMAT_SPECIAL:
            throw new Error("FORMAT_SPECIAL is a special token only for the lexer.");
        }

        output = formatter.format(output, value, type);
        // reset formatter
        formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER;

        toAppendTo.append(output
                .getBytes(context.runtime.getEncodingService().charsetForEncoding(toAppendTo.getEncoding())));
    }

    return toAppendTo;
}

From source file:org.kalypso.ui.rrm.internal.calccase.CatchmentModelHelper.java

License:Open Source License

private static DateRange modifyWithTimestamp(final LocalTime timestamp, final DateTime simulationStart,
        final DateTime simulationEnd) {
    /* Nothing to do. */
    if (timestamp == null)
        return new DateRange(simulationStart.toDate(), simulationEnd.toDate());

    /* Convert to a date with the kalypso timezone. */
    /* The date fields are ignored. */
    final DateTime timestampUTC = timestamp
            .toDateTimeToday(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"))); //$NON-NLS-1$
    final DateTime timestampDate = new DateTime(timestampUTC.toDate(),
            DateTimeZone.forTimeZone(KalypsoCorePlugin.getDefault().getTimeZone()));

    /* Further adjust range by predefined time. */
    final DateTime startWithTime = simulationStart.withTime(timestampDate.getHourOfDay(),
            timestampDate.getMinuteOfHour(), timestampDate.getSecondOfMinute(),
            timestampDate.getMillisOfSecond());
    final DateTime endWithTime = simulationEnd.withTime(timestampDate.getHourOfDay(),
            timestampDate.getMinuteOfHour(), timestampDate.getSecondOfMinute(),
            timestampDate.getMillisOfSecond());

    return new DateRange(startWithTime.toDate(), endWithTime.toDate());
}

From source file:org.kalypso.ui.rrm.internal.calccase.CatchmentModelHelper.java

License:Open Source License

/**
 * This function calculates the range for the timeseries to be generated. The range equals the range defined in the
 * simulation adjusted as follows://www  .  j  a v  a  2s. co m
 * <ul>
 * <li>1 timestep earlier</li>
 * <li>3 timesteps later</li>
 * </ul>
 *
 * @param control
 *          The na control.
 * @param timestep
 *          The timestep.
 * @param timestamp
 *          The timestamp in UTC.
 * @return The date range.
 */
public static DateRange getRange(final NAControl control, final Period timestep, final LocalTime timestamp) {
    final Date simulationStart = control.getSimulationStart();
    final Date simulationEnd = control.getSimulationEnd();

    final DateTime start = new DateTime(simulationStart);
    final DateTime end = new DateTime(simulationEnd);

    final DateTime adjustedStart = start.minus(timestep);
    final DateTime adjustedEnd = end.plus(timestep).plus(timestep).plus(timestep);

    if (timestep.getDays() == 0 || timestamp == null)
        return new DateRange(adjustedStart.toDate(), adjustedEnd.toDate());

    /* Convert to a date with the kalypso timezone. */
    /* The date fields are ignored. */
    final DateTime timestampUTC = timestamp
            .toDateTimeToday(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"))); //$NON-NLS-1$
    final DateTime timestampDate = new DateTime(timestampUTC.toDate(),
            DateTimeZone.forTimeZone(KalypsoCorePlugin.getDefault().getTimeZone()));

    /* Further adjust range by predefined time. */
    final DateTime startWithTime = adjustedStart.withTime(timestampDate.getHourOfDay(),
            timestampDate.getMinuteOfHour(), timestampDate.getSecondOfMinute(),
            timestampDate.getMillisOfSecond());
    final DateTime endWithTime = adjustedEnd.withTime(timestampDate.getHourOfDay(),
            timestampDate.getMinuteOfHour(), timestampDate.getSecondOfMinute(),
            timestampDate.getMillisOfSecond());

    /* New start must always be before unadjusted start, fix, if this is not the case. */
    DateTime startWithTimeFixed;
    if (startWithTime.isAfter(adjustedStart))
        startWithTimeFixed = startWithTime.minus(timestep);
    else
        startWithTimeFixed = startWithTime;

    /* New end must always be after unadjusted end, fix, if this is not the case. */
    DateTime endWithTimeFixed;
    if (endWithTime.isBefore(adjustedEnd))
        endWithTimeFixed = endWithTime.plus(timestep);
    else
        endWithTimeFixed = endWithTime;

    return new DateRange(startWithTimeFixed.toDate(), endWithTimeFixed.toDate());

}

From source file:org.mongoste.util.DateUtil.java

License:Open Source License

public static DateTime toUTC(DateTime fromDate) {
    if (DateTimeZone.UTC.equals(fromDate.getZone())) {
        return fromDate;
    }//from  w ww.  jav a  2 s .c  o  m
    MutableDateTime dt = getDateTimeUTC().toMutableDateTime();
    dt.setDateTime(fromDate.getYear(), fromDate.getMonthOfYear(), fromDate.getDayOfMonth(),
            fromDate.getHourOfDay(), fromDate.getMinuteOfHour(), fromDate.getSecondOfMinute(),
            fromDate.getMillisOfSecond());
    return dt.toDateTime();
}

From source file:org.openehr.rm.datatypes.quantity.datetime.DvDateTimeParser.java

License:LGPL

public static DateTime defaultTime() {
    DateTime time = new DateTime();
    return new DateTime(1970, 1, 1, time.getHourOfDay(), time.getMinuteOfHour(), time.getSecondOfMinute(),
            time.getMillisOfSecond());
}

From source file:org.openehr.rm.datatypes.quantity.datetime.DvTime.java

License:LGPL

/**
 * Parses a string value and return a DvTime
 *//*from   www  . j  a  va  2 s  . co m*/
public DvTime parse(String value) {
    DateTime time = DvDateTimeParser.parseTime(value);
    return new DvTime(time.getHourOfDay(), time.getMinuteOfHour(), time.getSecondOfMinute(),
            time.getMillisOfSecond(), time.getZone().toTimeZone());
}