public static int plusYears(int yearsToAdd, int packedDate) { if (yearsToAdd == 0) { return packedDate; } byte d = getDayOfMonth(packedDate); byte m = getMonthValue(packedDate); short y = getYear(packedDate); int newYear = YEAR.checkValidIntValue(y + yearsToAdd); return resolvePreviousValid(newYear, m, d); }
public static int of(int hour, int minute, int second, int millis) { ChronoField.HOUR_OF_DAY.checkValidValue(hour); ChronoField.MINUTE_OF_HOUR.checkValidValue(minute); ChronoField.SECOND_OF_MINUTE.checkValidValue(second); ChronoField.MILLI_OF_SECOND.checkValidValue(millis); return create(hour, minute, second, millis); }
private static TemporalQuery<Consumer<FieldResult>> temporalQuery(final ChronoField field) { return temporal -> result -> { if (field.isSupportedBy(temporal)) { String key = field.getBaseUnit().toString().toLowerCase(); long value = field.getFrom(temporal); switch (field) { case YEAR: result.years = value;break; case MONTH_OF_YEAR: result.months = value;break; case DAY_OF_WEEK: result.weekdays = value; key = "weekdays"; break; case DAY_OF_MONTH: result.days = value;break; case HOUR_OF_DAY: result.hours = value;break; case MINUTE_OF_HOUR: result.minutes = value;break; case SECOND_OF_MINUTE: result.seconds = value;break; } result.value.put(key, value); } }; } }
private static int ofEpochDay(long epochDay) { EPOCH_DAY.checkValidValue(epochDay); long zeroDay = epochDay + DAYS_0000_TO_1970; // find the march-based year zeroDay -= 60; // adjust to 0000-03-01 so leap day is at end of four year cycle long adjust = 0; if (zeroDay < 0) { // adjust negative years to positive for calculation long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1; adjust = adjustCycles * 400; zeroDay += -adjustCycles * DAYS_PER_CYCLE; } long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE; long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400); if (doyEst < 0) { // fix estimate yearEst--; doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400); } yearEst += adjust; // reset any negative year int marchDoy0 = (int) doyEst; // convert march-based values back to january-based int marchMonth0 = (marchDoy0 * 5 + 2) / 153; int month = (marchMonth0 + 2) % 12 + 1; int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1; yearEst += marchMonth0 / 10; // check year now we are certain it is correct int year = YEAR.checkValidIntValue(yearEst); return pack((short) year, (byte) month, (byte) dom); }
/** * @param temporal temporal to check * @return true if temporal is the last day in the month */ private static boolean isLastDayInMonth(Temporal temporal) { final int day = temporal.get(ChronoField.DAY_OF_MONTH); final int max = (int) ChronoField.DAY_OF_MONTH.rangeRefinedBy(temporal).getMaximum(); return day == max; }
@Nullable Temporal isOk(Temporal t) { if (checker.matches(t)) { return null; } Temporal out = t.plus(1, type.getBaseUnit()); // Fall-through switch case. for example if type is year all cases below must also be handled. switch (type) { case YEAR: out = out.with(ChronoField.MONTH_OF_YEAR, 1); case MONTH_OF_YEAR: out = out.with(ChronoField.DAY_OF_MONTH, 1); case DAY_OF_WEEK: case DAY_OF_MONTH: out = out.with(ChronoField.HOUR_OF_DAY, 0); case HOUR_OF_DAY: out = out.with(ChronoField.MINUTE_OF_HOUR, 0); case MINUTE_OF_HOUR: out = out.with(ChronoField.SECOND_OF_MINUTE, 0); case SECOND_OF_MINUTE: return out; default: throw new IllegalArgumentException("Invalid field type " + type); } }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP) public static long castToTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long value) { if (session.isLegacyTimestamp()) { return unpackMillisUtc(value); } else { // This is hack that we need to use as the timezone interpretation depends on date (not only on time) // TODO remove REFERENCE_TIMESTAMP_UTC when removing support for political time zones in TIME WIT TIME ZONE long currentMillisOfDay = ChronoField.MILLI_OF_DAY.getFrom(Instant.ofEpochMilli(REFERENCE_TIMESTAMP_UTC).atZone(ZoneOffset.UTC)); long timeMillisUtcInCurrentDay = REFERENCE_TIMESTAMP_UTC - currentMillisOfDay + unpackMillisUtc(value); ISOChronology chronology = getChronology(unpackZoneKey(value)); return unpackMillisUtc(value) + chronology.getZone().getOffset(timeMillisUtcInCurrentDay); } }
private static boolean isDefinitelyInvalidChronoField( MethodInvocationTree tree, Iterable<ChronoField> invalidChronoFields) { Optional<String> constant = getEnumName(Iterables.getOnlyElement(tree.getArguments())); if (constant.isPresent()) { for (ChronoField invalidChronoField : invalidChronoFields) { if (constant.get().equals(invalidChronoField.name())) { return true; } } } return false; }
if (!ChronoField.DAY_OF_MONTH.isSupportedBy(temporal)) { return MACHINE_FORMAT_TIME_FORMAT.format(temporal); } else {
@Override public String getDisplayName(Locale locale) { Jdk8Methods.requireNonNull(locale, "locale"); return toString(); }
@Override public ValueRange rangeRefinedBy(TemporalAccessor temporal) { return YEAR.range(); } @Override
private static int ofEpochDay(long epochDay) { EPOCH_DAY.checkValidValue(epochDay); long zeroDay = epochDay + DAYS_0000_TO_1970; // find the march-based year zeroDay -= 60; // adjust to 0000-03-01 so leap day is at end of four year cycle long adjust = 0; if (zeroDay < 0) { // adjust negative years to positive for calculation long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1; adjust = adjustCycles * 400; zeroDay += -adjustCycles * DAYS_PER_CYCLE; } long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE; long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400); if (doyEst < 0) { // fix estimate yearEst--; doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400); } yearEst += adjust; // reset any negative year int marchDoy0 = (int) doyEst; // convert march-based values back to january-based int marchMonth0 = (marchDoy0 * 5 + 2) / 153; int month = (marchMonth0 + 2) % 12 + 1; int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1; yearEst += marchMonth0 / 10; // check year now we are certain it is correct int year = YEAR.checkValidIntValue(yearEst); return pack((short) year, (byte) month, (byte) dom); }
/** * @param temporal temporal to check * @return true if temporal is the last week day in this month. I.e. the last Saturday */ private static boolean isLastOfThisWeekDayInMonth(Temporal temporal) { final int day = temporal.get(ChronoField.DAY_OF_MONTH); final int max = (int) ChronoField.DAY_OF_MONTH.rangeRefinedBy(temporal).getMaximum(); return day + 7 > max; }
dateTime = dateTime.minus(1, ChronoField.MILLI_OF_SECOND.getBaseUnit());
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP) public static long castToTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long value) { if (session.isLegacyTimestamp()) { return unpackMillisUtc(value); } else { // This is hack that we need to use as the timezone interpretation depends on date (not only on time) // TODO remove REFERENCE_TIMESTAMP_UTC when removing support for political time zones in TIME WIT TIME ZONE long currentMillisOfDay = ChronoField.MILLI_OF_DAY.getFrom(Instant.ofEpochMilli(REFERENCE_TIMESTAMP_UTC).atZone(ZoneOffset.UTC)); long timeMillisUtcInCurrentDay = REFERENCE_TIMESTAMP_UTC - currentMillisOfDay + unpackMillisUtc(value); ISOChronology chronology = getChronology(unpackZoneKey(value)); return unpackMillisUtc(value) + chronology.getZone().getOffset(timeMillisUtcInCurrentDay); } }
protected static CodegenExpression codegenLDTZDTMinMax(CodegenExpression val, boolean max, CalendarFieldEnum fieldName) { CodegenExpression chronoField = enumValue(ChronoField.class, fieldName.getChronoField().name()); CodegenExpression valueRange = exprDotMethod(val, "range", chronoField); return exprDotMethod(val, "with", chronoField, exprDotMethod(valueRange, max ? "getMaximum" : "getMinimum")); } }
private static String formatSpokenDateWithTime(Temporal date) { // We have a time, perform some nice formatting... LocalDateTime givenDateTime = LocalDateTime.from(date); if (givenDateTime.isAfter(LocalDateTime.now())) { return formatSpokenFutureDateWithTime(date, givenDateTime); } if (givenDateTime.isAfter(LocalDateTime.now().minusHours(12))) { return formatSpokenRecentDateWithTime(givenDateTime); } if (!ChronoField.DAY_OF_MONTH.isSupportedBy(date)) { // We don't have a date and the time difference is quite big -> simply format the time... return getTimeFormat(getCurrentLang()).format(date); } return formatSpokenDate(date); }
private int[] parseRange(String range, int min, int max, ChronoField cf, String[] names) { int[] r = new int[2]; r[0] = 0; r[1] = max; if ("*".equals(range)) return r; String parts[] = range.split("-"); r[0] = r[1] = parseInt(parts[0], min, max, names); if (parts.length == 2) r[1] = parseInt(parts[1], min, max, names); if (r[0] < min) throw new IllegalArgumentException("Value too small: " + r[0] + " for " + cf.toString()); if (r[1] > max) throw new IllegalArgumentException("Value too high: " + r[1] + " for " + cf.toString()); return r; }
public static int of(int hour, int minute, int second) { ChronoField.HOUR_OF_DAY.checkValidValue(hour); ChronoField.MINUTE_OF_HOUR.checkValidValue(minute); ChronoField.SECOND_OF_MINUTE.checkValidValue(second); return create(hour, minute, second, 0); }