use of org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate in project legend-pure by finos.
the class TestPureDate method testAddMilliseconds.
@Test
public void testAddMilliseconds() {
PureDate date = DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "780013429");
Assert.assertSame(date, date.addMilliseconds(0));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 34, "779013429"), date.addMilliseconds(999));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 32, "781013429"), date.addMilliseconds(-999));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "783013429"), date.addMilliseconds(3));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "777013429"), date.addMilliseconds(-3));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 34, "780013429"), date.addMilliseconds(1000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 32, "780013429"), date.addMilliseconds(-1000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 43, "780013429"), date.addMilliseconds(10000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 23, "780013429"), date.addMilliseconds(-10000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 44, "603013429"), date.addMilliseconds(10823));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 22, "957013429"), date.addMilliseconds(-10823));
}
use of org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate in project legend-pure by finos.
the class TestPureDate method testFormatWithMultipleTimeZones.
@Test
public void testFormatWithMultipleTimeZones() {
PureDate date = DateFunctions.newPureDate(2014, 1, 1, 1, 1, 1, "070004235");
Assert.assertEquals("2013-12-31 20:01:01.070-0500", date.format("[EST]yyyy-MM-dd HH:mm:ss.SSSZ"));
Assert.assertEquals("2013-12-31 20:01:01.070-0500", date.format("[EST]yyyy-MM-dd [EST]HH:mm:ss.SSSZ"));
try {
date.format("[EST]yyyy-MM-dd [CST] HH:mm:ss.SSSZ");
Assert.fail();
} catch (IllegalArgumentException e) {
Assert.assertEquals("Cannot set multiple timezones: EST, CST", e.getMessage());
}
}
use of org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate in project legend-pure by finos.
the class TestPureDate method testFormat.
@Test
public void testFormat() {
PureDate date = DateFunctions.newPureDate(2014, 3, 10, 16, 12, 35, "070004235");
Assert.assertEquals("2014", date.format("yyyy"));
Assert.assertEquals("2014-3", date.format("yyyy-M"));
Assert.assertEquals("2014-03", date.format("yyyy-MM"));
Assert.assertEquals("2014-003", date.format("yyyy-MMM"));
Assert.assertEquals("2014-03-10", date.format("yyyy-MM-d"));
Assert.assertEquals("2014-03-10", date.format("yyyy-MM-dd"));
Assert.assertEquals("2014-03-10 4:12:35PM", date.format("yyyy-MM-dd h:mm:ssa"));
Assert.assertEquals("2014-03-10 16:12:35.070004235 GMT", date.format("yyyy-MM-dd HH:mm:ss.SSSS z"));
Assert.assertEquals("2014-03-10T16:12:35.070004235+0000", date.format("yyyy-MM-dd\"T\"HH:mm:ss.SSSSZ"));
Assert.assertEquals("2014-03-10 16:12:35.070Z", date.format("yyyy-MM-dd HH:mm:ss.SSSX"));
}
use of org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate in project legend-pure by finos.
the class TestPureDate method testAddMicroseconds.
@Test
public void testAddMicroseconds() {
PureDate date = DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "780013429");
Assert.assertSame(date, date.addMicroseconds(0));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "781012429"), date.addMicroseconds(999));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "779014429"), date.addMicroseconds(-999));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "780016429"), date.addMicroseconds(3));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "780010429"), date.addMicroseconds(-3));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "781013429"), date.addMicroseconds(1000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "779013429"), date.addMicroseconds(-1000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "790013429"), date.addMicroseconds(10000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "770013429"), date.addMicroseconds(-10000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "790836429"), date.addMicroseconds(10823));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 33, "769190429"), date.addMicroseconds(-10823));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 34, "780013429"), date.addMicroseconds(1_000_000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 32, "780013429"), date.addMicroseconds(-1_000_000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 43, "780013429"), date.addMicroseconds(10_000_000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 23, "780013429"), date.addMicroseconds(-10_000_000));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 44, "603014429"), date.addMicroseconds(10_823_001));
Assert.assertEquals(DateFunctions.newPureDate(2016, 5, 17, 10, 26, 22, "957012429"), date.addMicroseconds(-10_823_001));
}
use of org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate in project legend-pure by finos.
the class PureStringFormat method format.
public static String format(String formatString, Iterable<?> formatArgs, Function2<Object, ExecutionSupport, String> toRepresentationFunction, ExecutionSupport executionSupport) {
Iterator<?> argIterator = formatArgs.iterator();
int index = 0;
int length = formatString.length();
StringBuilder builder = new StringBuilder(length * 2);
try {
while (index < length) {
char character = formatString.charAt(index++);
if (character == '%') {
switch(formatString.charAt(index++)) {
case '%':
{
builder.append('%');
break;
}
case 's':
{
Object arg = argIterator.next();
builder.append(CompiledSupport.pureToString(arg, executionSupport));
break;
}
case 'r':
{
Object arg = toRepresentationFunction.value(argIterator.next(), executionSupport);
builder.append(CompiledSupport.pureToString(arg, executionSupport));
break;
}
case 'd':
{
Object arg = argIterator.next();
if (arg instanceof Long) {
builder.append(((Long) arg).longValue());
} else if (arg instanceof Integer) {
builder.append(((Integer) arg).intValue());
} else if (arg instanceof BigInteger) {
builder.append(arg.toString());
} else {
throw new IllegalArgumentException("Expected Integer, got: " + arg);
}
break;
}
case 't':
{
Object arg = argIterator.next();
if (!(arg instanceof PureDate)) {
throw new IllegalArgumentException("Expected Date, got: " + arg);
}
int dateFormatEnd = FormatTools.findEndOfDateFormatString(formatString, index);
if (dateFormatEnd == -1) {
builder.append(arg);
} else {
((PureDate) arg).format(builder, formatString.substring(index + 1, dateFormatEnd));
index = dateFormatEnd + 1;
}
break;
}
case '0':
{
int j = index;
while (Character.isDigit(formatString.charAt(j))) {
j++;
}
if (formatString.charAt(j) != 'd') {
throw new IllegalArgumentException("Invalid format specifier: %" + formatString.substring(index, j + 1));
}
int zeroPad = Integer.valueOf(formatString.substring(index, j));
Object arg = argIterator.next();
if (!(arg instanceof Long) && !(arg instanceof Integer) && !(arg instanceof BigInteger)) {
throw new IllegalArgumentException("Expected Integer, got: " + arg);
}
FormatTools.appendIntegerString(builder, arg.toString(), zeroPad);
index = j + 1;
break;
}
case 'f':
{
Object arg = argIterator.next();
if (!(arg instanceof Double) && !(arg instanceof Float) && !(arg instanceof BigDecimal)) {
throw new IllegalArgumentException("Expected Float, got: " + arg);
}
FormatTools.appendFloatString(builder, CompiledSupport.pureToString(arg, executionSupport));
break;
}
case '.':
{
int j = index;
while (Character.isDigit(formatString.charAt(j))) {
j++;
}
if (formatString.charAt(j) != 'f') {
throw new IllegalArgumentException("Invalid format specifier: %" + formatString.substring(index, j + 1));
}
int precision = Integer.valueOf(formatString.substring(index, j));
Object arg = argIterator.next();
if (!(arg instanceof Double) && !(arg instanceof Float) && !(arg instanceof BigDecimal)) {
throw new IllegalArgumentException("Expected Float, got: " + arg);
}
FormatTools.appendFloatString(builder, CompiledSupport.pureToString(arg, executionSupport), precision);
index = j + 1;
break;
}
default:
{
throw new IllegalArgumentException("Invalid format specifier: %" + formatString.charAt(index - 1));
}
}
} else {
builder.append(character);
}
}
} catch (NoSuchElementException e) {
throw new PureExecutionException("Too few arguments passed to format function. Format expression \"" + formatString + "\", number of arguments [" + Iterate.sizeOf(formatArgs) + "]");
}
if (argIterator.hasNext()) {
throw new RuntimeException(new PureExecutionException("Unused format args. [" + Iterate.sizeOf(formatArgs) + "] arguments provided to expression \"" + formatString + "\""));
}
return builder.toString();
}
Aggregations