Example usage for org.joda.time DateTime getSecondOfMinute

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

Introduction

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

Prototype

public int getSecondOfMinute() 

Source Link

Document

Get the second of minute field value.

Usage

From source file:org.jasig.portlet.calendar.adapter.ExchangeCalendarAdapter.java

License:Apache License

/**
 * Get an XMLGregorianCalendar for the specified date.
 *
 * @param date/*from   w  w  w  .  ja v a2  s . co m*/
 * @return
 * @throws DatatypeConfigurationException
 */
protected XMLGregorianCalendar getXmlDate(DateTime date) throws DatatypeConfigurationException {
    // construct an XMLGregorianCalendar
    DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
    XMLGregorianCalendar start = datatypeFactory.newXMLGregorianCalendar();
    start.setYear(date.getYear());
    start.setMonth(date.getMonthOfYear());
    start.setTime(date.getHourOfDay(), date.getMinuteOfHour(), date.getSecondOfMinute(),
            date.getMillisOfSecond());
    start.setDay(date.getDayOfMonth());
    return start;
}

From source file:org.jbpm.designer.web.server.SimulationServlet.java

License:Apache License

private String getDateString(long seDate) {
    Date d = new Date(seDate);
    DateTime dt = new DateTime(seDate);
    StringBuffer retBuf = new StringBuffer();
    retBuf.append(dt.getYear()).append(",");
    retBuf.append(dt.getMonthOfYear()).append(",");
    retBuf.append(dt.getDayOfMonth()).append(",");
    retBuf.append(dt.getHourOfDay()).append(",");
    retBuf.append(dt.getMinuteOfHour()).append(",");
    retBuf.append(dt.getSecondOfMinute()).append(",");
    retBuf.append(dt.getMillisOfSecond());
    return retBuf.toString();
}

From source file:org.jimcat.services.rename.Renamer.java

License:Open Source License

@SuppressWarnings("null")
private String getNewName(Image image, int n) {
    String newName = configString;
    DateTime date = null;

    switch (useDate) {
    case MODIFICATION:
        date = image.getMetadata().getModificationDate();
        break;/*from  w ww .j  ava2s.co  m*/
    case ADDED:
        date = image.getMetadata().getDateAdded();
        break;
    case TAKEN:

        ExifMetadata exifMetadata = image.getExifMetadata();

        if (exifMetadata != null) {
            date = exifMetadata.getDateTaken();
        }
        break;
    }

    String random;

    do {
        random = String.valueOf(Math.random());
    } while (configString.contains(random));

    if (hasParameter(escapeCharacter.charAt(0))) {
        newName = newName.replace(escapeCharacter + escapeCharacter, random);
    }

    if (hasParameter('n')) {
        String number = String.format("%0" + digits + "d", new Integer(n));
        newName = newName.replace(escapeCharacter + "n", number);
    }

    if (hasParameter('w')) {
        newName = newName.replace(escapeCharacter + "w", "" + image.getMetadata().getWidth());
    }

    if (hasParameter('h')) {
        newName = newName.replace(escapeCharacter + "h", "" + image.getMetadata().getHeight());
    }

    if (hasParameter('d')) {
        if (date == null) {
            newName = newName.replace(escapeCharacter + "d", unknownResultCharacter);
        } else {
            String day = formatNumber(date.getDayOfMonth(), 2);
            newName = newName.replace(escapeCharacter + "d", day);
        }
    }

    if (hasParameter('m')) {
        if (date == null) {
            newName = newName.replace(escapeCharacter + "m", unknownResultCharacter);
        } else {
            String month = formatNumber(date.getMonthOfYear(), 2);
            newName = newName.replace(escapeCharacter + "m", month);
        }
    }

    if (hasParameter('y')) {
        if (date == null) {
            newName = newName.replace(escapeCharacter + "y", unknownResultCharacter);
        } else {
            String year = formatNumber(date.getYear(), 4);
            newName = newName.replace(escapeCharacter + "y", year);
        }
    }

    if (hasParameter('H')) {
        if (date == null) {
            newName = newName.replace(escapeCharacter + "H", unknownResultCharacter);
        } else {
            String hour = formatNumber(date.getHourOfDay(), 2);
            newName = newName.replace(escapeCharacter + "H", hour);
        }
    }

    if (hasParameter('M')) {
        if (date == null) {
            newName = newName.replace(escapeCharacter + "M", unknownResultCharacter);
        } else {
            String minute = formatNumber(date.getMinuteOfHour(), 2);
            newName = newName.replace(escapeCharacter + "M", minute);
        }
    }

    if (hasParameter('S')) {
        if (date == null) {
            newName = newName.replace(escapeCharacter + "S", unknownResultCharacter);
        } else {
            String seconds = formatNumber(date.getSecondOfMinute(), 2);
            newName = newName.replace(escapeCharacter + "S", seconds);
        }
    }

    if (hasParameter('r')) {
        newName = newName.replace(escapeCharacter + "r", ratingToString(image.getRating()));
    }

    if (hasParameter('f')) {
        if (image.getMetadata() != null && image.getMetadata().getPath() != null) {
            String fileName = removeFileType(image.getMetadata().getPath());
            newName = newName.replace(escapeCharacter + "f", fileName);
        } else {
            newName = newName.replace(escapeCharacter + "f", unknownResultCharacter);
        }
    }

    if (newName.length() == 0) {
        return newName;
    }

    // if the user is just typing dont show the last $
    // but if he wants a $ at the and (by using $$) allow it and even allow
    // $$ at the end

    int escapeCharactersAtEnd = 0;
    int index = newName.length() - 1;

    while (index >= 0 && newName.charAt(index--) == escapeCharacter.charAt(0)) {
        escapeCharactersAtEnd++;
    }

    if (escapeCharactersAtEnd % 2 == 1) {
        newName = newName.substring(0, newName.length() - 1);
    }

    if (hasParameter(escapeCharacter.charAt(0))) {
        newName = newName.replace(random, escapeCharacter);
    }

    // set at the end because the original title could contain evil control
    // sequences
    if (hasParameter('t')) {
        newName = newName.replace(escapeCharacter + "t", image.getTitle());
    }

    return newName;
}

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

License:LGPL

@JRubyMethod // Time.new(year, mon, mday, hour, min, sec + sec_fraction, (@of * 86400.0))
public RubyTime to_time(ThreadContext context) {
    final Ruby runtime = context.runtime;
    DateTime dt = this.dt;

    dt = new DateTime(adjustJodaYear(dt.getYear()), dt.getMonthOfYear(), dt.getDayOfMonth(), dt.getHourOfDay(),
            dt.getMinuteOfHour(), dt.getSecondOfMinute(), dt.getMillisOfSecond(),
            RubyTime.getTimeZone(runtime, this.off));

    RubyTime time = new RubyTime(runtime, runtime.getTime(), dt, true);
    if (subMillisNum != 0) {
        RubyNumeric usec = (RubyNumeric) subMillis(runtime).op_mul(context,
                RubyFixnum.newFixnum(runtime, 1_000_000));
        time.setNSec(usec.getLongValue());
    }/*  w  ww . j  a  va 2 s  .  co  m*/
    return time;
}

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

License:LGPL

@JRubyMethod(name = "to_datetime")
public static RubyDateTime to_datetime(ThreadContext context, IRubyObject self) {
    final RubyTime time = (RubyTime) self;
    DateTime dt = ((RubyTime) self).getDateTime();

    long subMillisNum = 0, subMillisDen = 1;
    if (time.getNSec() != 0) {
        IRubyObject subMillis = RubyRational.newRationalCanonicalize(context, time.getNSec(), 1_000_000);
        if (subMillis instanceof RubyRational) {
            subMillisNum = ((RubyRational) subMillis).getNumerator().getLongValue();
            subMillisDen = ((RubyRational) subMillis).getDenominator().getLongValue();
        } else {//from w ww .  j av a  2s.  c om
            subMillisNum = ((RubyInteger) subMillis).getLongValue();
        }
    }

    final int off = dt.getZone().getOffset(dt.getMillis()) / 1000;

    int year = dt.getYear();
    if (year <= 0)
        year--; // JODA's Julian chronology (no year 0)

    if (year == 1582) { // take the "slow" path -  JODA isn't adjusting for missing (reform) dates
        return calcAjdFromCivil(context, dt, off, subMillisNum, subMillisDen);
    }

    dt = new DateTime(year, dt.getMonthOfYear(), dt.getDayOfMonth(), dt.getHourOfDay(), dt.getMinuteOfHour(),
            dt.getSecondOfMinute(), dt.getMillisOfSecond(), getChronology(context, ITALY, dt.getZone()));

    return new RubyDateTime(context.runtime, getDateTime(context.runtime), dt, off, ITALY, subMillisNum,
            subMillisDen);
}

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());

    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 ww  w.j a v a 2 s  .  co 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  w  w  .j  a va 2  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;//from   ww  w  .  j  av a 2  s  .  com
        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;//  www .ja  v  a2 s. com
        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;
}