use of com.amazon.ion.Timestamp.Precision in project ion-java by amzn.
the class TimestampTest method testNewTimestampFromUtcFieldsWithDifferentPrecisions.
/**
* Test for {@link Timestamp#createFromUtcFields(Precision, int, int, int, int, int, int, BigDecimal, Integer)}
* ensuring that varying precisions produce Timestamps as expected as per
* precision "narrowing".
*/
@Test
public void testNewTimestampFromUtcFieldsWithDifferentPrecisions() {
// Non-varying time components
int zyear = 2012;
int zmonth = 2;
int zday = 3;
int zhour = 4;
int zminute = 5;
int zsecond = 6;
BigDecimal zfrac = new BigDecimal("0.007");
Integer offset = null;
// Varying precisions
Precision p;
Timestamp ts;
p = YEAR;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012T
checkFields(zyear, 1, 1, 0, 0, 0, null, offset, p, ts);
p = MONTH;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02T
checkFields(zyear, zmonth, 1, 0, 0, 0, null, offset, p, ts);
p = DAY;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-03Z
checkFields(zyear, zmonth, zday, 0, 0, 0, null, offset, p, ts);
p = MINUTE;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-03T04:05-00:00
checkFields(zyear, zmonth, zday, zhour, zminute, 0, null, offset, p, ts);
p = SECOND;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-03T04:05:06-00:00
checkFields(zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset, p, ts);
p = FRACTION;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-03T04:05:06.007-00:00
checkFields(zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset, p, ts);
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, BigDecimal.ZERO, 0);
assertEquals(Timestamp.valueOf("2012-02-03T04:05:06Z"), ts);
}
use of com.amazon.ion.Timestamp.Precision in project ion-java by amzn.
the class TimestampTest method testNewTimestampFromUtcFieldsWithDifferentOffsets.
/**
* Test for {@link Timestamp#createFromUtcFields(Precision, int, int, int, int, int, int, BigDecimal, Integer)}
* ensuring that varying local offsets produce Timestamps as expected.
*/
@Test
public void testNewTimestampFromUtcFieldsWithDifferentOffsets() {
// Non-varying time components
int zyear = 2012;
int zmonth = 2;
int zday = 3;
int zhour = 4;
int zminute = 5;
int zsecond = 6;
BigDecimal zfrac = new BigDecimal("0.007");
Precision p = FRACTION;
// Varying local offsets (in minutes)
Integer offset;
Timestamp ts;
// unknown local offset
offset = null;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-03T04:05:06.007-00:00
checkFields(zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset, p, ts);
// zero local offset
offset = 0;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-03T04:05:06.007Z
checkFields(zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset, p, ts);
// 8 hours
offset = 480;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-03T12:05:06.007+08:00
checkFields(zyear, zmonth, zday, 12, zminute, zsecond, zfrac, offset, p, ts);
// -8 hours
offset = -480;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-02T20:05:06.007-08:00
checkFields(zyear, zmonth, 2, 20, zminute, zsecond, zfrac, offset, p, ts);
// 2 hours 3 minutes
offset = 123;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-03T06:08:06.007+02:03
checkFields(zyear, zmonth, zday, 6, 8, zsecond, zfrac, offset, p, ts);
// -2 hours 3 minutes
offset = -123;
ts = Timestamp.createFromUtcFields(p, zyear, zmonth, zday, zhour, zminute, zsecond, zfrac, offset);
// 2012-02-03T02:02:06.007-02:03
checkFields(zyear, zmonth, zday, 2, 2, zsecond, zfrac, offset, p, ts);
}
use of com.amazon.ion.Timestamp.Precision in project ion-java by amzn.
the class IonReaderBinaryRawX method readTimestamp.
/**
* @see IonBinary.Reader#readTimestampValue
*/
protected final Timestamp readTimestamp(int len) throws IOException {
if (len < 1) {
// nothing to do here - and the timestamp will be NULL
return null;
}
int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
BigDecimal frac = null;
long save_limit = NO_LIMIT;
if (_local_remaining != NO_LIMIT) {
save_limit = _local_remaining - len;
}
// > 0
_local_remaining = len;
// first up is the offset, which requires a special int reader
// to return the -0 as a null Integer
Integer offset = readVarInteger();
// now we'll read the struct values from the input stream
// year is from 0001 to 9999
// or 0x1 to 0x270F or 14 bits - 1 or 2 bytes
year = readVarUInt();
// our lowest significant option
Precision p = Precision.YEAR;
// now we look for months
if (_local_remaining > 0) {
month = readVarUInt();
p = Precision.MONTH;
// now we look for days
if (_local_remaining > 0) {
day = readVarUInt();
// our lowest significant option
p = Precision.DAY;
// now we look for hours and minutes
if (_local_remaining > 0) {
hour = readVarUInt();
minute = readVarUInt();
p = Precision.MINUTE;
if (_local_remaining > 0) {
second = readVarUInt();
p = Precision.SECOND;
if (_local_remaining > 0) {
// now we read in our actual "milliseconds since the epoch"
frac = readDecimal((int) _local_remaining);
if (frac.compareTo(BigDecimal.ZERO) < 0 || frac.compareTo(BigDecimal.ONE) >= 0) {
throwErrorAt("The fractional seconds value in a timestamp must be greater than or " + "equal to zero and less than one.");
}
}
}
}
}
}
// restore out outer limit(s)
_local_remaining = save_limit;
// now we let timestamp put it all together
try {
Timestamp val = Timestamp.createFromUtcFields(p, year, month, day, hour, minute, second, frac, offset);
return val;
} catch (IllegalArgumentException e) {
// Rewrap to the expected type.
throw newErrorAt("Invalid timestamp encoding: " + e.getMessage());
}
}
use of com.amazon.ion.Timestamp.Precision in project ion-java by amzn.
the class IonValueDeltaGenerator method generateIonTimestamps.
private Set<IonValue> generateIonTimestamps() {
List<IonValue> dataList = new ArrayList<IonValue>();
Precision precisionToSet = null;
Precision precisionToDelta = null;
switch((IonTimestampDeltaType) deltaType) {
case YEAR:
precisionToSet = precisionToDelta = Precision.YEAR;
break;
case MONTH:
precisionToSet = precisionToDelta = Precision.MONTH;
break;
case DAY:
precisionToSet = precisionToDelta = Precision.DAY;
break;
case MINUTE:
precisionToSet = precisionToDelta = Precision.MINUTE;
break;
case SECOND:
precisionToSet = precisionToDelta = Precision.SECOND;
break;
case SECOND_YEAR:
precisionToSet = Precision.SECOND;
precisionToDelta = Precision.YEAR;
break;
case SECOND_MONTH:
precisionToSet = Precision.SECOND;
precisionToDelta = Precision.MONTH;
break;
case SECOND_DAY:
precisionToSet = Precision.SECOND;
precisionToDelta = Precision.DAY;
break;
case SECOND_MINUTE:
precisionToSet = Precision.SECOND;
precisionToDelta = Precision.MINUTE;
break;
case SECOND_SECOND:
precisionToSet = Precision.SECOND;
precisionToDelta = Precision.SECOND;
break;
default:
Assert.fail("not supported: " + deltaType);
}
IonTimestamp curr = (IonTimestamp) baseValue;
for (int i = 0; i < size; i++) {
IonTimestamp next = getIonTimestampWithDelta(curr.timestampValue(), precisionToDelta, precisionToSet, delta);
// There might be equal IonTimestamps generated due to there being
// no reliable way to generate IonTimestamp deltas. As such, we
// append to a List first, and convert it to a Set as the result to
// return.
dataList.add(next);
curr = next;
}
return new HashSet<IonValue>(dataList);
}
Aggregations