improve leap day behavior

This commit is contained in:
Milos Kozak 2020-03-29 23:38:58 +02:00
parent a5a5f048ea
commit f25e871a91
11 changed files with 49 additions and 15 deletions

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.data;
import androidx.collection.LongSparseArray; import androidx.collection.LongSparseArray;
import org.joda.time.DateTime;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -607,13 +608,15 @@ public class Profile {
} }
public static int secondsFromMidnight() { public static int secondsFromMidnight() {
long passed = DateUtil.now() - MidnightTime.calc(); // long passed = DateUtil.now() - MidnightTime.calc();
long passed = new DateTime().getMillisOfDay();
return (int) (passed / 1000); return (int) (passed / 1000);
} }
public static int secondsFromMidnight(long date) { public static int secondsFromMidnight(long date) {
long midnight = MidnightTime.calc(date); //long midnight = MidnightTime.calc(date);
long passed = date - midnight; //long passed = date - midnight;
long passed = new DateTime(date).getMillisOfDay();
return (int) (passed / 1000); return (int) (passed / 1000);
} }

View file

@ -57,7 +57,7 @@ class InputTime(injector: HasAndroidInjector) : Element(injector) {
root.addView(l) root.addView(l)
} }
private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calc() + T.mins(minutesSinceMidnight.toLong()).msecs() private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight)
private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60
} }

View file

@ -80,7 +80,7 @@ class InputTimeRange(injector: HasAndroidInjector) : Element(injector) {
root.addView(l) root.addView(l)
} }
private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calc() + T.mins(minutesSinceMidnight.toLong()).msecs() private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight)
private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60
} }

View file

@ -93,7 +93,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) {
override fun duplicate(): Trigger = TriggerRecurringTime(injector, this) override fun duplicate(): Trigger = TriggerRecurringTime(injector, this)
private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calc() + T.mins(minutesSinceMidnight.toLong()).msecs() private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight)
private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60

View file

@ -76,7 +76,7 @@ class TriggerTimeRange(injector: HasAndroidInjector) : Trigger(injector) {
override fun duplicate(): Trigger = TriggerTimeRange(injector, range.start, range.end) override fun duplicate(): Trigger = TriggerTimeRange(injector, range.start, range.end)
private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calc() + T.mins(minutesSinceMidnight.toLong()).msecs() private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight)
private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60

View file

@ -703,13 +703,11 @@ class SmsCommunicatorPlugin @Inject constructor(
var grams = SafeParse.stringToInt(splitted[1]) var grams = SafeParse.stringToInt(splitted[1])
var time = DateUtil.now() var time = DateUtil.now()
if (splitted.size > 2) { if (splitted.size > 2) {
val seconds = DateUtil.toSeconds(splitted[2].toUpperCase(Locale.getDefault())) time = DateUtil.toTodayTime(splitted[2].toUpperCase(Locale.getDefault()))
val midnight = MidnightTime.calc() if (time == 0L) {
if (seconds == 0 && (!splitted[2].startsWith("00:00") || !splitted[2].startsWith("12:00"))) {
sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat)))
return return
} }
time = midnight + T.secs(seconds.toLong()).msecs()
} }
grams = constraintChecker.applyCarbsConstraints(Constraint(grams)).value() grams = constraintChecker.applyCarbsConstraints(Constraint(grams)).value()
if (grams == 0) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) if (grams == 0) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat)))

View file

@ -108,6 +108,28 @@ public class DateUtil {
return retval; return retval;
} }
public static long toTodayTime(String hh_colon_mm) {
Pattern p = Pattern.compile("(\\d+):(\\d+)( a.m.| p.m.| AM| PM|AM|PM|)");
Matcher m = p.matcher(hh_colon_mm);
long retval = 0;
if (m.find()) {
int hours = SafeParse.stringToInt(m.group(1));
int minutes = SafeParse.stringToInt(m.group(2));
if ((m.group(3).equals(" a.m.") || m.group(3).equals(" AM") || m.group(3).equals("AM")) && m.group(1).equals("12"))
hours -= 12;
if ((m.group(3).equals(" p.m.") || m.group(3).equals(" PM") || m.group(3).equals("PM")) && !(m.group(1).equals("12")))
hours += 12;
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hours);
c.set(Calendar.MINUTE, minutes);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
retval = c.getTimeInMillis();
}
return retval;
}
public static String dateString(Date date) { public static String dateString(Date date) {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
return df.format(date); return df.format(date);

View file

@ -21,6 +21,17 @@ public class MidnightTime {
return c.getTimeInMillis(); return c.getTimeInMillis();
} }
public static long calcPlusMinutes(int minutes) {
int h = minutes / 60;
int m = minutes % 60;
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, h);
c.set(Calendar.MINUTE, m);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTimeInMillis();
}
public static long calc(long time) { public static long calc(long time) {
Long m; Long m;
synchronized (times) { synchronized (times) {

View file

@ -40,7 +40,7 @@ class TddCalculator @Inject constructor(
fun calculate(days: Long): LongSparseArray<TDD> { fun calculate(days: Long): LongSparseArray<TDD> {
val range = T.days(days + 1).msecs() val range = T.days(days + 1).msecs()
val startTime = MidnightTime.calc(DateUtil.now()) - T.days(days).msecs() val startTime = MidnightTime.calc(DateUtil.now() - T.days(days).msecs())
val endTime = MidnightTime.calc(DateUtil.now()) val endTime = MidnightTime.calc(DateUtil.now())
initializeData(range) initializeData(range)

View file

@ -23,7 +23,7 @@ class TirCalculator @Inject constructor(
fun calculate(days: Long, lowMgdl: Double, highMgdl: Double): LongSparseArray<TIR> { fun calculate(days: Long, lowMgdl: Double, highMgdl: Double): LongSparseArray<TIR> {
if (lowMgdl < 39) throw RuntimeException("Low below 39") if (lowMgdl < 39) throw RuntimeException("Low below 39")
if (lowMgdl > highMgdl) throw RuntimeException("Low > High") if (lowMgdl > highMgdl) throw RuntimeException("Low > High")
val startTime = MidnightTime.calc(DateUtil.now()) - T.days(days).msecs() val startTime = MidnightTime.calc(DateUtil.now() - T.days(days).msecs())
val endTime = MidnightTime.calc(DateUtil.now()) val endTime = MidnightTime.calc(DateUtil.now())
val bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(startTime, endTime, true) val bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(startTime, endTime, true)

View file

@ -22,9 +22,9 @@ class TriggerTimeRangeTest : TriggerTestBase() {
@Before @Before
fun mock() { fun mock() {
var realNow = System.currentTimeMillis()
PowerMockito.mockStatic(DateUtil::class.java) PowerMockito.mockStatic(DateUtil::class.java)
PowerMockito.`when`(DateUtil.now()).thenReturn(now.toLong() * 60000 + MidnightTime.calc(realNow)) val nowMills = MidnightTime.calcPlusMinutes(now)
PowerMockito.`when`(DateUtil.now()).thenReturn(nowMills)
} }
@Test @Test