Skip to content

Commit

Permalink
#1350 - Observation viewer - display observed on in original observat…
Browse files Browse the repository at this point in the history
…ion timezone instead of local
  • Loading branch information
budowski committed Sep 29, 2024
1 parent c013a40 commit 1f78b3d
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -2064,7 +2065,7 @@ public void onClick(View view) {
mUserPic.setOnClickListener(showUser);
}

mObservedOn.setText(formatObservedOn(mObservation.observed_on, mObservation.time_observed_at));
mObservedOn.setText(formatObservedOn(mObservation.observed_on, mObservation.time_observed_at, mObservation.observed_on_string));

if (mPhotosAdapter.getCount() <= 1) {
mIndicator.setVisibility(View.GONE);
Expand Down Expand Up @@ -2393,7 +2394,7 @@ private JSONObject downloadJson(String uri) {

}

private String formatObservedOn(Timestamp date, Timestamp time) {
private String formatObservedOn(Timestamp date, Timestamp time, String observedOnString) {
BetterJSONObject observation = mObsJson != null ? new BetterJSONObject(mObsJson) : null;
// Only show month/year for observations that you don't own + obscured/private
String currentUser = mApp.currentUserLogin();
Expand All @@ -2403,6 +2404,17 @@ private String formatObservedOn(Timestamp date, Timestamp time) {
observation.getJSONObject("user").optString("login", "").equals(currentUser);
boolean isPrivateOrObscured = false;


// Prefer the time string, if available, since it's in local time zone.
Date parsedTime = null;
if (observedOnString != null && observedOnString.length() > 0) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd h:mm a");
try {
parsedTime = sdf.parse(observedOnString);
} catch (ParseException e) {
}
}

if (observation != null) {
if (observation.getString("geoprivacy") != null) {
if (observation.getString("geoprivacy").equals("private") ||
Expand Down Expand Up @@ -2434,7 +2446,12 @@ private String formatObservedOn(Timestamp date, Timestamp time) {
Calendar today = Calendar.getInstance();
today.setTime(new Date());
Calendar calDate = Calendar.getInstance();
calDate.setTimeInMillis(date.getTime());

if (parsedTime != null) {
calDate.setTime(parsedTime);
} else {
calDate.setTimeInMillis(date.getTime());
}

String dateFormatString;
if (today.get(Calendar.YEAR) > calDate.get(Calendar.YEAR)) {
Expand All @@ -2448,6 +2465,7 @@ private String formatObservedOn(Timestamp date, Timestamp time) {
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatString);
format.append(dateFormat.format(date));
}

if (time != null) {
// Format the time part
if (date != null) {
Expand All @@ -2462,7 +2480,11 @@ private String formatObservedOn(Timestamp date, Timestamp time) {
}
SimpleDateFormat timeFormat = new SimpleDateFormat(timeFormatString);

format.append(timeFormat.format(time));
if (parsedTime != null) {
format.append(timeFormat.format(parsedTime));
} else {
format.append(timeFormat.format(time));
}
}

return format.toString();
Expand Down

0 comments on commit 1f78b3d

Please sign in to comment.