2016-06-05 01:40:35 +02:00
|
|
|
package info.nightscout.utils;
|
|
|
|
|
2017-10-02 19:57:41 +02:00
|
|
|
import android.support.v4.util.LongSparseArray;
|
2017-01-03 19:06:35 +01:00
|
|
|
import android.text.format.DateUtils;
|
2017-12-22 02:55:40 +01:00
|
|
|
|
|
|
|
import org.joda.time.DateTime;
|
2018-04-01 18:44:58 +02:00
|
|
|
import org.joda.time.format.DateTimeFormat;
|
2017-12-22 02:55:40 +01:00
|
|
|
import org.joda.time.format.DateTimeFormatter;
|
|
|
|
import org.joda.time.format.ISODateTimeFormat;
|
2017-01-03 19:06:35 +01:00
|
|
|
|
2016-06-05 01:40:35 +02:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
2016-10-20 23:50:31 +02:00
|
|
|
import java.util.Calendar;
|
2016-06-05 01:40:35 +02:00
|
|
|
import java.util.Date;
|
2016-10-20 23:50:31 +02:00
|
|
|
import java.util.GregorianCalendar;
|
2017-10-02 17:38:45 +02:00
|
|
|
import java.util.Locale;
|
2016-06-05 01:40:35 +02:00
|
|
|
import java.util.TimeZone;
|
2016-10-20 23:50:31 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2017-01-03 19:06:35 +01:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
2017-06-26 12:44:03 +02:00
|
|
|
import info.nightscout.androidaps.R;
|
2017-01-03 19:06:35 +01:00
|
|
|
|
2016-06-05 01:40:35 +02:00
|
|
|
/**
|
|
|
|
* The Class DateUtil. A simple wrapper around SimpleDateFormat to ease the handling of iso date string <-> date obj
|
|
|
|
* with TZ
|
|
|
|
*/
|
2016-07-16 22:37:10 +02:00
|
|
|
public class DateUtil {
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2016-07-16 22:37:10 +02:00
|
|
|
/**
|
|
|
|
* The date format in iso.
|
|
|
|
*/
|
2017-12-22 00:37:22 +01:00
|
|
|
private static String FORMAT_DATE_ISO_OUT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
|
|
|
|
2016-06-05 01:40:35 +02:00
|
|
|
/**
|
|
|
|
* Takes in an ISO date string of the following format:
|
|
|
|
* yyyy-mm-ddThh:mm:ss.ms+HoMo
|
|
|
|
*
|
|
|
|
* @param isoDateString the iso date string
|
|
|
|
* @return the date
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
public static Date fromISODateString(String isoDateString)
|
2016-07-16 22:37:10 +02:00
|
|
|
throws Exception {
|
2017-12-03 17:34:29 +01:00
|
|
|
|
2017-12-22 02:55:40 +01:00
|
|
|
DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
|
|
|
|
DateTime dateTime = DateTime.parse(isoDateString, parser);
|
|
|
|
return dateTime.toDate();
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render date
|
|
|
|
*
|
2016-07-16 22:37:10 +02:00
|
|
|
* @param date the date obj
|
2016-06-05 01:40:35 +02:00
|
|
|
* @param format - if not specified, will use FORMAT_DATE_ISO
|
2016-07-16 22:37:10 +02:00
|
|
|
* @param tz - tz to set to, if not specified uses local timezone
|
2016-06-05 01:40:35 +02:00
|
|
|
* @return the iso-formatted date string
|
|
|
|
*/
|
2016-07-16 22:37:10 +02:00
|
|
|
public static String toISOString(Date date, String format, TimeZone tz) {
|
2017-12-22 00:37:22 +01:00
|
|
|
if (format == null) format = FORMAT_DATE_ISO_OUT;
|
2016-07-16 22:37:10 +02:00
|
|
|
if (tz == null) tz = TimeZone.getDefault();
|
2017-10-02 17:38:45 +02:00
|
|
|
DateFormat f = new SimpleDateFormat(format, Locale.getDefault());
|
2016-06-05 01:40:35 +02:00
|
|
|
f.setTimeZone(tz);
|
|
|
|
return f.format(date);
|
|
|
|
}
|
|
|
|
|
2016-07-16 22:37:10 +02:00
|
|
|
public static String toISOString(Date date) {
|
2017-12-22 00:37:22 +01:00
|
|
|
return toISOString(date, FORMAT_DATE_ISO_OUT, TimeZone.getTimeZone("UTC"));
|
2016-07-16 22:37:10 +02:00
|
|
|
}
|
2017-12-03 17:34:29 +01:00
|
|
|
|
2016-07-21 00:18:45 +02:00
|
|
|
public static String toISOString(long date) {
|
2017-12-22 00:37:22 +01:00
|
|
|
return toISOString(new Date(date), FORMAT_DATE_ISO_OUT, TimeZone.getTimeZone("UTC"));
|
2016-07-21 00:18:45 +02:00
|
|
|
}
|
2016-10-20 23:50:31 +02:00
|
|
|
|
|
|
|
public static Date toDate(Integer seconds) {
|
|
|
|
Calendar calendar = new GregorianCalendar();
|
2018-04-01 18:44:58 +02:00
|
|
|
calendar.set(Calendar.MONTH, 0); // Set january to be sure we miss DST changing
|
2016-10-20 23:50:31 +02:00
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, seconds / 60 / 60);
|
|
|
|
calendar.set(Calendar.MINUTE, (seconds / 60) % 60);
|
|
|
|
calendar.set(Calendar.SECOND, 0);
|
|
|
|
return calendar.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int toSeconds(String hh_colon_mm) {
|
2017-10-02 17:38:45 +02:00
|
|
|
Pattern p = Pattern.compile("(\\d+):(\\d+)( a.m.| p.m.| AM | PM|)");
|
2016-10-20 23:50:31 +02:00
|
|
|
Matcher m = p.matcher(hh_colon_mm);
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
if (m.find()) {
|
|
|
|
retval = SafeParse.stringToInt(m.group(1)) * 60 * 60 + SafeParse.stringToInt(m.group(2)) * 60;
|
2017-09-18 21:04:46 +02:00
|
|
|
if ((m.group(3).equals(" a.m.") || m.group(3).equals(" AM")) && m.group(1).equals("12"))
|
2017-08-16 19:33:41 +02:00
|
|
|
retval -= 12 * 60 * 60;
|
2017-09-18 21:04:46 +02:00
|
|
|
if ((m.group(3).equals(" p.m.") || m.group(3).equals(" PM")) && !(m.group(1).equals("12")))
|
2017-08-16 19:33:41 +02:00
|
|
|
retval += 12 * 60 * 60;
|
2016-10-20 23:50:31 +02:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:06:35 +01:00
|
|
|
public static String dateString(Date date) {
|
2017-01-07 23:26:28 +01:00
|
|
|
//return DateUtils.formatDateTime(MainApp.instance(), date.getTime(), DateUtils.FORMAT_SHOW_DATE); this provide month name not number
|
|
|
|
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
|
|
|
|
return df.format(date);
|
2017-01-03 19:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String dateString(long mills) {
|
2017-01-07 23:26:28 +01:00
|
|
|
//return DateUtils.formatDateTime(MainApp.instance(), mills, DateUtils.FORMAT_SHOW_DATE); this provide month name not number
|
|
|
|
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
|
|
|
|
return df.format(mills);
|
2017-01-03 19:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String timeString(Date date) {
|
2018-04-01 18:44:58 +02:00
|
|
|
//return DateUtils.formatDateTime(MainApp.instance(), date.getTime(), DateUtils.FORMAT_SHOW_TIME);
|
|
|
|
return new DateTime(date).toString(DateTimeFormat.shortTime());
|
2017-01-03 19:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String timeString(long mills) {
|
2018-04-01 18:44:58 +02:00
|
|
|
//return DateUtils.formatDateTime(MainApp.instance(), mills, DateUtils.FORMAT_SHOW_TIME);
|
|
|
|
return new DateTime(mills).toString(DateTimeFormat.shortTime());
|
2017-01-03 19:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String dateAndTimeString(Date date) {
|
|
|
|
return dateString(date) + " " + timeString(date);
|
|
|
|
}
|
2017-12-03 17:34:29 +01:00
|
|
|
|
2018-04-09 19:52:01 +02:00
|
|
|
public static String dateAndTimeRangeString(long start, long end) {
|
|
|
|
return dateAndTimeString(start) + " - " + timeString(end);
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:06:35 +01:00
|
|
|
public static String dateAndTimeString(long mills) {
|
|
|
|
return dateString(mills) + " " + timeString(mills);
|
|
|
|
}
|
2017-06-26 12:44:03 +02:00
|
|
|
|
|
|
|
public static String minAgo(long time) {
|
2018-04-01 18:44:58 +02:00
|
|
|
int mins = (int) ((now() - time) / 1000 / 60);
|
2018-02-07 22:50:58 +01:00
|
|
|
return MainApp.gs(R.string.minago, mins);
|
2017-06-26 12:44:03 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 19:05:39 +01:00
|
|
|
public static String hourAgo(long time) {
|
2018-04-01 18:44:58 +02:00
|
|
|
double hours = (now() - time) / 1000d / 60 / 60;
|
2018-02-07 22:50:58 +01:00
|
|
|
return MainApp.gs(R.string.hoursago, hours);
|
2018-01-29 19:05:39 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 19:57:41 +02:00
|
|
|
private static LongSparseArray<String> timeStrings = new LongSparseArray<>();
|
|
|
|
|
|
|
|
public static String timeStringFromSeconds(int seconds) {
|
|
|
|
String cached = timeStrings.get(seconds);
|
|
|
|
if (cached != null)
|
|
|
|
return cached;
|
2018-04-01 18:44:58 +02:00
|
|
|
String t = timeString(toDate(seconds));
|
2017-10-02 19:57:41 +02:00
|
|
|
timeStrings.put(seconds, t);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-03 17:34:29 +01:00
|
|
|
public static String timeFrameString(long timeInMillis) {
|
|
|
|
long remainingTimeMinutes = timeInMillis / (1000 * 60);
|
|
|
|
long remainingTimeHours = remainingTimeMinutes / 60;
|
|
|
|
remainingTimeMinutes = remainingTimeMinutes % 60;
|
|
|
|
return "(" + ((remainingTimeHours > 0) ? (remainingTimeHours + "h ") : "") + remainingTimeMinutes + "')";
|
2017-10-29 02:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 17:34:29 +01:00
|
|
|
public static String sinceString(long timestamp) {
|
|
|
|
return timeFrameString(System.currentTimeMillis() - timestamp);
|
2017-10-29 02:06:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 17:34:29 +01:00
|
|
|
public static String untilString(long timestamp) {
|
|
|
|
return timeFrameString(timestamp - System.currentTimeMillis());
|
2017-10-29 02:06:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 16:51:25 +02:00
|
|
|
public static long now() {
|
|
|
|
return System.currentTimeMillis();
|
|
|
|
}
|
2017-10-29 02:06:06 +02:00
|
|
|
|
2018-04-05 09:39:18 +02:00
|
|
|
public static long roundDateToSec(long date) {
|
|
|
|
return date - date % 1000;
|
|
|
|
}
|
|
|
|
|
2017-10-29 02:06:06 +02:00
|
|
|
}
|