diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java index bf570f08a7..b6f22aae07 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Profile.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.data; import androidx.collection.LongSparseArray; +import org.joda.time.DateTime; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -607,13 +608,15 @@ public class Profile { } 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); } public static int secondsFromMidnight(long date) { - long midnight = MidnightTime.calc(date); - long passed = date - midnight; + //long midnight = MidnightTime.calc(date); + //long passed = date - midnight; + long passed = new DateTime(date).getMillisOfDay(); return (int) (passed / 1000); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt index 34bc74560c..01a7854e35 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt @@ -57,7 +57,7 @@ class InputTime(injector: HasAndroidInjector) : Element(injector) { 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 } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt index 18cf98fc73..4775990d16 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt @@ -80,7 +80,7 @@ class InputTimeRange(injector: HasAndroidInjector) : Element(injector) { 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 } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt index 11dd1e0475..8982a12669 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt @@ -93,7 +93,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) { 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 diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt index 46edd9a0b2..2c565fa733 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt @@ -76,7 +76,7 @@ class TriggerTimeRange(injector: HasAndroidInjector) : Trigger(injector) { 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 diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index 78a0bf0ffc..350e5f08db 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -703,13 +703,11 @@ class SmsCommunicatorPlugin @Inject constructor( var grams = SafeParse.stringToInt(splitted[1]) var time = DateUtil.now() if (splitted.size > 2) { - val seconds = DateUtil.toSeconds(splitted[2].toUpperCase(Locale.getDefault())) - val midnight = MidnightTime.calc() - if (seconds == 0 && (!splitted[2].startsWith("00:00") || !splitted[2].startsWith("12:00"))) { + time = DateUtil.toTodayTime(splitted[2].toUpperCase(Locale.getDefault())) + if (time == 0L) { sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) return } - time = midnight + T.secs(seconds.toLong()).msecs() } grams = constraintChecker.applyCarbsConstraints(Constraint(grams)).value() if (grams == 0) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java b/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java index 7d2790990f..82fabc52b7 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java @@ -108,6 +108,28 @@ public class DateUtil { 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) { DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); return df.format(date); diff --git a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java index 235bb9c6e9..501f6147e7 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java @@ -21,6 +21,17 @@ public class MidnightTime { 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) { Long m; synchronized (times) { diff --git a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt index 0444513952..247d4c22d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt @@ -40,7 +40,7 @@ class TddCalculator @Inject constructor( fun calculate(days: Long): LongSparseArray { 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()) initializeData(range) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/stats/TirCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/stats/TirCalculator.kt index b8d29af3d5..f1f13cbf0d 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/stats/TirCalculator.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/stats/TirCalculator.kt @@ -23,7 +23,7 @@ class TirCalculator @Inject constructor( fun calculate(days: Long, lowMgdl: Double, highMgdl: Double): LongSparseArray { if (lowMgdl < 39) throw RuntimeException("Low below 39") 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 bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(startTime, endTime, true) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt index fc1dbbf690..dba3ddce80 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt @@ -22,9 +22,9 @@ class TriggerTimeRangeTest : TriggerTestBase() { @Before fun mock() { - var realNow = System.currentTimeMillis() 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