Fix time parsing/display in EPG. Always use UTC for parsing datetimes.

This commit is contained in:
Synced Synapse 2015-12-16 19:05:41 +00:00
parent 4d7acd80d0
commit b56384ac16
2 changed files with 25 additions and 30 deletions

View File

@ -4,6 +4,12 @@ import com.fasterxml.jackson.databind.JsonNode;
import org.xbmc.kore.utils.JsonUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Types from PVR.*
*/
@ -98,7 +104,6 @@ public class PVRType {
public static final String WASACTIVE = "wasactive";
public final int broadcastid;
public final String endtime;
public final String episodename;
public final int episodenum;
public final int episodepart;
@ -113,10 +118,11 @@ public class PVRType {
public final double progresspercentage;
public final int rating;
public final int runtime;
public final String starttime;
public final String thumbnail;
public final String title;
public final boolean wasactive;
public Date starttime;
public Date endtime;
/**
* Constructor
@ -125,7 +131,6 @@ public class PVRType {
public DetailsBroadcast(JsonNode node) {
super(node);
broadcastid = JsonUtils.intFromJsonNode(node, BROADCASTID);
endtime = JsonUtils.stringFromJsonNode(node, ENDTIME);
episodename = JsonUtils.stringFromJsonNode(node, EPISODENAME);
episodenum = JsonUtils.intFromJsonNode(node, EPISODENUM, 0);
episodepart = JsonUtils.intFromJsonNode(node, EPISODEPART, 0);
@ -140,10 +145,20 @@ public class PVRType {
progresspercentage = JsonUtils.doubleFromJsonNode(node, PROGRESSPERCENTAGE, 0);
rating = JsonUtils.intFromJsonNode(node, RATING, 0);
runtime = JsonUtils.intFromJsonNode(node, RUNTIME, 0);
starttime = JsonUtils.stringFromJsonNode(node, STARTTIME);
thumbnail = JsonUtils.stringFromJsonNode(node, THUMBNAIL);
title = JsonUtils.stringFromJsonNode(node, TITLE);
wasactive = JsonUtils.booleanFromJsonNode(node, WASACTIVE, false);
// Get times. All in UTC
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
endtime = sdf.parse(JsonUtils.stringFromJsonNode(node, ENDTIME));
starttime = sdf.parse(JsonUtils.stringFromJsonNode(node, STARTTIME));
} catch (ParseException exc) {
starttime = new Date();
endtime = new Date();
}
}
}

View File

@ -43,6 +43,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import butterknife.ButterKnife;
import butterknife.InjectView;
@ -231,19 +232,8 @@ public class PVRChannelEPGListFragment extends Fragment
String duration = String.format(this.getContext().getString(R.string.minutes_abbrev2),
String.valueOf(broadcastDetails.runtime));
// Parse dates
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date startTime, endTime;
try {
startTime = sdf.parse(broadcastDetails.starttime);
// endTime = sdf.parse(broadcastDetails.endtime);
} catch (ParseException exc) {
startTime = new Date();
// endTime = new Date();
}
int flags = DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_TIME;
viewHolder.startTimeView.setText(DateUtils.formatDateTime(getActivity(), startTime.getTime(), flags));
viewHolder.startTimeView.setText(DateUtils.formatDateTime(getActivity(), broadcastDetails.starttime.getTime(), flags));
viewHolder.endTimeView.setText(duration);
} else {
// For a day
@ -307,34 +297,24 @@ public class PVRChannelEPGListFragment extends Fragment
* @return List of rows to show
*/
public static List<EPGListRow> buildFromBroadcastList(List<PVRType.DetailsBroadcast> broadcasts) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date currentTime = new Date(),
startTime, endTime;
Date currentTime = new Date();
int previousDayIdx = 0, dayIdx;
Calendar cal = Calendar.getInstance();
List<EPGListRow> result = new ArrayList<>(broadcasts.size() + 5);
for (PVRType.DetailsBroadcast broadcast: broadcasts) {
try {
startTime = sdf.parse(broadcast.starttime);
endTime = sdf.parse(broadcast.endtime);
} catch (ParseException exc) {
startTime = new Date();
endTime = new Date();
}
// Ignore if before current time
if (endTime.before(currentTime)) {
if (broadcast.endtime.before(currentTime)) {
continue;
}
cal.setTime(startTime);
cal.setTime(broadcast.starttime);
dayIdx = cal.get(Calendar.YEAR) * 366 + cal.get(Calendar.DATE);
if (dayIdx > previousDayIdx) {
// New day, add a row representing it to the list
previousDayIdx = dayIdx;
result.add(new EPGListRow(startTime));
result.add(new EPGListRow(broadcast.starttime));
}
result.add(new EPGListRow(broadcast));
}