private long getPreviousPeriod(Tick tick, int indexOfPreviousTick) { switch (timeLevel) { case DAY: // return previous day int prevCalendarDay = tick.getEndTime().minusDays(1).getDayOfYear(); // skip weekend and holidays: while (getTimeSeries().getTick(indexOfPreviousTick).getEndTime().getDayOfYear() != prevCalendarDay && indexOfPreviousTick > 0) { prevCalendarDay--; } return prevCalendarDay; case WEEK: // return previous week return tick.getEndTime().minusWeeks(1).get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); case MONTH: // return previous month return tick.getEndTime().minusMonths(1).getMonthValue(); default: // return previous year return tick.getEndTime().minusYears(1).getYear(); } }
@Test(dataProvider = "parseLenientQuarter") public void test_parse_parseLenientQuarter_SMART(String str, LocalDate expected, boolean smart) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral(':') .appendValue(IsoFields.DAY_OF_QUARTER) .toFormatter().withResolverStyle(ResolverStyle.SMART); if (smart) { LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, expected); } else { try { LocalDate.parse(str, f); fail("Should have failed"); } catch (DateTimeParseException ex) { // expected } } }
@Test(dataProvider = "parseLenientWeek") public void test_parse_parseLenientWeek_SMART(String str, LocalDate expected, boolean smart) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':') .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':') .appendValue(DAY_OF_WEEK) .toFormatter().withResolverStyle(ResolverStyle.SMART); if (smart) { LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, expected); } else { try { LocalDate.parse(str, f); fail("Should have failed"); } catch (DateTimeParseException ex) { // expected } } }
@DataProvider(name="resolveTwoNoChange") Object[][] data_resolveTwoNoChange() { return new Object[][]{ {YEAR, 2012, MONTH_OF_YEAR, 5}, {YEAR, 2012, DAY_OF_MONTH, 5}, {YEAR, 2012, DAY_OF_WEEK, 5}, {YEAR, 2012, ALIGNED_WEEK_OF_YEAR, 5}, {YEAR, 2012, ALIGNED_WEEK_OF_MONTH, 5}, {YEAR, 2012, IsoFields.QUARTER_OF_YEAR, 3}, {YEAR, 2012, MINUTE_OF_HOUR, 5}, {YEAR, 2012, SECOND_OF_MINUTE, 5}, {YEAR, 2012, NANO_OF_SECOND, 5}, {MONTH_OF_YEAR, 5, DAY_OF_MONTH, 5}, {MONTH_OF_YEAR, 5, DAY_OF_WEEK, 5}, {MONTH_OF_YEAR, 5, ALIGNED_WEEK_OF_YEAR, 5}, {MONTH_OF_YEAR, 5, ALIGNED_WEEK_OF_MONTH, 5}, {MONTH_OF_YEAR, 3, IsoFields.QUARTER_OF_YEAR, 5}, {MONTH_OF_YEAR, 5, MINUTE_OF_HOUR, 5}, {MONTH_OF_YEAR, 5, SECOND_OF_MINUTE, 5}, {MONTH_OF_YEAR, 5, NANO_OF_SECOND, 5}, }; }
private long getPeriod(Tick tick) { switch (timeLevel) { case DAY: // return previous day return tick.getEndTime().getDayOfYear(); case WEEK: // return previous week return tick.getEndTime().get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); case MONTH: // return previous month return tick.getEndTime().getMonthValue(); default: // return previous year return tick.getEndTime().getYear(); } }
@Test(dataProvider = "quarter") public void test_parse_quarters(LocalDate date, int doq, int qoy) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral('-') .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral('-') .appendValue(IsoFields.DAY_OF_QUARTER) .toFormatter().withResolverStyle(ResolverStyle.STRICT); LocalDate parsed = LocalDate.parse(date.getYear() + "-" + qoy + "-" + doq, f); assertEquals(parsed, date); }
@Test(dataProvider = "quarter") public void test_parse_quarters_SMART(LocalDate date, int doq, int qoy) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral('-') .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral('-') .appendValue(IsoFields.DAY_OF_QUARTER) .toFormatter().withResolverStyle(ResolverStyle.SMART); LocalDate parsed = LocalDate.parse(date.getYear() + "-" + qoy + "-" + doq, f); assertEquals(parsed, date); }
@Test(dataProvider = "quarter") public void test_parse_quarters_LENIENT(LocalDate date, int doq, int qoy) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral('-') .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral('-') .appendValue(IsoFields.DAY_OF_QUARTER) .toFormatter().withResolverStyle(ResolverStyle.LENIENT); LocalDate parsed = LocalDate.parse(date.getYear() + "-" + qoy + "-" + doq, f); assertEquals(parsed, date); }
@Test(dataProvider = "parseLenientQuarter", expectedExceptions = DateTimeParseException.class) public void test_parse_parseLenientQuarter_STRICT(String str, LocalDate expected, boolean smart) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral(':') .appendValue(IsoFields.DAY_OF_QUARTER) .toFormatter().withResolverStyle(ResolverStyle.STRICT); LocalDate.parse(str, f); }
@Test(dataProvider = "parseLenientQuarter") public void test_parse_parseLenientQuarter_LENIENT(String str, LocalDate expected, boolean smart) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(YEAR).appendLiteral(':') .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral(':') .appendValue(IsoFields.DAY_OF_QUARTER) .toFormatter().withResolverStyle(ResolverStyle.LENIENT); LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, expected); }
@Test(dataProvider="week") public void test_parse_weeks_STRICT(LocalDate date, DayOfWeek dow, int week, int wby) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral('-') .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral('-') .appendValue(DAY_OF_WEEK) .toFormatter().withResolverStyle(ResolverStyle.STRICT); LocalDate parsed = LocalDate.parse(wby + "-" + week + "-" + dow.getValue(), f); assertEquals(parsed, date); }
@Test(dataProvider="week") public void test_parse_weeks_SMART(LocalDate date, DayOfWeek dow, int week, int wby) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral('-') .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral('-') .appendValue(DAY_OF_WEEK) .toFormatter().withResolverStyle(ResolverStyle.SMART); LocalDate parsed = LocalDate.parse(wby + "-" + week + "-" + dow.getValue(), f); assertEquals(parsed, date); }
@Test(dataProvider="week") public void test_parse_weeks_LENIENT(LocalDate date, DayOfWeek dow, int week, int wby) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral('-') .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral('-') .appendValue(DAY_OF_WEEK) .toFormatter().withResolverStyle(ResolverStyle.LENIENT); LocalDate parsed = LocalDate.parse(wby + "-" + week + "-" + dow.getValue(), f); assertEquals(parsed, date); }
@Test(dataProvider = "parseLenientWeek", expectedExceptions = DateTimeParseException.class) public void test_parse_parseLenientWeek_STRICT(String str, LocalDate expected, boolean smart) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':') .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':') .appendValue(DAY_OF_WEEK) .toFormatter().withResolverStyle(ResolverStyle.STRICT); LocalDate.parse(str, f); }
@Test(dataProvider = "parseLenientWeek") public void test_parse_parseLenientWeek_LENIENT(String str, LocalDate expected, boolean smart) { DateTimeFormatter f = new DateTimeFormatterBuilder() .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':') .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':') .appendValue(DAY_OF_WEEK) .toFormatter().withResolverStyle(ResolverStyle.LENIENT); LocalDate parsed = LocalDate.parse(str, f); assertEquals(parsed, expected); }
public void test_loop() { // loop round at least one 400 year cycle, including before 1970 LocalDate date = LocalDate.of(1960, 1, 5); // Tuseday of week 1 1960 int year = 1960; int wby = 1960; int weekLen = 52; int week = 1; while (date.getYear() < 2400) { DayOfWeek loopDow = date.getDayOfWeek(); if (date.getYear() != year) { year = date.getYear(); } if (loopDow == MONDAY) { week++; if ((week == 53 && weekLen == 52) || week == 54) { week = 1; LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1); DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek(); weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52); wby++; } } assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek()); assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week, "Failed on " + date + " " + date.getDayOfWeek()); assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek()); assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby, "Failed on " + date + " " + date.getDayOfWeek()); assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek()); date = date.plusDays(1); } }
@Test public void test_parse_weekDate_largeYear() { TemporalAccessor parsed = DateTimeFormatter.ISO_WEEK_DATE.parseUnresolved("+123456-W04-5", new ParsePosition(0)); assertEquals(parsed.getLong(IsoFields.WEEK_BASED_YEAR), 123456L); assertEquals(parsed.getLong(IsoFields.WEEK_OF_WEEK_BASED_YEAR), 4L); assertEquals(parsed.getLong(DAY_OF_WEEK), 5L); }
void setFields(LocalDate dt) { if (dt != null) { fields.put(YEAR, (long) dt.getYear()); fields.put(MONTH_OF_YEAR, (long) dt.getMonthValue()); fields.put(DAY_OF_MONTH, (long) dt.getDayOfMonth()); fields.put(DAY_OF_YEAR, (long) dt.getDayOfYear()); fields.put(DAY_OF_WEEK, (long) dt.getDayOfWeek().getValue()); fields.put(IsoFields.WEEK_BASED_YEAR, dt.getLong(IsoFields.WEEK_BASED_YEAR)); fields.put(IsoFields.WEEK_OF_WEEK_BASED_YEAR, dt.getLong(IsoFields.WEEK_OF_WEEK_BASED_YEAR)); } }
void setFields(LocalDateTime dt) { if (dt != null) { fields.put(YEAR, (long) dt.getYear()); fields.put(MONTH_OF_YEAR, (long) dt.getMonthValue()); fields.put(DAY_OF_MONTH, (long) dt.getDayOfMonth()); fields.put(DAY_OF_YEAR, (long) dt.getDayOfYear()); fields.put(DAY_OF_WEEK, (long) dt.getDayOfWeek().getValue()); fields.put(IsoFields.WEEK_BASED_YEAR, dt.getLong(IsoFields.WEEK_BASED_YEAR)); fields.put(IsoFields.WEEK_OF_WEEK_BASED_YEAR, dt.getLong(IsoFields.WEEK_OF_WEEK_BASED_YEAR)); fields.put(HOUR_OF_DAY, (long) dt.getHour()); fields.put(MINUTE_OF_HOUR, (long) dt.getMinute()); fields.put(SECOND_OF_MINUTE, (long) dt.getSecond()); fields.put(NANO_OF_SECOND, (long) dt.getNano()); } }
@DataProvider(name="resolveThreeNoChange") Object[][] data_resolveThreeNoChange() { return new Object[][]{ {YEAR, 2012, MONTH_OF_YEAR, 5, DAY_OF_WEEK, 5}, {YEAR, 2012, ALIGNED_WEEK_OF_YEAR, 5, DAY_OF_MONTH, 5}, {YEAR, 2012, ALIGNED_WEEK_OF_MONTH, 5, DAY_OF_MONTH, 5}, {YEAR, 2012, MONTH_OF_YEAR, 5, DAY_OF_WEEK, 5}, {ERA, 1, MONTH_OF_YEAR, 5, DAY_OF_MONTH, 5}, {MONTH_OF_YEAR, 1, DAY_OF_MONTH, 5, IsoFields.QUARTER_OF_YEAR, 3}, {HOUR_OF_DAY, 1, SECOND_OF_MINUTE, 5, NANO_OF_SECOND, 5}, {MINUTE_OF_HOUR, 1, SECOND_OF_MINUTE, 5, NANO_OF_SECOND, 5}, }; }
@DataProvider(name = "fields") Object[][] data_Fields() { return new Object[][] { {IsoFields.WEEK_OF_WEEK_BASED_YEAR, IsoFields.WEEK_BASED_YEAR}, {WeekFields.ISO.weekOfWeekBasedYear(), WeekFields.ISO.weekBasedYear()}, }; }
@Test(dataProvider = "fields") public void test_WOWBY_basics(TemporalField weekField, TemporalField yearField) { assertEquals(weekField.isDateBased(), true); assertEquals(weekField.isTimeBased(), false); assertEquals(weekField.getBaseUnit(), ChronoUnit.WEEKS); assertEquals(weekField.getRangeUnit(), IsoFields.WEEK_BASED_YEARS); }
@Test(dataProvider = "fields") public void test_WBY_basics(TemporalField weekField, TemporalField yearField) { assertEquals(yearField.isDateBased(), true); assertEquals(yearField.isTimeBased(), false); assertEquals(yearField.getBaseUnit(), IsoFields.WEEK_BASED_YEARS); assertEquals(yearField.getRangeUnit(), ChronoUnit.FOREVER); }
@Test(dataProvider = "fields") public void test_addTo_weekBasedYears(TemporalField weekField, TemporalField yearField) { // tests every day from 2012 to 2016 inclusive LocalDate date = LocalDate.of(2012, 1, 2); int wby = 2012; int week = 1; int dow = 1; for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) { for (int j = -5; j <= 5; j++) { LocalDate adjusted = IsoFields.WEEK_BASED_YEARS.addTo(date, j); assertEquals(adjusted.get(yearField), wby + j); assertEquals(adjusted.get(DAY_OF_WEEK), dow); assertEquals(adjusted.get(weekField), (week == 53 && wbyLen(wby + j) == 52 ? 52 : week), "" + date + " " + adjusted); } if (dow == 7) { dow = 1; week++; } else { dow++; } if (week > wbyLen(wby)) { week = 1; wby++; } date = date.plusDays(1); } }