fix time in dialogs during DST change
This commit is contained in:
parent
89ee73ef7d
commit
5ce184e67d
3 changed files with 70 additions and 10 deletions
|
@ -4,6 +4,7 @@ import android.support.v4.util.LongSparseArray;
|
||||||
import android.text.format.DateUtils;
|
import android.text.format.DateUtils;
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
import org.joda.time.format.DateTimeFormat;
|
||||||
import org.joda.time.format.DateTimeFormatter;
|
import org.joda.time.format.DateTimeFormatter;
|
||||||
import org.joda.time.format.ISODateTimeFormat;
|
import org.joda.time.format.ISODateTimeFormat;
|
||||||
|
|
||||||
|
@ -73,6 +74,7 @@ public class DateUtil {
|
||||||
|
|
||||||
public static Date toDate(Integer seconds) {
|
public static Date toDate(Integer seconds) {
|
||||||
Calendar calendar = new GregorianCalendar();
|
Calendar calendar = new GregorianCalendar();
|
||||||
|
calendar.set(Calendar.MONTH, 0); // Set january to be sure we miss DST changing
|
||||||
calendar.set(Calendar.HOUR_OF_DAY, seconds / 60 / 60);
|
calendar.set(Calendar.HOUR_OF_DAY, seconds / 60 / 60);
|
||||||
calendar.set(Calendar.MINUTE, (seconds / 60) % 60);
|
calendar.set(Calendar.MINUTE, (seconds / 60) % 60);
|
||||||
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.SECOND, 0);
|
||||||
|
@ -107,11 +109,13 @@ public class DateUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String timeString(Date date) {
|
public static String timeString(Date date) {
|
||||||
return DateUtils.formatDateTime(MainApp.instance(), date.getTime(), DateUtils.FORMAT_SHOW_TIME);
|
//return DateUtils.formatDateTime(MainApp.instance(), date.getTime(), DateUtils.FORMAT_SHOW_TIME);
|
||||||
|
return new DateTime(date).toString(DateTimeFormat.shortTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String timeString(long mills) {
|
public static String timeString(long mills) {
|
||||||
return DateUtils.formatDateTime(MainApp.instance(), mills, DateUtils.FORMAT_SHOW_TIME);
|
//return DateUtils.formatDateTime(MainApp.instance(), mills, DateUtils.FORMAT_SHOW_TIME);
|
||||||
|
return new DateTime(mills).toString(DateTimeFormat.shortTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String dateAndTimeString(Date date) {
|
public static String dateAndTimeString(Date date) {
|
||||||
|
@ -123,12 +127,12 @@ public class DateUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String minAgo(long time) {
|
public static String minAgo(long time) {
|
||||||
int mins = (int) ((System.currentTimeMillis() - time) / 1000 / 60);
|
int mins = (int) ((now() - time) / 1000 / 60);
|
||||||
return MainApp.gs(R.string.minago, mins);
|
return MainApp.gs(R.string.minago, mins);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String hourAgo(long time) {
|
public static String hourAgo(long time) {
|
||||||
double hours = (System.currentTimeMillis() - time) / 1000d / 60 / 60;
|
double hours = (now() - time) / 1000d / 60 / 60;
|
||||||
return MainApp.gs(R.string.hoursago, hours);
|
return MainApp.gs(R.string.hoursago, hours);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +142,7 @@ public class DateUtil {
|
||||||
String cached = timeStrings.get(seconds);
|
String cached = timeStrings.get(seconds);
|
||||||
if (cached != null)
|
if (cached != null)
|
||||||
return cached;
|
return cached;
|
||||||
String t = DateUtils.formatDateTime(MainApp.instance(), toDate(seconds).getTime(), DateUtils.FORMAT_SHOW_TIME);
|
String t = timeString(toDate(seconds));
|
||||||
timeStrings.put(seconds, t);
|
timeStrings.put(seconds, t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,6 +78,8 @@ public class AAPSMocker {
|
||||||
when(MainApp.gs(R.string.insulin_unit_shortname)).thenReturn("U");
|
when(MainApp.gs(R.string.insulin_unit_shortname)).thenReturn("U");
|
||||||
when(MainApp.gs(R.string.minimalbasalvaluereplaced)).thenReturn("Basal value replaced by minimal supported value");
|
when(MainApp.gs(R.string.minimalbasalvaluereplaced)).thenReturn("Basal value replaced by minimal supported value");
|
||||||
when(MainApp.gs(R.string.basalprofilenotaligned)).thenReturn("Basal values not aligned to hours: %s");
|
when(MainApp.gs(R.string.basalprofilenotaligned)).thenReturn("Basal values not aligned to hours: %s");
|
||||||
|
when(MainApp.gs(R.string.minago)).thenReturn("%d min ago");
|
||||||
|
when(MainApp.gs(R.string.hoursago)).thenReturn("%.1fh ago");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MainApp mockMainApp() {
|
public static MainApp mockMainApp() {
|
||||||
|
|
|
@ -1,22 +1,28 @@
|
||||||
package info.nightscout.utils;
|
package info.nightscout.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import info.AAPSMocker;
|
||||||
|
import info.nightscout.androidaps.MainApp;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mike on 20.11.2017.
|
* Created by mike on 20.11.2017.
|
||||||
*/
|
*/
|
||||||
|
@RunWith(PowerMockRunner.class)
|
||||||
|
@PrepareForTest({MainApp.class, DateUtil.class})
|
||||||
public class DateUtilTest {
|
public class DateUtilTest {
|
||||||
|
|
||||||
public DateUtilTest() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void fromISODateStringTest() throws Exception {
|
public void fromISODateStringTest() throws Exception {
|
||||||
assertEquals(1511124634417L, DateUtil.fromISODateString("2017-11-19T22:50:34.417+0200").getTime());
|
assertEquals(1511124634417L, DateUtil.fromISODateString("2017-11-19T22:50:34.417+0200").getTime());
|
||||||
|
@ -28,6 +34,54 @@ public class DateUtilTest {
|
||||||
@Test
|
@Test
|
||||||
public void toISOStringTest() throws Exception {
|
public void toISOStringTest() throws Exception {
|
||||||
assertEquals("2017-12-22T00:32:30Z", DateUtil.toISOString(new Date(1513902750000L)));
|
assertEquals("2017-12-22T00:32:30Z", DateUtil.toISOString(new Date(1513902750000L)));
|
||||||
|
assertEquals("2017-12-22T00:32:30Z", DateUtil.toISOString(1513902750000L));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toDateTest() {
|
||||||
|
assertTrue(DateUtil.toDate((int) (T.hours(1).secs() + T.mins(1).secs() + 1)).toString().contains("01:01:00"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toSecondsTest() {
|
||||||
|
Assert.assertEquals(3600, DateUtil.toSeconds("01:00"));
|
||||||
|
Assert.assertEquals(3600, DateUtil.toSeconds("01:00 a.m."));
|
||||||
|
Assert.assertEquals(3600, DateUtil.toSeconds("01:00 AM"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dateStringTest() {
|
||||||
|
assertTrue(DateUtil.dateString(new Date(1513902750000L)).contains("22"));
|
||||||
|
assertTrue(DateUtil.dateString(1513902750000L).contains("22"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void timeStringTest() {
|
||||||
|
assertTrue(DateUtil.timeString(new Date(1513902750000L)).contains("1:32"));
|
||||||
|
assertTrue(DateUtil.timeString(1513902750000L).contains("1:32"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dateAndTimeStringTest() {
|
||||||
|
assertTrue(DateUtil.dateAndTimeString(1513902750000L).contains("22"));
|
||||||
|
assertTrue(DateUtil.dateAndTimeString(1513902750000L).contains("1:32"));
|
||||||
|
assertTrue(DateUtil.dateAndTimeString(new Date(1513902750000L)).contains("22"));
|
||||||
|
assertTrue(DateUtil.dateAndTimeString(new Date(1513902750000L)).contains("1:32"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void timeStringFromSecondsTest() {
|
||||||
|
Assert.assertEquals("1:00 AM", DateUtil.timeStringFromSeconds((int) T.hours(1).secs()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void timeFrameStringTest() {
|
||||||
|
Assert.assertEquals("(1h 1')", DateUtil.timeFrameString((T.hours(1).msecs() + T.mins(1).msecs())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void prepareMock() {
|
||||||
|
AAPSMocker.mockMainApp();
|
||||||
|
AAPSMocker.mockStrings();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue