Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

Launcher: lunar_date, no day 0 before a new moon just after local midnight

onedgef8c55ce2e611
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/Lunar.java b/launcher/app/src/main/java/com/gathin/tvhome/Lunar.java
index e926ff8f4..d6bf13a34 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/Lunar.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/Lunar.java
@@ -36,9 +36,11 @@ final class Lunar {
/** Gregorian date to {day, month, year, leap (1 = leap month)} of the lunar calendar at UTC+tz. */
static int[] fromSolar(int dd, int mm, int yy, double tz) {
long dayNumber = jdFromDate(dd, mm, yy);
- int k = (int) Math.floor((dayNumber - 2415021.076998695) / 29.530588853);
- long monthStart = newMoonDay(k + 1, tz);
- if (monthStart > dayNumber) monthStart = newMoonDay(k, tz);
+ int k = (int) Math.floor((dayNumber - 2415021.076998695) / 29.530588853) + 1;
+ long monthStart = newMoonDay(k, tz);
+ // The mean-month estimate can still be a new moon ahead when the real one falls just after local midnight;
+ // one step back was not always enough, and the day before that new moon came out as day 0.
+ while (monthStart > dayNumber) monthStart = newMoonDay(--k, tz);
long a11 = lunarMonth11(yy, tz);
long b11 = a11;
int lunarYear;
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/LunarTest.java b/launcher/app/src/test/java/com/gathin/tvhome/LunarTest.java
index d88390cd5..0b4a11368 100644
--- a/launcher/app/src/test/java/com/gathin/tvhome/LunarTest.java
+++ b/launcher/app/src/test/java/com/gathin/tvhome/LunarTest.java
@@ -1,6 +1,7 @@
package com.gathin.tvhome;
import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertTrue;
import org.junit.Test;
@@ -61,4 +62,24 @@ public class LunarTest {
assertArrayEquals(new int[]{1, 1, 1985, 0}, Lunar.fromSolar(21, 1, 1985, 7));
assertArrayEquals(new int[]{1, 1, 1985, 0}, Lunar.fromSolar(20, 2, 1985, 8));
}
+
+ @Test
+ public void dayBeforeANewMoonJustAfterMidnightIsTheThirtieth() {
+ // The new moon of 26 March 2009 16:06 UTC is 27 March in China; the day before was day 0, not 30/2.
+ assertArrayEquals(new int[]{30, 2, 2009, 0}, Lunar.fromSolar(26, 3, 2009, 8));
+ assertArrayEquals(new int[]{1, 3, 2009, 0}, Lunar.fromSolar(27, 3, 2009, 8));
+ assertArrayEquals(new int[]{30, 3, 2001, 0}, Lunar.fromSolar(23, 4, 2001, 9));
+ assertArrayEquals(new int[]{30, 3, 2054, 0}, Lunar.fromSolar(7, 5, 2054, 7));
+ }
+
+ @Test
+ public void everyDayFrom1900To2099IsDay1To30() {
+ for (int tz = 7; tz <= 9; tz++) {
+ for (long jd = Lunar.jdFromDate(1, 1, 1900); jd < Lunar.jdFromDate(1, 1, 2100); jd++) {
+ java.time.LocalDate d = java.time.LocalDate.ofEpochDay(jd - 2440588);
+ int day = Lunar.fromSolar(d.getDayOfMonth(), d.getMonthValue(), d.getYear(), tz)[0];
+ assertTrue(d + " at UTC+" + tz + " is day " + day, day >= 1 && day <= 30);
+ }
+ }
+ }
}