mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-28 10:40:47 +02:00
Move daysBetween to a separate class and add tests
Also change the behaviour to the previous one were the time is ignored and only the date is used to calculate number of days between two dates.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/* Copyright (C) 2017 Erik Johansson <erik@ejohansson.se>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.health.openscale.core.utils;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
public final class DateTimeHelpers {
|
||||
static public int daysBetween(Calendar start, Calendar end) {
|
||||
if (start.after(end)) {
|
||||
return -daysBetween(end, start);
|
||||
}
|
||||
|
||||
int days = 0;
|
||||
|
||||
Calendar current = (Calendar)start.clone();
|
||||
while (current.get(Calendar.YEAR) < end.get(Calendar.YEAR)) {
|
||||
final int daysInYear =
|
||||
current.getActualMaximum(Calendar.DAY_OF_YEAR)
|
||||
- current.get(Calendar.DAY_OF_YEAR) + 1;
|
||||
days += daysInYear;
|
||||
current.add(Calendar.DAY_OF_YEAR, daysInYear);
|
||||
}
|
||||
|
||||
days += end.get(Calendar.DAY_OF_YEAR) - current.get(Calendar.DAY_OF_YEAR);
|
||||
|
||||
return days;
|
||||
}
|
||||
}
|
@@ -37,6 +37,7 @@ import com.health.openscale.R;
|
||||
import com.health.openscale.core.OpenScale;
|
||||
import com.health.openscale.core.datatypes.ScaleData;
|
||||
import com.health.openscale.core.datatypes.ScaleUser;
|
||||
import com.health.openscale.core.utils.DateTimeHelpers;
|
||||
import com.health.openscale.gui.activities.DataEntryActivity;
|
||||
import com.health.openscale.gui.views.BMIMeasurementView;
|
||||
import com.health.openscale.gui.views.BMRMeasurementView;
|
||||
@@ -54,6 +55,7 @@ import com.health.openscale.gui.views.WeightMeasurementView;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@@ -255,7 +257,8 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
||||
max_i = scaleDataList.size();
|
||||
}
|
||||
|
||||
Date now = new Date();
|
||||
final Calendar now = Calendar.getInstance();
|
||||
Calendar histCalendar = Calendar.getInstance();
|
||||
|
||||
scaleDataLastDays = new ArrayList<ScaleData>();
|
||||
|
||||
@@ -280,7 +283,8 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
||||
if (histData.getBone() != 0.0f)
|
||||
valuesBone.add(new PointValue(i, histData.getBone()));
|
||||
|
||||
int days = daysBetween(now, histData.getDateTime());
|
||||
histCalendar.setTime(histData.getDateTime());
|
||||
int days = DateTimeHelpers.daysBetween(now, histCalendar);
|
||||
String label = getResources().getQuantityString(R.plurals.label_days, Math.abs(days), days);
|
||||
axisValues.add(new AxisValue(i, label.toCharArray()));
|
||||
}
|
||||
@@ -421,11 +425,6 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
||||
pieChartLast.setPieChartData(pieChartData);
|
||||
}
|
||||
|
||||
private int daysBetween(Date startDate, Date endDate) {
|
||||
final float msPerDay = 24 * 60 * 60 * 1000;
|
||||
return Math.round((endDate.getTime() - startDate.getTime()) / msPerDay);
|
||||
}
|
||||
|
||||
public void btnOnClickInsertData()
|
||||
{
|
||||
Intent intent = new Intent(overviewView.getContext(), DataEntryActivity.class);
|
||||
|
@@ -30,6 +30,7 @@ import com.health.openscale.R;
|
||||
import com.health.openscale.core.OpenScale;
|
||||
import com.health.openscale.core.datatypes.ScaleData;
|
||||
import com.health.openscale.core.datatypes.ScaleUser;
|
||||
import com.health.openscale.core.utils.DateTimeHelpers;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.ArrayList;
|
||||
@@ -114,7 +115,9 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
|
||||
double weight_diff = goalScaleData.getConvertedWeight(currentScaleUser.scale_unit) - lastScaleData.getConvertedWeight(currentScaleUser.scale_unit);
|
||||
txtGoalDiff.setText(String.format("%.1f " + ScaleUser.UNIT_STRING[currentScaleUser.scale_unit], weight_diff));
|
||||
|
||||
int days = Math.max(0, daysBetween(new Date(), currentScaleUser.goal_date));
|
||||
Calendar goalCalendar = Calendar.getInstance();
|
||||
goalCalendar.setTime(currentScaleUser.goal_date);
|
||||
int days = Math.max(0, DateTimeHelpers.daysBetween(Calendar.getInstance(), goalCalendar));
|
||||
txtGoalDayLeft.setText(getResources().getQuantityString(R.plurals.label_days, days, days));
|
||||
|
||||
lastScaleData.setUserId(currentScaleUser.id);
|
||||
@@ -330,9 +333,4 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
|
||||
txtAvgWeek.setText(weekSize + " " + getResources().getString(R.string.label_measures));
|
||||
txtAvgMonth.setText(monthSize + " " + getResources().getString(R.string.label_measures));
|
||||
}
|
||||
|
||||
private int daysBetween(Date startDate, Date endDate) {
|
||||
final float msPerDay = 24 * 60 * 60 * 1000;
|
||||
return (int)Math.ceil((endDate.getTime() - startDate.getTime()) / msPerDay);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,78 @@
|
||||
/* Copyright (C) 2017 Erik Johansson <erik@ejohansson.se>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.health.openscale.core.utils;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class DateTimeHelpersTest {
|
||||
Calendar getDate(int year, int month, int day, int hour, int minute, int second, int ms) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(year, month - 1, day, hour, minute, second);
|
||||
cal.set(Calendar.MILLISECOND, ms);
|
||||
return cal;
|
||||
}
|
||||
Calendar getDate(int year, int month, int day) {
|
||||
return getDate(year, month, day, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void daysBetween() throws Exception {
|
||||
assertEquals(0,
|
||||
DateTimeHelpers.daysBetween(
|
||||
getDate(2017, 1, 1),
|
||||
getDate(2017, 1, 1)));
|
||||
|
||||
assertEquals(0,
|
||||
DateTimeHelpers.daysBetween(
|
||||
getDate(2017, 1, 1, 12, 10, 10, 0),
|
||||
getDate(2017, 1, 1, 12, 9, 0, 0)));
|
||||
|
||||
assertEquals(0,
|
||||
DateTimeHelpers.daysBetween(
|
||||
getDate(2017, 1, 1, 23, 59, 59, 999),
|
||||
getDate(2017, 1, 1)));
|
||||
|
||||
assertEquals(1,
|
||||
DateTimeHelpers.daysBetween(
|
||||
getDate(2017, 1, 1),
|
||||
getDate(2017, 1, 2)));
|
||||
|
||||
assertEquals(-1,
|
||||
DateTimeHelpers.daysBetween(
|
||||
getDate(2017, 1, 2),
|
||||
getDate(2017, 1, 1)));
|
||||
|
||||
assertEquals(1,
|
||||
DateTimeHelpers.daysBetween(
|
||||
getDate(2017, 1, 1, 23, 59, 59, 999),
|
||||
getDate(2017, 1, 2)));
|
||||
|
||||
assertEquals(29 - 10 + 4 * 30 + 6 * 31 + 2,
|
||||
DateTimeHelpers.daysBetween(
|
||||
getDate(2016, 2, 10, 1, 2, 3, 10),
|
||||
getDate(2017, 1, 2)));
|
||||
|
||||
assertEquals(1 + 365 + 366,
|
||||
DateTimeHelpers.daysBetween(
|
||||
getDate(2014, 12, 31),
|
||||
getDate(2017, 1, 1)));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user