Sha256: a724cc4ccf3aa1b59992f6fed4d37012b41fdd84dae6fb9466b02a2bbca8e2a1

Contents?: true

Size: 1.65 KB

Versions: 6

Compression:

Stored size: 1.65 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 DateTimeZone getTimeZone()
    {
        return timeZone;
    }

    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.getEpochSecond()*1000, timeZone));
        dateFormat.setNSec(value.getNano());
        return dateFormat.format(null);
    }
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
embulk-0.6.13 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.6.12 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.6.11 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.6.10 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.6.9 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java
embulk-0.6.8 embulk-core/src/main/java/org/embulk/spi/time/TimestampFormatter.java