提交 f011b5bc authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add DateTimeUtils.dateValueFromDenormalizedDate()

上级 e080ea15
...@@ -1033,6 +1033,36 @@ public class DateTimeUtils { ...@@ -1033,6 +1033,36 @@ public class DateTimeUtils {
return (year << SHIFT_YEAR) | (month << SHIFT_MONTH) | day; return (year << SHIFT_YEAR) | (month << SHIFT_MONTH) | day;
} }
/**
* Get the date value from a given denormalized date.
*
* @param year
* the year
* @param month
* the month, if out of range month and year will be normalized
* @param day
* the day of the month, if out of range it will be saturated
* @return the date value
*/
public static long dateValueFromDenormalizedDate(long year, long month, int day) {
long mm1 = month - 1;
long yd = mm1 / 12;
if (mm1 < 0 && yd * 12 != mm1) {
yd--;
}
int y = (int) (year + yd);
int m = (int) (month - yd * 12);
if (day < 1) {
day = 1;
} else {
int max = getDaysInMonth(y, m);
if (day > max) {
day = max;
}
}
return dateValue(y, m, day);
}
/** /**
* Convert a UTC datetime in millis to an encoded date in the default * Convert a UTC datetime in millis to an encoded date in the default
* timezone. * timezone.
......
...@@ -11,6 +11,8 @@ import java.util.GregorianCalendar; ...@@ -11,6 +11,8 @@ import java.util.GregorianCalendar;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import static org.h2.util.DateTimeUtils.dateValue;
/** /**
* Unit tests for the DateTimeUtils class * Unit tests for the DateTimeUtils class
*/ */
...@@ -30,6 +32,7 @@ public class TestDateTimeUtils extends TestBase { ...@@ -30,6 +32,7 @@ public class TestDateTimeUtils extends TestBase {
testParseTimeNanosDB2Format(); testParseTimeNanosDB2Format();
testDayOfWeek(); testDayOfWeek();
testWeekOfYear(); testWeekOfYear();
testDateValueFromDenormalizedDate();
} }
private void testParseTimeNanosDB2Format() { private void testParseTimeNanosDB2Format() {
...@@ -53,7 +56,7 @@ public class TestDateTimeUtils extends TestBase { ...@@ -53,7 +56,7 @@ public class TestDateTimeUtils extends TestBase {
if (gc.get(Calendar.ERA) == GregorianCalendar.BC) { if (gc.get(Calendar.ERA) == GregorianCalendar.BC) {
year = 1 - year; year = 1 - year;
} }
long expectedDateValue = DateTimeUtils.dateValue(year, gc.get(Calendar.MONTH) + 1, long expectedDateValue = dateValue(year, gc.get(Calendar.MONTH) + 1,
gc.get(Calendar.DAY_OF_MONTH)); gc.get(Calendar.DAY_OF_MONTH));
long dateValue = DateTimeUtils.dateValueFromAbsoluteDay(i); long dateValue = DateTimeUtils.dateValueFromAbsoluteDay(i);
assertEquals(expectedDateValue, dateValue); assertEquals(expectedDateValue, dateValue);
...@@ -90,4 +93,15 @@ public class TestDateTimeUtils extends TestBase { ...@@ -90,4 +93,15 @@ public class TestDateTimeUtils extends TestBase {
} }
} }
/**
* Test for {@link DateTimeUtils#dateValueFromDenormalizedDate(long, long, int)}.
*/
private void testDateValueFromDenormalizedDate() {
assertEquals(dateValue(2017, 1, 1), DateTimeUtils.dateValueFromDenormalizedDate(2018, -11, 0));
assertEquals(dateValue(2001, 2, 28), DateTimeUtils.dateValueFromDenormalizedDate(2000, 14, 29));
assertEquals(dateValue(1999, 8, 1), DateTimeUtils.dateValueFromDenormalizedDate(2000, -4, -100));
assertEquals(dateValue(2100, 12, 31), DateTimeUtils.dateValueFromDenormalizedDate(2100, 12, 2000));
assertEquals(dateValue(-100, 2, 29), DateTimeUtils.dateValueFromDenormalizedDate(-100, 2, 30));
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论