Search in sources :

Example 1 with PureDate

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));
}
Also used : PureDate(org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate) Test(org.junit.Test)

Example 2 with PureDate

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());
    }
}
Also used : PureDate(org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate) Test(org.junit.Test)

Example 3 with PureDate

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"));
}
Also used : PureDate(org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate) Test(org.junit.Test)

Example 4 with PureDate

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));
}
Also used : PureDate(org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate) Test(org.junit.Test)

Example 5 with PureDate

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();
}
Also used : PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) PureDate(org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

PureDate (org.finos.legend.pure.m4.coreinstance.primitive.date.PureDate)23 Test (org.junit.Test)11 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)7 BigDecimal (java.math.BigDecimal)5 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)5 BigInteger (java.math.BigInteger)3 RichIterable (org.eclipse.collections.api.RichIterable)2 Any (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Any)2 ConstraintsOverride (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ConstraintsOverride)2 PrimitiveType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.PrimitiveType)2 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)2 BooleanCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.BooleanCoreInstance)2 StringCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.StringCoreInstance)2 DateTime (org.finos.legend.pure.m4.coreinstance.primitive.date.DateTime)2 LatestDate (org.finos.legend.pure.m4.coreinstance.primitive.date.LatestDate)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 ListIterable (org.eclipse.collections.api.list.ListIterable)1 MutableMap (org.eclipse.collections.api.map.MutableMap)1