Search in sources :

Example 36 with DateTime

use of org.joda.time.DateTime in project openhab1-addons by openhab.

the class CommunicationService method setTime.

/**
     * This method set the time of the heat pump to the current time
     * 
     * @return true if time has been updated
     */
public Map<String, String> setTime() throws StiebelHeatPumpException {
    startCommunication();
    Map<String, String> data = new HashMap<String, String>();
    Request timeRequest = null;
    for (Request request : heatPumpSettingConfiguration) {
        if (request.getName().equals("Time")) {
            timeRequest = request;
            break;
        }
    }
    if (timeRequest == null) {
        logger.warn("Could not find request definition for time settings! Skip setting time.");
        return data;
    }
    logger.debug("Loading current time data ...");
    try {
        // get time from heat pump
        byte[] requestMessage = createRequestMessage(timeRequest);
        byte[] response = getData(requestMessage);
        // get current time from local machine
        DateTime dt = DateTime.now();
        logger.debug("Current time is : {}", dt.toString());
        String weekday = Integer.toString(dt.getDayOfWeek() - 1);
        String day = Integer.toString(dt.getDayOfMonth());
        String month = Integer.toString(dt.getMonthOfYear());
        String year = Integer.toString(dt.getYearOfCentury());
        String seconds = Integer.toString(dt.getSecondOfMinute());
        String hours = Integer.toString(dt.getHourOfDay());
        String minutes = Integer.toString(dt.getMinuteOfHour());
        data = parser.parseRecords(response, timeRequest);
        boolean updateRequired = false;
        for (Map.Entry<String, String> entry : data.entrySet()) {
            String entryName = entry.getKey();
            String entryValue = entry.getValue();
            RecordDefinition currentRecord = null;
            for (RecordDefinition record : timeRequest.getRecordDefinitions()) {
                if (record.getName().equals(entryName)) {
                    currentRecord = record;
                    break;
                }
            }
            if (entryName.equals("WeekDay") && !entryValue.equals(weekday)) {
                updateRequired = true;
                response = parser.composeRecord(weekday, response, currentRecord);
                logger.debug("WeekDay needs update from {} to {}", entryValue, weekday);
                continue;
            }
            if (entryName.equals("Hours") && !entryValue.equals(hours)) {
                updateRequired = true;
                response = parser.composeRecord(hours, response, currentRecord);
                logger.debug("Hours needs update from {} to {}", entryValue, hours);
                continue;
            }
            if (entryName.equals("Minutes") && !entryValue.equals(minutes)) {
                updateRequired = true;
                response = parser.composeRecord(minutes, response, currentRecord);
                logger.debug("Minutes needs update from {} to {}", entryValue, minutes);
                continue;
            }
            if (entryName.equals("Seconds") && !entryValue.equals(seconds)) {
                updateRequired = true;
                response = parser.composeRecord(seconds, response, currentRecord);
                logger.debug("Seconds needs update from {} to {}", entryValue, seconds);
                continue;
            }
            if (entryName.equals("Year") && !entryValue.equals(year)) {
                updateRequired = true;
                response = parser.composeRecord(year, response, currentRecord);
                logger.debug("Year needs update from {} to {}", entryValue, year);
                continue;
            }
            if (entryName.equals("Month") && !entryValue.equals(month)) {
                updateRequired = true;
                response = parser.composeRecord(month, response, currentRecord);
                logger.debug("Month needs update from {} to {}", entryValue, month);
                continue;
            }
            if (entryName.equals("Day") && !entryValue.equals(day)) {
                updateRequired = true;
                response = parser.composeRecord(day, response, currentRecord);
                logger.debug("Day needs update from {} to {}", entryValue, day);
                continue;
            }
        }
        if (updateRequired) {
            Thread.sleep(WAITING_TIME_BETWEEN_REQUESTS);
            logger.info("Time need update. Set time to " + dt.toString());
            setData(response);
            Thread.sleep(WAITING_TIME_BETWEEN_REQUESTS);
            response = getData(requestMessage);
            data = parser.parseRecords(response, timeRequest);
            dt = DateTime.now();
            logger.debug("Current time is : {}", dt.toString());
        }
        return data;
    } catch (InterruptedException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
}
Also used : HashMap(java.util.HashMap) Request(org.openhab.binding.stiebelheatpump.protocol.Request) HashMap(java.util.HashMap) Map(java.util.Map) DateTime(org.joda.time.DateTime) RecordDefinition(org.openhab.binding.stiebelheatpump.protocol.RecordDefinition)

Example 37 with DateTime

use of org.joda.time.DateTime in project presto by prestodb.

the class TestHiveIntegrationSmokeTest method createTableWithEveryType.

@Test
public void createTableWithEveryType() throws Exception {
    @Language("SQL") String query = "" + "CREATE TABLE test_types_table AS " + "SELECT" + " 'foo' _varchar" + ", cast('bar' as varbinary) _varbinary" + ", cast(1 as bigint) _bigint" + ", 2 _integer" + ", CAST('3.14' AS DOUBLE) _double" + ", true _boolean" + ", DATE '1980-05-07' _date" + ", TIMESTAMP '1980-05-07 11:22:33.456' _timestamp" + ", CAST('3.14' AS DECIMAL(3,2)) _decimal_short" + ", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _decimal_long" + ", CAST('bar' AS CHAR(10)) _char";
    assertUpdate(query, 1);
    MaterializedResult results = getQueryRunner().execute(getSession(), "SELECT * FROM test_types_table").toJdbcTypes();
    assertEquals(results.getRowCount(), 1);
    MaterializedRow row = results.getMaterializedRows().get(0);
    assertEquals(row.getField(0), "foo");
    assertEquals(row.getField(1), "bar".getBytes(UTF_8));
    assertEquals(row.getField(2), 1L);
    assertEquals(row.getField(3), 2);
    assertEquals(row.getField(4), 3.14);
    assertEquals(row.getField(5), true);
    assertEquals(row.getField(6), new Date(new DateTime(1980, 5, 7, 0, 0, 0, UTC).getMillis()));
    assertEquals(row.getField(7), new Timestamp(new DateTime(1980, 5, 7, 11, 22, 33, 456, UTC).getMillis()));
    assertEquals(row.getField(8), new BigDecimal("3.14"));
    assertEquals(row.getField(9), new BigDecimal("12345678901234567890.0123456789"));
    assertEquals(row.getField(10), "bar       ");
    assertUpdate("DROP TABLE test_types_table");
    assertFalse(getQueryRunner().tableExists(getSession(), "test_types_table"));
}
Also used : Language(org.intellij.lang.annotations.Language) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Timestamp(java.sql.Timestamp) MaterializedRow(com.facebook.presto.testing.MaterializedRow) Date(java.sql.Date) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test) AbstractTestIntegrationSmokeTest(com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)

Example 38 with DateTime

use of org.joda.time.DateTime in project presto by prestodb.

the class TestExpressionInterpreter method optimize.

private static Object optimize(@Language("SQL") String expression) {
    assertRoundTrip(expression);
    Expression parsedExpression = FunctionAssertions.createExpression(expression, METADATA, SYMBOL_TYPES);
    IdentityLinkedHashMap<Expression, Type> expressionTypes = getExpressionTypes(TEST_SESSION, METADATA, SQL_PARSER, SYMBOL_TYPES, parsedExpression, emptyList());
    ExpressionInterpreter interpreter = expressionOptimizer(parsedExpression, METADATA, TEST_SESSION, expressionTypes);
    return interpreter.optimize(symbol -> {
        switch(symbol.getName().toLowerCase(ENGLISH)) {
            case "bound_integer":
                return 1234L;
            case "bound_long":
                return 1234L;
            case "bound_string":
                return utf8Slice("hello");
            case "bound_double":
                return 12.34;
            case "bound_date":
                return new LocalDate(2001, 8, 22).toDateMidnight(DateTimeZone.UTC).getMillis();
            case "bound_time":
                return new LocalTime(3, 4, 5, 321).toDateTime(new DateTime(0, DateTimeZone.UTC)).getMillis();
            case "bound_timestamp":
                return new DateTime(2001, 8, 22, 3, 4, 5, 321, DateTimeZone.UTC).getMillis();
            case "bound_pattern":
                return utf8Slice("%el%");
            case "bound_timestamp_with_timezone":
                return new SqlTimestampWithTimeZone(new DateTime(1970, 1, 1, 1, 0, 0, 999, DateTimeZone.UTC).getMillis(), getTimeZoneKey("Z"));
            case "bound_varbinary":
                return Slices.wrappedBuffer((byte) 0xab);
        }
        return symbol.toSymbolReference();
    });
}
Also used : VarbinaryType(com.facebook.presto.spi.type.VarbinaryType) VarcharType.createVarcharType(com.facebook.presto.spi.type.VarcharType.createVarcharType) Type(com.facebook.presto.spi.type.Type) SqlTimestampWithTimeZone(com.facebook.presto.spi.type.SqlTimestampWithTimeZone) LocalTime(org.joda.time.LocalTime) ExpressionFormatter.formatExpression(com.facebook.presto.sql.ExpressionFormatter.formatExpression) Expression(com.facebook.presto.sql.tree.Expression) ExpressionInterpreter(com.facebook.presto.sql.planner.ExpressionInterpreter) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime)

Example 39 with DateTime

use of org.joda.time.DateTime in project presto by prestodb.

the class TestExpressionInterpreter method testExtract.

@Test
public void testExtract() {
    DateTime dateTime = new DateTime(2001, 8, 22, 3, 4, 5, 321, DateTimeZone.UTC);
    double seconds = dateTime.getMillis() / 1000.0;
    assertOptimizedEquals("extract (YEAR from from_unixtime(" + seconds + "))", "2001");
    assertOptimizedEquals("extract (QUARTER from from_unixtime(" + seconds + "))", "3");
    assertOptimizedEquals("extract (MONTH from from_unixtime(" + seconds + "))", "8");
    assertOptimizedEquals("extract (WEEK from from_unixtime(" + seconds + "))", "34");
    assertOptimizedEquals("extract (DOW from from_unixtime(" + seconds + "))", "3");
    assertOptimizedEquals("extract (DOY from from_unixtime(" + seconds + "))", "234");
    assertOptimizedEquals("extract (DAY from from_unixtime(" + seconds + "))", "22");
    assertOptimizedEquals("extract (HOUR from from_unixtime(" + seconds + "))", "3");
    assertOptimizedEquals("extract (MINUTE from from_unixtime(" + seconds + "))", "4");
    assertOptimizedEquals("extract (SECOND from from_unixtime(" + seconds + "))", "5");
    assertOptimizedEquals("extract (TIMEZONE_HOUR from from_unixtime(" + seconds + ", 7, 9))", "7");
    assertOptimizedEquals("extract (TIMEZONE_MINUTE from from_unixtime(" + seconds + ", 7, 9))", "9");
    assertOptimizedEquals("extract (YEAR from bound_timestamp)", "2001");
    assertOptimizedEquals("extract (QUARTER from bound_timestamp)", "3");
    assertOptimizedEquals("extract (MONTH from bound_timestamp)", "8");
    assertOptimizedEquals("extract (WEEK from bound_timestamp)", "34");
    assertOptimizedEquals("extract (DOW from bound_timestamp)", "3");
    assertOptimizedEquals("extract (DOY from bound_timestamp)", "234");
    assertOptimizedEquals("extract (DAY from bound_timestamp)", "22");
    assertOptimizedEquals("extract (HOUR from bound_timestamp)", "3");
    assertOptimizedEquals("extract (MINUTE from bound_timestamp)", "4");
    assertOptimizedEquals("extract (SECOND from bound_timestamp)", "5");
    // todo reenable when cast as timestamp with time zone is implemented
    // todo add bound timestamp with time zone
    //assertOptimizedEquals("extract (TIMEZONE_HOUR from bound_timestamp)", "0");
    //assertOptimizedEquals("extract (TIMEZONE_MINUTE from bound_timestamp)", "0");
    assertOptimizedEquals("extract (YEAR from unbound_timestamp)", "extract (YEAR from unbound_timestamp)");
    assertOptimizedEquals("extract (SECOND from bound_timestamp + INTERVAL '3' SECOND)", "8");
}
Also used : DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 40 with DateTime

use of org.joda.time.DateTime in project presto by prestodb.

the class TestDateTimeFunctions method testDateDiffDate.

@Test
public void testDateDiffDate() {
    DateTime baseDateTime = new DateTime(1960, 5, 3, 0, 0, 0, 0, DateTimeZone.UTC);
    String baseDateTimeLiteral = "DATE '1960-05-03'";
    assertFunction("date_diff('day', " + baseDateTimeLiteral + ", " + DATE_LITERAL + ")", BIGINT, (long) daysBetween(baseDateTime, DATE).getDays());
    assertFunction("date_diff('week', " + baseDateTimeLiteral + ", " + DATE_LITERAL + ")", BIGINT, (long) weeksBetween(baseDateTime, DATE).getWeeks());
    assertFunction("date_diff('month', " + baseDateTimeLiteral + ", " + DATE_LITERAL + ")", BIGINT, (long) monthsBetween(baseDateTime, DATE).getMonths());
    assertFunction("date_diff('quarter', " + baseDateTimeLiteral + ", " + DATE_LITERAL + ")", BIGINT, (long) monthsBetween(baseDateTime, DATE).getMonths() / 3);
    assertFunction("date_diff('year', " + baseDateTimeLiteral + ", " + DATE_LITERAL + ")", BIGINT, (long) yearsBetween(baseDateTime, DATE).getYears());
}
Also used : DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

DateTime (org.joda.time.DateTime)3596 Test (org.junit.Test)1091 Test (org.testng.annotations.Test)499 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)349 ResponseBody (okhttp3.ResponseBody)332 ArrayList (java.util.ArrayList)322 LocalDate (org.joda.time.LocalDate)257 Date (java.util.Date)246 Interval (org.joda.time.Interval)204 Result (io.druid.query.Result)153 HashMap (java.util.HashMap)149 ServiceCall (com.microsoft.rest.ServiceCall)148 List (java.util.List)137 BigDecimal (java.math.BigDecimal)132 DateTimeZone (org.joda.time.DateTimeZone)129 UUID (java.util.UUID)104 LocalDateTime (org.joda.time.LocalDateTime)103 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)95 IOException (java.io.IOException)94 Map (java.util.Map)90