Search in sources :

Example 6 with Timestamp

use of com.amazon.ion.Timestamp in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method timestamps.

@Test
public void timestamps() throws Exception {
    final List<Timestamp> timestamps = new ArrayList<Timestamp>();
    timestamps.add(Timestamp.valueOf("2000T"));
    timestamps.add(Timestamp.valueOf("2000-01T"));
    timestamps.add(Timestamp.valueOf("2000-01-02T"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.6Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.06Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.006Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.600Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.060Z"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.060-07:00"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.060+07:00"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05+07:00"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04+07:00"));
    timestamps.add(Timestamp.valueOf("2000-01-02T03:04:05.9999999Z"));
    IonReaderBinaryIncremental reader = readerFor(new WriterFunction() {

        @Override
        public void write(IonWriter writer) throws IOException {
            for (Timestamp timestamp : timestamps) {
                writer.writeTimestamp(timestamp);
            }
        }
    });
    for (Timestamp timestamp : timestamps) {
        assertEquals(IonType.TIMESTAMP, reader.next());
        assertEquals(timestamp, reader.timestampValue());
    }
    assertNull(reader.next());
    assertNull(reader.getType());
    reader.close();
}
Also used : ArrayList(java.util.ArrayList) IonWriter(com.amazon.ion.IonWriter) IOException(java.io.IOException) Timestamp(com.amazon.ion.Timestamp) Test(org.junit.Test)

Example 7 with Timestamp

use of com.amazon.ion.Timestamp in project ion-java by amzn.

the class IonReaderBinaryRawLargeStreamTest method cleanlyFailsOnLargeContainerIncremental.

@Test
public void cleanlyFailsOnLargeContainerIncremental() throws Exception {
    final Timestamp timestamp = Timestamp.forDay(2000, 1, 1);
    byte[] data = testData(timestamp);
    // 42 makes the value exceed Integer.MAX_VALUE by an arbitrary amount.
    final int totalNumberOfBatches = (Integer.MAX_VALUE / data.length) + 42;
    ByteArrayOutputStream header = new ByteArrayOutputStream();
    header.write(BINARY_VERSION_MARKER_1_0);
    // S-exp with length subfield.
    header.write(0xCE);
    // Length
    IonBinary.writeVarUInt(header, (long) data.length * totalNumberOfBatches);
    InputStream inputStream = new SequenceInputStream(new ByteArrayInputStream(header.toByteArray()), // This will provide the data 'totalNumberOfBatches' times
    new RepeatInputStream(data, totalNumberOfBatches - 1));
    IonReader reader = IonReaderBuilder.standard().withIncrementalReadingEnabled(true).build(inputStream);
    thrown.expect(IonException.class);
    reader.next();
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IonReader(com.amazon.ion.IonReader) RepeatInputStream(com.amazon.ion.util.RepeatInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Timestamp(com.amazon.ion.Timestamp) Test(org.junit.Test)

Example 8 with Timestamp

use of com.amazon.ion.Timestamp in project ion-java by amzn.

the class ReverseBinaryEncoder method writeIonTimestampContent.

private void writeIonTimestampContent(IonTimestamp val) {
    if (val.isNullValue()) {
        writeByte((byte) (TYPE_TIMESTAMP | NULL_LENGTH_MASK));
    } else {
        final int originalOffset = myBuffer.length - myOffset;
        Timestamp t = val.timestampValue();
        // Time and date portion
        switch(t.getPrecision()) {
            // Fall through each case - by design
            case FRACTION:
            case SECOND:
                {
                    BigDecimal fraction = t.getZFractionalSecond();
                    if (fraction != null) {
                        assert (fraction.signum() >= 0 && !fraction.equals(BigDecimal.ZERO)) : "Bad timestamp fraction: " + fraction;
                        writeIonDecimalContent(fraction);
                    }
                    writeVarUInt(t.getZSecond());
                }
            case MINUTE:
                writeVarUInt(t.getZMinute());
                writeVarUInt(t.getZHour());
            case DAY:
                writeVarUInt(t.getZDay());
            case MONTH:
                writeVarUInt(t.getZMonth());
            case YEAR:
                writeVarUInt(t.getZYear());
                break;
            default:
                throw new IllegalStateException("unrecognized Timestamp precision: " + t.getPrecision());
        }
        // Offset portion
        Integer offset = t.getLocalOffset();
        if (offset == null) {
            // Negative 0 (no timezone)
            writeByte((byte) (0x80 | 0x40));
        } else {
            writeVarInt(offset.intValue());
        }
        writePrefix(TYPE_TIMESTAMP, myBuffer.length - myOffset - originalOffset);
    }
}
Also used : BigInteger(java.math.BigInteger) Timestamp(com.amazon.ion.Timestamp) IonTimestamp(com.amazon.ion.IonTimestamp) com.amazon.ion.impl._Private_IonConstants.tidTimestamp(com.amazon.ion.impl._Private_IonConstants.tidTimestamp) BigDecimal(java.math.BigDecimal)

Example 9 with Timestamp

use of com.amazon.ion.Timestamp in project ion-java by amzn.

the class _Private_IonWriterBase method writeTimestampUTC.

public void writeTimestampUTC(Date value) throws IOException {
    Timestamp time = Timestamp.forDateZ(value);
    writeTimestamp(time);
}
Also used : Timestamp(com.amazon.ion.Timestamp)

Example 10 with Timestamp

use of com.amazon.ion.Timestamp in project ion-java by amzn.

the class IonReaderTextSystemX method getIonValue.

public IonValue getIonValue(IonSystem sys) {
    if (isNullValue()) {
        switch(_value_type) {
            case NULL:
                return sys.newNull();
            case BOOL:
                return sys.newNullBool();
            case INT:
                return sys.newNullInt();
            case FLOAT:
                return sys.newNullFloat();
            case DECIMAL:
                return sys.newNullDecimal();
            case TIMESTAMP:
                return sys.newNullTimestamp();
            case SYMBOL:
                return sys.newNullSymbol();
            case STRING:
                return sys.newNullString();
            case CLOB:
                return sys.newNullClob();
            case BLOB:
                return sys.newNullBlob();
            case LIST:
                return sys.newNullList();
            case SEXP:
                return sys.newNullSexp();
            case STRUCT:
                return sys.newNullString();
            default:
                throw new IonException("unrecognized type encountered");
        }
    }
    switch(_value_type) {
        case NULL:
            return sys.newNull();
        case BOOL:
            return sys.newBool(booleanValue());
        case INT:
            return sys.newInt(longValue());
        case FLOAT:
            return sys.newFloat(doubleValue());
        case DECIMAL:
            return sys.newDecimal(decimalValue());
        case TIMESTAMP:
            IonTimestamp t = sys.newNullTimestamp();
            Timestamp ti = timestampValue();
            t.setValue(ti);
            return t;
        case SYMBOL:
            return sys.newSymbol(stringValue());
        case STRING:
            return sys.newString(stringValue());
        case CLOB:
            IonClob clob = sys.newNullClob();
            // FIXME inefficient: both newBytes and setBytes copy the data
            clob.setBytes(newBytes());
            return clob;
        case BLOB:
            IonBlob blob = sys.newNullBlob();
            // FIXME inefficient: both newBytes and setBytes copy the data
            blob.setBytes(newBytes());
            return blob;
        case LIST:
            IonList list = sys.newNullList();
            fillContainerList(sys, list);
            return list;
        case SEXP:
            IonSexp sexp = sys.newNullSexp();
            fillContainerList(sys, sexp);
            return sexp;
        case STRUCT:
            IonStruct struct = sys.newNullStruct();
            fillContainerStruct(sys, struct);
            return struct;
        default:
            throw new IonException("unrecognized type encountered");
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonSexp(com.amazon.ion.IonSexp) IonException(com.amazon.ion.IonException) IonTimestamp(com.amazon.ion.IonTimestamp) IonList(com.amazon.ion.IonList) IonClob(com.amazon.ion.IonClob) IonBlob(com.amazon.ion.IonBlob) Timestamp(com.amazon.ion.Timestamp) IonTimestamp(com.amazon.ion.IonTimestamp)

Aggregations

Timestamp (com.amazon.ion.Timestamp)51 Test (org.junit.Test)38 Instant (java.time.Instant)13 OffsetDateTime (java.time.OffsetDateTime)10 ZonedDateTime (java.time.ZonedDateTime)10 IonReader (com.amazon.ion.IonReader)8 IonObjectMapper (com.fasterxml.jackson.dataformat.ion.IonObjectMapper)8 RepeatInputStream (com.amazon.ion.util.RepeatInputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 InputStream (java.io.InputStream)7 SequenceInputStream (java.io.SequenceInputStream)7 BigInteger (java.math.BigInteger)7 IonTimestamp (com.amazon.ion.IonTimestamp)6 BigDecimal (java.math.BigDecimal)5 Temporal (java.time.temporal.Temporal)5 Decimal (com.amazon.ion.Decimal)2 IonException (com.amazon.ion.IonException)2 IonWriter (com.amazon.ion.IonWriter)2 IonBlob (com.amazon.ion.IonBlob)1