Skip to content

Commit

Permalink
Simplify isTimeBetweenTwoTimes
Browse files Browse the repository at this point in the history
This was an interesting function that took string constructed from hours
and minutes, parsed them back into datetimes around 1st Jan 1970 and
then compared them. Replaced with some simple arithmetic on minutes
passed since the beginning of current day.
  • Loading branch information
AppearamidGuy committed Sep 18, 2023
1 parent 6d86b4f commit ee78866
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

import com.simon.harmonichackernews.R;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class ThemeUtils {

Expand Down Expand Up @@ -116,19 +115,15 @@ public static String getPreferredTheme(Context ctx) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
if (SettingsUtils.shouldUseSpecialNighttimeTheme(ctx)) {
//check time
Date currentTimeDate = Calendar.getInstance().getTime();
Calendar currentCalendar = Calendar.getInstance();
int[] nighttimeHours = Utils.getNighttimeHours(ctx);

String startTime = (nighttimeHours[0] < 10 ? "0" : "") + nighttimeHours[0] + ":" + (nighttimeHours[1] < 10 ? "0" : "") + nighttimeHours[1];
String endTime = (nighttimeHours[2] < 10 ? "0" : "") + nighttimeHours[2] + ":" + (nighttimeHours[3] < 10 ? "0" : "") + nighttimeHours[3];
String currentTime = (currentTimeDate.getHours() < 10 ? "0" : "") + currentTimeDate.getHours() + ":" + (currentTimeDate.getMinutes() < 10 ? "0" : "") + currentTimeDate.getMinutes();
long startTime = TimeUnit.HOURS.toMinutes(nighttimeHours[0]) + nighttimeHours[1];
long endTime = TimeUnit.HOURS.toMinutes(nighttimeHours[2]) + nighttimeHours[3];
long currentTime = TimeUnit.HOURS.toMinutes(currentCalendar.get(Calendar.HOUR_OF_DAY)) + currentCalendar.get(Calendar.MINUTE);

try {
if (Utils.isTimeBetweenTwoTimes(startTime, endTime, currentTime)) {
return prefs.getString("pref_theme_nighttime", "dark");
}
} catch (ParseException e) {
e.printStackTrace();
if (Utils.isTimeBetweenTwoTimes(startTime, endTime, currentTime)) {
return prefs.getString("pref_theme_nighttime", "dark");
}
}
return prefs.getString("pref_theme", "dark");
Expand Down
43 changes: 14 additions & 29 deletions app/src/main/java/com/simon/harmonichackernews/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -47,17 +46,14 @@
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class Utils {

Expand Down Expand Up @@ -524,34 +520,23 @@ public static String readFileContent(Context ctx, Uri uri) throws IOException {
return stringBuilder.toString();
}

@SuppressLint("SimpleDateFormat")
public static boolean isTimeBetweenTwoTimes(String initialTime, String finalTime, String currentTime) throws ParseException {
//Start Time
//all times are from java.util.Date
Date inTime = new SimpleDateFormat("HH:mm").parse(initialTime);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(inTime);

//Current Time
Date checkTime = new SimpleDateFormat("HH:mm").parse(currentTime);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(checkTime);

//End Time
Date finTime = new SimpleDateFormat("HH:mm").parse(finalTime);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(finTime);

if (finalTime.compareTo(initialTime) < 0) {
calendar2.add(Calendar.DATE, 1);
/**
* Check if time represented as minutes since midnight is between two other times.
* <p>
* If {@code initialTime} is after {@code finalTime}, then {@code currentTime} must be between
* last day's {@code initialTime} and this day's {@code finalTime} or this day's {@code initialTime}
* and next day's {@code finalTime}
*/
public static boolean isTimeBetweenTwoTimes(long initialTime, long finalTime, long currentTime) {
if (finalTime < initialTime) {
finalTime += TimeUnit.DAYS.toMinutes(1);
}

if (currentTime.compareTo(initialTime) < 0) {
calendar3.add(Calendar.DATE, 1);
if (currentTime < initialTime) {
currentTime += TimeUnit.DAYS.toMinutes(1);
}

java.util.Date actualTime = calendar3.getTime();
return (actualTime.after(calendar1.getTime()) || actualTime.compareTo(calendar1.getTime()) == 0) && actualTime.before(calendar2.getTime());
return initialTime <= currentTime && currentTime < finalTime;
}

public static void setNighttimeHours(int fromHour, int fromMinute, int toHour, int toMinute, Context ctx) {
Expand Down

0 comments on commit ee78866

Please sign in to comment.