@Override public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException { if (value != null) { statement.setTimestamp(position, Timestamp.valueOf(value.atStartOfDay())); } else { statement.setNull(position, Types.TIMESTAMP); } } }
/** * Converts local date to Date. */ public static Date toDate(final LocalDate localDate) { return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); } /**
@Override public Object encode(final Object value, final MappedField optionalExtraInfo) { if (value == null) { return null; } LocalDate date = (LocalDate) value; return Date.from(date.atStartOfDay() .atZone(systemDefault()) .toInstant()); } }
public static Date toDate(String time, String pattern) { LocalDate formatted = LocalDate.parse(time, DateTimeFormatter.ofPattern(pattern)); return Date.from(Instant.from(formatted.atStartOfDay(ZoneId.systemDefault()))); }
public static Date toDate(String time, String pattern) { LocalDate formatted = LocalDate.parse(time, DateTimeFormatter.ofPattern(pattern)); return Date.from(Instant.from(formatted.atStartOfDay(ZoneId.systemDefault()))); }
/** * Return a date at the start of day. * * @param s string in format {@link #DATE_FORMAT} * @throws SonarException when string cannot be parsed */ public static Date parseDate(String s) { return Date.from(parseLocalDate(s).atStartOfDay(ZoneId.systemDefault()).toInstant()); }
/** * Converts local date time to epoh milliseconds assuming start of the day as time point. */ public static long toMilliseconds(LocalDate localDate) { return toMilliseconds(localDate.atStartOfDay()); }
public long toEpochMilli() { return localDate.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli(); }
public long toEpochSecond() { return localDate.atStartOfDay().toEpochSecond(ZoneOffset.UTC); }
default Selection isBefore(LocalDate value) { return isBefore(value.atStartOfDay()); }
@CheckForNull private static Instant parseDate(String propertyValue) { try { LocalDate localDate = LocalDate.parse(propertyValue); return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); } catch (DateTimeParseException e) { boolean invalidDate = e.getCause() == null || e.getCause() == e || !e.getCause().getMessage().contains("Invalid date"); checkPeriodProperty(invalidDate, propertyValue, "Invalid date"); return null; } }
private static void raw(Exchange bfx) throws IOException { /* BitfinexTradeServiceRaw tradeService = (BitfinexTradeServiceRaw) bfx.getTradeService(); LimitOrder limitOrder = new LimitOrder.Builder(OrderType.BID, CurrencyPair.BTC_USD).limitPrice(new BigDecimal("481.69")) .originalAmount(new BigDecimal("0.001")).build(); tradeService.placeBitfinexLimitOrder(limitOrder, BitfinexOrderType.LIMIT); */ BitfinexTradeServiceRaw tradeService = (BitfinexTradeServiceRaw) bfx.getTradeService(); Date tenDaysAgo = Date.from(LocalDate.now().minusDays(10).atStartOfDay(ZoneId.systemDefault()).toInstant()); BitfinexFundingTradeResponse[] fundingTradeResponses = tradeService.getBitfinexFundingHistory("USD", tenDaysAgo, 2000); } }
private Date getEpochDate() { return Date.from(LocalDate.of(1970, 1, 1).atStartOfDay(ZoneId.of("UTC")).toInstant()); } }
@Test public void testDateToTimestampCoercion() { // allow running tests with a connector that supports TIMESTAMP but not DATE // ordinary date MaterializedResult rows = h2QueryRunner.execute(TEST_SESSION, "SELECT DATE '2018-01-13'", ImmutableList.of(TIMESTAMP)); assertEquals(rows.getOnlyValue(), LocalDate.of(2018, 1, 13).atStartOfDay()); // date, which midnight was skipped in JVM zone LocalDate forwardOffsetChangeAtMidnightInJvmZone = LocalDate.of(1970, 1, 1); checkState(ZoneId.systemDefault().getRules().getValidOffsets(forwardOffsetChangeAtMidnightInJvmZone.atStartOfDay()).size() == 0, "This test assumes certain JVM time zone"); rows = h2QueryRunner.execute(TEST_SESSION, DateTimeFormatter.ofPattern("'SELECT DATE '''uuuu-MM-dd''").format(forwardOffsetChangeAtMidnightInJvmZone), ImmutableList.of(TIMESTAMP)); assertEquals(rows.getOnlyValue(), forwardOffsetChangeAtMidnightInJvmZone.atStartOfDay()); } }
@Before public void setUp() { localDateTimeColumn.append(LocalDate.of(2016, 2, 28).atStartOfDay()); // sunday localDateTimeColumn.append(LocalDate.of(2016, 2, 29).atStartOfDay()); // monday localDateTimeColumn.append(LocalDate.of(2016, 3, 1).atStartOfDay()); // tues localDateTimeColumn.append(LocalDate.of(2016, 3, 2).atStartOfDay()); // weds localDateTimeColumn.append(LocalDate.of(2016, 3, 3).atStartOfDay()); // thurs localDateTimeColumn.append(LocalDate.of(2016, 4, 1).atStartOfDay()); localDateTimeColumn.append(LocalDate.of(2016, 4, 2).atStartOfDay()); localDateTimeColumn.append(LocalDate.of(2016, 3, 4).atStartOfDay()); // fri localDateTimeColumn.append(LocalDate.of(2016, 3, 5).atStartOfDay()); // sat table.addColumns(localDateTimeColumn); }
private static CalendarEventModel.CalendarEventTime getEventTime(EventDateTime dateTime) { if (dateTime == null) { return null; } OffsetDateTime offsetDateTime; if (dateTime.getDate() == null) { offsetDateTime = OffsetDateTime.parse(dateTime.getDateTime().toString()); } else { offsetDateTime = OffsetDateTime.from( LocalDate.parse(dateTime.getDate().toString()).atStartOfDay(ZoneId.of("UTC"))); } return new CalendarEventModel.CalendarEventTime(offsetDateTime, dateTime.getDate() != null); }
@Test public void testLocallyUnrepresentableTimeLiterals() { LocalDateTime localTimeThatDidNotExist = LocalDateTime.of(2017, 4, 2, 2, 10); checkState(ZoneId.systemDefault().getRules().getValidOffsets(localTimeThatDidNotExist).isEmpty(), "This test assumes certain JVM time zone"); // This tests that both Presto runner and H2 can return TIMESTAMP value that never happened in JVM's zone (e.g. is not representable using java.sql.Timestamp) @Language("SQL") String sql = DateTimeFormatter.ofPattern("'SELECT TIMESTAMP '''uuuu-MM-dd HH:mm:ss''").format(localTimeThatDidNotExist); assertEquals(computeScalar(sql), localTimeThatDidNotExist); // this tests Presto and the QueryRunner assertQuery(sql); // this tests H2QueryRunner LocalDate localDateThatDidNotHaveMidnight = LocalDate.of(1970, 1, 1); checkState(ZoneId.systemDefault().getRules().getValidOffsets(localDateThatDidNotHaveMidnight.atStartOfDay()).isEmpty(), "This test assumes certain JVM time zone"); // This tests that both Presto runner and H2 can return DATE value for a day which midnight never happened in JVM's zone (e.g. is not exactly representable using java.sql.Date) sql = DateTimeFormatter.ofPattern("'SELECT DATE '''uuuu-MM-dd''").format(localDateThatDidNotHaveMidnight); assertEquals(computeScalar(sql), localDateThatDidNotHaveMidnight); // this tests Presto and the QueryRunner assertQuery(sql); // this tests H2QueryRunner LocalTime localTimeThatDidNotOccurOn19700101 = LocalTime.of(0, 10); checkState(ZoneId.systemDefault().getRules().getValidOffsets(localTimeThatDidNotOccurOn19700101.atDate(LocalDate.ofEpochDay(0))).isEmpty(), "This test assumes certain JVM time zone"); checkState(!Objects.equals(java.sql.Time.valueOf(localTimeThatDidNotOccurOn19700101).toLocalTime(), localTimeThatDidNotOccurOn19700101), "This test assumes certain JVM time zone"); sql = DateTimeFormatter.ofPattern("'SELECT TIME '''HH:mm:ss''").format(localTimeThatDidNotOccurOn19700101); assertEquals(computeScalar(sql), localTimeThatDidNotOccurOn19700101); // this tests Presto and the QueryRunner assertQuery(sql); // this tests H2QueryRunner } }
@Test public void testSimpleDate() { MapSettings settings = new MapSettings(); settings.appendProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01"); Clock clock = mock(Clock.class); ProjectAnalysisInfo info = new ProjectAnalysisInfo(settings.asConfig(), clock); info.start(); LocalDate date = LocalDate.of(2017, 1, 1); assertThat(info.analysisDate()).isEqualTo(Date.from(date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())); }
default DateTimeColumn atStartOfDay() { DateTimeColumn newColumn = DateTimeColumn.create(this.name() + " " + " start"); for (int r = 0; r < this.size(); r++) { LocalDate c1 = this.get(r); if (c1 == null) { newColumn.appendMissing(); } else { newColumn.append(c1.atStartOfDay()); } } return newColumn; }
@Test public void testDate() { // Note: there is identical test for PostgreSQL ZoneId jvmZone = ZoneId.systemDefault(); checkState(jvmZone.getId().equals("America/Bahia_Banderas"), "This test assumes certain JVM time zone"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInJvmZone = LocalDate.of(1970, 1, 1); verify(jvmZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInJvmZone.atStartOfDay()).isEmpty()); ZoneId someZone = ZoneId.of("Europe/Vilnius"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInSomeZone = LocalDate.of(1983, 4, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInSomeZone.atStartOfDay()).isEmpty()); LocalDate dateOfLocalTimeChangeBackwardAtMidnightInSomeZone = LocalDate.of(1983, 10, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeBackwardAtMidnightInSomeZone.atStartOfDay().minusMinutes(1)).size() == 2); DataTypeTest testCases = DataTypeTest.create() .addRoundTrip(dateDataType(), LocalDate.of(1952, 4, 3)) // before epoch .addRoundTrip(dateDataType(), LocalDate.of(1970, 1, 1)) .addRoundTrip(dateDataType(), LocalDate.of(1970, 2, 3)) .addRoundTrip(dateDataType(), LocalDate.of(2017, 7, 1)) // summer on northern hemisphere (possible DST) .addRoundTrip(dateDataType(), LocalDate.of(2017, 1, 1)) // winter on northern hemisphere (possible DST on southern hemisphere) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInJvmZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInSomeZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeBackwardAtMidnightInSomeZone); for (String timeZoneId : ImmutableList.of(UTC_KEY.getId(), jvmZone.getId(), someZone.getId())) { Session session = Session.builder(getQueryRunner().getDefaultSession()) .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(timeZoneId)) .build(); testCases.execute(getQueryRunner(), session, mysqlCreateAndInsert("tpch.test_date")); testCases.execute(getQueryRunner(), session, prestoCreateAsSelect("test_date")); } }