@Test public void testQuoteReceived_AllPricesInitialized_OrdersNotPlaced_AfterTradeTime() { ZonedDateTime local = ZonedDateTime.of(2015, 6, 15, 0, 0, 0, 0, ZoneId.systemDefault()); ZoneOffset offset = local.getOffset(); ZonedDateTime zdt = ZonedDateTime.of(2015, 6, 15, 21, 50, 0, 0, ZoneId.of("Z")); System.out.println("Offset: " + offset.getTotalSeconds() / 3600); int hour = strategy.timeToPlaceOrders.getHour() + (-offset.getTotalSeconds() / (3600)); zdt = zdt.withHour(hour); zdt = zdt.plusMinutes(1); ILevel1Quote mockQuote = mock(ILevel1Quote.class); strategy.longShortPairMap.put(longTicker, shortTicker); when(mockQuote.getType()).thenReturn(QuoteType.LAST); when(mockQuote.getTicker()).thenReturn(longTicker); when(mockQuote.getValue()).thenReturn(BigDecimal.ONE); when(mockQuote.getTimeStamp()).thenReturn(zdt); doReturn(true).when(strategy).setAllPricesInitialized(); doNothing() .when(strategy) .placeMOCOrders(any(Ticker.class), any(Ticker.class), any(ZonedDateTime.class)); strategy.ordersPlaced = false; strategy.quoteRecieved(mockQuote); assertTrue(strategy.ordersPlaced); verify(strategy).setAllPricesInitialized(); verify(strategy).placeMOCOrders(longTicker, shortTicker, zdt); }
static class MockDynamicProvider extends ZoneRulesProvider { final ZoneRules BASE = ZoneOffset.of("+01:15").getRules(); final ZoneRules ALTERNATE = ZoneOffset.of("+01:30").getRules(); int count = 0; @Override public Set<String> provideZoneIds() { return new HashSet<>(Collections.singleton("DynamicLocation")); } @Override protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) { NavigableMap<String, ZoneRules> result = new TreeMap<>(); result.put("DynamicVersion1", BASE); if (count > 2) { result.put("DynamicVersion2", ALTERNATE); } return result; } @Override protected ZoneRules provideRules(String zoneId, boolean forCaching) { count++; if (zoneId.equals("DynamicLocation")) { return (forCaching ? null : (count > 2 ? ALTERNATE : BASE)); } throw new ZoneRulesException("Invalid"); } }
@Test public void testQuoteReceived_AllPricesInitialized_OrdersNotPlaced_IsBeforeTradeTime() { ZonedDateTime local = ZonedDateTime.of(2015, 6, 15, 0, 0, 0, 0, ZoneId.systemDefault()); ZoneOffset offset = local.getOffset(); ZonedDateTime zdt = ZonedDateTime.of(2015, 6, 15, 20, 30, 0, 0, ZoneId.of("Z")); System.out.println("Offset: " + offset.getTotalSeconds() / 3600); int hour = strategy.timeToPlaceOrders.getHour() + (-offset.getTotalSeconds() / (3600)); System.out.println("Offset hour: " + hour); zdt = zdt.withHour(hour); ILevel1Quote mockQuote = mock(ILevel1Quote.class); Ticker ticker = new StockTicker("ABC"); when(mockQuote.getType()).thenReturn(QuoteType.LAST); when(mockQuote.getTicker()).thenReturn(ticker); when(mockQuote.getValue()).thenReturn(BigDecimal.ONE); when(mockQuote.getTimeStamp()).thenReturn(zdt); doReturn(true).when(strategy).setAllPricesInitialized(); strategy.ordersPlaced = false; strategy.quoteRecieved(mockQuote); assertFalse(strategy.ordersPlaced); verify(strategy).setAllPricesInitialized(); verify(strategy, never()) .placeMOCOrders(any(Ticker.class), any(Ticker.class), any(ZonedDateTime.class)); }
@Override protected Optional<DataPoint<SleepDuration>> asDataPoint( JsonNode listEntryNode, Integer measureUnitMagicNumber) { SleepDuration.Builder sleepDurationBuilder = new SleepDuration.Builder( new DurationUnitValue( DurationUnit.MINUTE, asRequiredBigDecimal(listEntryNode, "HoursSlept"))); Optional<Long> startTime = asOptionalLong(listEntryNode, "StartTime"); Optional<Long> endTime = asOptionalLong(listEntryNode, "EndTime"); if (startTime.isPresent() && endTime.isPresent()) { Optional<String> timeZone = asOptionalString(listEntryNode, "TimeZone"); if (timeZone.isPresent()) { sleepDurationBuilder.setEffectiveTimeFrame( ofStartDateTimeAndEndDateTime( getDateTimeWithCorrectOffset(startTime.get(), ZoneOffset.of(timeZone.get())), getDateTimeWithCorrectOffset(endTime.get(), ZoneOffset.of(timeZone.get())))); } } getUserNoteIfExists(listEntryNode).ifPresent(sleepDurationBuilder::setUserNotes); SleepDuration sleepDuration = sleepDurationBuilder.build(); asOptionalBigDecimal(listEntryNode, "Awaken") .ifPresent(awaken -> sleepDuration.setAdditionalProperty("wakeup_count", awaken)); return Optional.of( new DataPoint<>(createDataPointHeader(listEntryNode, sleepDuration), sleepDuration)); }
/** * Gets the amount of daylight savings in use for the specified instant in this zone. * * <p>This provides access to historic information on how the amount of daylight savings has * changed over time. This is the difference between the standard offset and the actual offset. * Typically the amount is zero during winter and one hour during summer. Time-zones are * second-based, so the nanosecond part of the duration will be zero. * * <p>This default implementation calculates the duration from the {@link * #getOffset(java.time.Instant) actual} and {@link #getStandardOffset(java.time.Instant) * standard} offsets. * * @param instant the instant to find the daylight savings for, not null, but null may be ignored * if the rules have a single offset for all instants * @return the difference between the standard and actual offset, not null */ public Duration getDaylightSavings(Instant instant) { if (savingsInstantTransitions.length == 0) { return Duration.ZERO; } ZoneOffset standardOffset = getStandardOffset(instant); ZoneOffset actualOffset = getOffset(instant); return Duration.ofSeconds(actualOffset.getTotalSeconds() - standardOffset.getTotalSeconds()); }
private Object getOffsetInfo(LocalDateTime dt) { if (savingsInstantTransitions.length == 0) { return standardOffsets[0]; } // check if using last rules if (lastRules.length > 0 && dt.isAfter(savingsLocalTransitions[savingsLocalTransitions.length - 1])) { ZoneOffsetTransition[] transArray = findTransitionArray(dt.getYear()); Object info = null; for (ZoneOffsetTransition trans : transArray) { info = findOffsetInfo(dt, trans); if (info instanceof ZoneOffsetTransition || info.equals(trans.getOffsetBefore())) { return info; } } return info; } // using historic rules int index = Arrays.binarySearch(savingsLocalTransitions, dt); if (index == -1) { // before first transition return wallOffsets[0]; } if (index < 0) { // switch negative insert position to start of matched range index = -index - 2; } else if (index < savingsLocalTransitions.length - 1 && savingsLocalTransitions[index].equals(savingsLocalTransitions[index + 1])) { // handle overlap immediately following gap index++; } if ((index & 1) == 0) { // gap or overlap LocalDateTime dtBefore = savingsLocalTransitions[index]; LocalDateTime dtAfter = savingsLocalTransitions[index + 1]; ZoneOffset offsetBefore = wallOffsets[index / 2]; ZoneOffset offsetAfter = wallOffsets[index / 2 + 1]; if (offsetAfter.getTotalSeconds() > offsetBefore.getTotalSeconds()) { // gap return new ZoneOffsetTransition(dtBefore, offsetBefore, offsetAfter); } else { // overlap return new ZoneOffsetTransition(dtAfter, offsetBefore, offsetAfter); } } else { // normal (neither gap or overlap) return wallOffsets[index / 2 + 1]; } }
public class ApacheCombinedFormatLoggerTest { LogRecordingHandler logRecords = new LogRecordingHandler(); Instant currentTime = LocalDateTime.of(2012, 6, 27, 12, 4, 0).toInstant(ZoneOffset.of("-05:00")); ApacheCombinedFormatLogger apacheCommonLogger = new ApacheCombinedFormatLogger( anonymousLogger(logRecords), Clock.fixed(currentTime, ZoneId.of("GMT+01:00")), Locale.US); Request request = new Request().protocol("HTTP/1.1").remoteIp("192.168.0.1"); Response response = new Response(); @Test public void logsRequestsServedInApacheCombinedLogFormat() throws Exception { request .method(GET) .uri("/products?keyword=dogs") .addHeader(HeaderNames.REFERER, "http://lama/wool") .addHeader(HeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)"); apacheCommonLogger.handle(request, response); response.status(OK).body("a response with a size of 28").done(); response.await(); logRecords.assertEntries( contains( "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"http://lama/wool\" \"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)\"")); } @Test public void logsEmptyStringWhenNoUserAgentInRequest() throws Exception { request .method(GET) .uri("/products?keyword=dogs") .addHeader(HeaderNames.REFERER, "http://lama/wool"); apacheCommonLogger.handle(request, response); response.status(OK).body("a response with a size of 28").done(); response.await(); logRecords.assertEntries( contains( "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"http://lama/wool\" \"\"")); } @Test public void logsEmptyStringWhenNoRefererInRequest() throws Exception { request .method(GET) .uri("/products?keyword=dogs") .addHeader(HeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)"); apacheCommonLogger.handle(request, response); response.status(OK).body("a response with a size of 28").done(); response.await(); logRecords.assertEntries( contains( "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"\" \"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)\"")); } }
// ----------------------------------------------------------------------- // registerProvider() // ----------------------------------------------------------------------- @Test(groups = {"tck"}) public void test_registerProvider() { Set<String> pre = ZoneRulesProvider.getAvailableZoneIds(); assertEquals(pre.contains("FooLocation"), false); ZoneRulesProvider.registerProvider(new MockTempProvider()); assertEquals(pre.contains("FooLocation"), false); Set<String> post = ZoneRulesProvider.getAvailableZoneIds(); assertEquals(post.contains("FooLocation"), true); assertEquals( ZoneRulesProvider.getRules("FooLocation", false), ZoneOffset.of("+01:45").getRules()); }
@DataProvider(name = "parseWithOffsetWithoutZone") Object[][] data_parse_WithOffset_WithoutZone() { return new Object[][] { { "2015-12-14T00:45:00-11:30", OffsetDateTime.of(2015, 12, 14, 0, 45, 0, 0, ZoneOffset.of("-11:30")) }, { "2015-12-14T01:45:00-05:00", OffsetDateTime.of(2015, 12, 14, 1, 45, 0, 0, ZoneOffset.of("-05:00")) }, { "2015-12-14T02:45:00-00:00", OffsetDateTime.of(2015, 12, 14, 2, 45, 0, 0, ZoneOffset.of("-00:00")) }, { "2015-12-14T03:45:00+00:00", OffsetDateTime.of(2015, 12, 14, 3, 45, 0, 0, ZoneOffset.of("+00:00")) }, { "2015-12-14T04:45:00+03:30", OffsetDateTime.of(2015, 12, 14, 4, 45, 0, 0, ZoneOffset.of("+03:30")) }, { "2015-12-14T05:45:00+10:00", OffsetDateTime.of(2015, 12, 14, 5, 45, 0, 0, ZoneOffset.of("+10:00")) } }; }
@Test public void testLocalDateTime() { System.out.println(LocalDateTime.now()); System.out.println(LocalDateTime.of(1994, Month.MAY, 15, 11, 30)); System.out.println(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())); System.out.println(LocalDateTime.ofEpochSecond(1450749600, 0, ZoneOffset.ofHours(8))); System.out.printf("6 months from now: %s%n", LocalDateTime.now().plusMonths(6)); System.out.printf("6 days ago: %s%n", LocalDateTime.now().minusDays(6)); System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now())); System.out.println(DateTimeFormatter.ofPattern("MM-dd HH:mm").format(LocalDateTime.now())); }
@SubscribeMapping("/topic/activity") @SendTo("/topic/tracker") public ActivityDTO sendActivity( @Payload ActivityDTO activityDTO, StompHeaderAccessor stompHeaderAccessor, Principal principal) { activityDTO.setUserLogin(SecurityUtils.getCurrentUserLogin()); activityDTO.setUserLogin(principal.getName()); activityDTO.setSessionId(stompHeaderAccessor.getSessionId()); activityDTO.setIpAddress(stompHeaderAccessor.getSessionAttributes().get(IP_ADDRESS).toString()); Instant instant = Instant.ofEpochMilli(Calendar.getInstance().getTimeInMillis()); activityDTO.setTime( dateTimeFormatter.format(ZonedDateTime.ofInstant(instant, ZoneOffset.systemDefault()))); log.debug("Sending user tracking data {}", activityDTO); return activityDTO; }
static class MockTempProvider extends ZoneRulesProvider { final ZoneRules rules = ZoneOffset.of("+01:45").getRules(); @Override public Set<String> provideZoneIds() { return new HashSet<String>(Collections.singleton("FooLocation")); } @Override protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) { NavigableMap<String, ZoneRules> result = new TreeMap<>(); result.put("BarVersion", rules); return result; } @Override protected ZoneRules provideRules(String zoneId, boolean forCaching) { if (zoneId.equals("FooLocation")) { return rules; } throw new ZoneRulesException("Invalid"); } }
private int findYear(long epochSecond, ZoneOffset offset) { // inline for performance long localSecond = epochSecond + offset.getTotalSeconds(); long localEpochDay = Math.floorDiv(localSecond, 86400); return LocalDate.ofEpochDay(localEpochDay).getYear(); }
@DataProvider(name = "parseWithZoneWithOffset") Object[][] data_parse_WithZone_WithOffset() { return new Object[][] { { "2012-10-28T01:45:00-02:30[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("-02:30"), EUROPE_BERLIN }, { "2012-10-28T01:45:00-01:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("-01:00"), EUROPE_BERLIN }, { "2012-10-28T01:45:00-00:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("-00:00"), EUROPE_BERLIN }, { "2012-10-28T01:45:00+00:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+00:00"), EUROPE_BERLIN }, { "2012-10-28T01:45:00+01:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+01:00"), EUROPE_BERLIN }, { "2012-10-28T01:45:00+02:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+02:00"), EUROPE_BERLIN }, { "2012-10-28T01:45:00+03:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+03:00"), EUROPE_BERLIN }, { "2012-10-28T02:45:00-02:30[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-02:30"), EUROPE_BERLIN }, { "2012-10-28T02:45:00-01:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-01:00"), EUROPE_BERLIN }, { "2012-10-28T02:45:00-00:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-00:00"), EUROPE_BERLIN }, { "2012-10-28T02:45:00+00:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+00:00"), EUROPE_BERLIN }, { "2012-10-28T02:45:00+01:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+01:00"), EUROPE_BERLIN }, { "2012-10-28T02:45:00+02:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+02:00"), EUROPE_BERLIN }, { "2012-10-28T02:45:00+03:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+03:00"), EUROPE_BERLIN }, { "2012-10-28T03:45:00-02:30[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-02:30"), EUROPE_BERLIN }, { "2012-10-28T03:45:00-01:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-01:00"), EUROPE_BERLIN }, { "2012-10-28T03:45:00-00:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-00:00"), EUROPE_BERLIN }, { "2012-10-28T03:45:00+00:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+00:00"), EUROPE_BERLIN }, { "2012-10-28T03:45:00+01:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+01:00"), EUROPE_BERLIN }, { "2012-10-28T03:45:00+02:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+02:00"), EUROPE_BERLIN }, { "2012-10-28T03:45:00+03:00[Europe/Berlin]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+03:00"), EUROPE_BERLIN }, { "2012-10-28T02:45:00-02:30[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-02:30"), ASIA_ISTANBUL }, { "2012-10-28T02:45:00-01:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-01:00"), ASIA_ISTANBUL }, { "2012-10-28T02:45:00-00:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-00:00"), ASIA_ISTANBUL }, { "2012-10-28T02:45:00+00:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+00:00"), ASIA_ISTANBUL }, { "2012-10-28T02:45:00+01:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+01:00"), ASIA_ISTANBUL }, { "2012-10-28T02:45:00+02:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+02:00"), ASIA_ISTANBUL }, { "2012-10-28T02:45:00+03:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+03:00"), ASIA_ISTANBUL }, { "2012-10-28T03:45:00-02:30[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-02:30"), ASIA_ISTANBUL }, { "2012-10-28T03:45:00-01:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-01:00"), ASIA_ISTANBUL }, { "2012-10-28T03:45:00-00:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-00:00"), ASIA_ISTANBUL }, { "2012-10-28T03:45:00+00:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+00:00"), ASIA_ISTANBUL }, { "2012-10-28T03:45:00+01:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+01:00"), ASIA_ISTANBUL }, { "2012-10-28T03:45:00+02:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+02:00"), ASIA_ISTANBUL }, { "2012-10-28T03:45:00+03:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+03:00"), ASIA_ISTANBUL }, { "2012-10-28T04:45:00-02:30[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("-02:30"), ASIA_ISTANBUL }, { "2012-10-28T04:45:00-01:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("-01:00"), ASIA_ISTANBUL }, { "2012-10-28T04:45:00-00:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("-00:00"), ASIA_ISTANBUL }, { "2012-10-28T04:45:00+00:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+00:00"), ASIA_ISTANBUL }, { "2012-10-28T04:45:00+01:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+01:00"), ASIA_ISTANBUL }, { "2012-10-28T04:45:00+02:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+02:00"), ASIA_ISTANBUL }, { "2012-10-28T04:45:00+03:00[Asia/Istanbul]", LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+03:00"), ASIA_ISTANBUL } }; }