Sha256: 6073bbf70fc6ed7a53b9ebd3673b56c45ae8d8e8cfe82899eef1d1597e0ab917

Contents?: true

Size: 1.57 KB

Versions: 8

Compression:

Stored size: 1.57 KB

Contents

package org.embulk.spi.time;

import java.util.Locale;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.jruby.embed.ScriptingContainer;
import org.jruby.util.RubyDateFormat;
import org.embulk.config.Task;
import org.embulk.config.Config;
import org.embulk.config.ConfigInject;
import org.embulk.config.ConfigDefault;
import org.embulk.config.ConfigException;
import org.embulk.spi.util.LineEncoder;

public class TimestampFormatter
{
    public interface FormatterTask
            extends Task
    {
        @Config("timezone")
        @ConfigDefault("\"UTC\"")
        public DateTimeZone getTimeZone();

        @ConfigInject
        public ScriptingContainer getJRuby();
    }

    private final RubyDateFormat dateFormat;
    private final DateTimeZone timeZone;

    public TimestampFormatter(String format, FormatterTask task)
    {
        this(task.getJRuby(), format, task.getTimeZone());
    }

    public TimestampFormatter(ScriptingContainer jruby, String format, DateTimeZone timeZone)
    {
        this.timeZone = timeZone;
        this.dateFormat = new RubyDateFormat(format, Locale.ENGLISH, true);
    }

    public void format(Timestamp value, LineEncoder encoder)
    {
        // TODO optimize by directly appending to internal buffer
        encoder.addText(format(value));
    }

    public String format(Timestamp value)
    {
        // TODO optimize by using reused StringBuilder
        dateFormat.setDateTime(new DateTime(value.toEpochMilli(), timeZone));
        dateFormat.setNSec(value.getNano());
        return dateFormat.format(null);
    }
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
embulk-0.4.7 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.4.6 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.4.5 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.4.4 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.4.3 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.4.2 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.4.1 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.4.0 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java