Skip to content

Commit

Permalink
Fixed bug where holidays wouldn't show up in single months
Browse files Browse the repository at this point in the history
Corrected display error if the holiday's coudn't be retrieved
Added another test for single month holidays
  • Loading branch information
frossm committed Dec 13, 2023
1 parent 8972e32 commit dbfa10f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.fross</groupId>
<artifactId>cal</artifactId>
<version>2.5.6</version>
<version>2.5.7</version>
<packaging>jar</packaging>

<name>cal</name>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: fcal
version: '2.5.6'
version: '2.5.7'
summary: Command line calendar display
description: |
fCal is a command line calendar utility. It will display a
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/org/fross/cal/Calendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,17 @@ public static boolean isLeapYear(int year) {
* @param args
*/
public static void printMonth(int month, int year) {
String[] days = getCalDays(month, year);

Output.debugPrintln(" 1 2 3 4 5 6 7 8 9 1\n"
+ "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");

// If Display Holidays is enabled get the information
if (Holidays.queryHolidaysEnabled() == true) {
holidayList = Holidays.getHolidays(year);
}

// Get the holidays for the month and year provided
String[] days = getCalDays(month, year);

Output.printColorln(Ansi.Color.CYAN, getCalHeader(month, year));
Output.printColorln(Ansi.Color.YELLOW, "Su Mo Tu We Th Fr Sa");

Expand All @@ -126,6 +132,7 @@ public static void printMonth(int month, int year) {

// If display holidays is enabled, display the list after the calendar
if (Holidays.queryHolidaysEnabled() == true) {
Output.printColorln(Ansi.Color.YELLOW, "\nHolidays");
StringBuilder sb = Holidays.queryHolidayListMonth(month);
Output.printColorln(Ansi.Color.CYAN, sb.toString());
}
Expand All @@ -145,6 +152,11 @@ public static void printYear(int year) {
Output.debugPrintln(" 1 2 3 4 5 6 7 8 9 1\n"
+ "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");

// If Display Holidays is enabled get the information
if (Holidays.queryHolidaysEnabled() == true) {
holidayList = Holidays.getHolidays(year);
}

// Loop through the calendar rows
for (i = 0; i < 12; i = i + calsPerRow) {
// Initialize the arrays
Expand Down Expand Up @@ -230,11 +242,6 @@ public static String[] getCalDays(int month, int year) {
daysInMonth[2] = 29;
}

// If Display Holidays is enabled get the information
if (Holidays.queryHolidaysEnabled() == true) {
holidayList = Holidays.getHolidays(year);
}

// Determine the which day of the week the 1st fall upon
int firstDayOfMon = getDayOfWeek(month, 1, year);
Output.debugPrintln("Firstday for " + month + "/" + year + ": " + firstDayOfMon);
Expand All @@ -257,8 +264,8 @@ public static String[] getCalDays(int month, int year) {
colorizedDay = ColorizeDay(day, TODAYHIGHLIGHT_FG, TODAYHIGHLIGHT_BG);
returnString[row] += String.format("%s ", colorizedDay);

// If holiday display is on, check to see if the current day we're processing is one
} else if (Holidays.queryHolidaysEnabled() == true
// If holiday display is on, and it's not null, check to see if the current day we're processing is one
} else if (Holidays.queryHolidaysEnabled() == true && holidayList != null
&& holidayList.get(year + "-" + String.format("%02d", month) + "-" + String.format("%02d", day)) != null) {
colorizedDay = ColorizeDay(day, 179);
returnString[row] += String.format("%s ", colorizedDay);
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/org/fross/cal/Holidays.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static TreeMap<String, String> getHolidays(int year) {
} catch (Exception ex) {
// Couldn't retrieve the holidays - turn off holiday display
Holidays.setDisplayHolidays(false);
Output.printColorln(Ansi.Color.RED, "Unable to retrieve the " + year + " holidays for " + locale.getDisplayCountry() + "\n");
Output.printColorln(Ansi.Color.RED, "Unable to retrieve holidays for the year '" + year + "' in " + locale.getDisplayCountry() + "\n");
return null;
}

Expand Down Expand Up @@ -174,24 +174,36 @@ public static void printHolidayListYear(int year, int displayWidth) {
}
}

/**
* queryHolidayListMonth(): If a year is provided, pull those holidays before returning the monthly list. Mostly used for
* testing I would assume
*
* @param month
* @param year
* @return
*/
public static StringBuilder queryHolidayListMonth(int month, int year) {
// Empty the current holiday list
holidays.clear();

// Pull the holidays for the year provided
holidays = Holidays.getHolidays(year);

// Return the holidays for that month and year
return queryHolidayListMonth(month);
}

/**
* queryHolidayListMonth(): Return the holidays for the month provided
*
* @param month
*/
public static StringBuilder queryHolidayListMonth(int mon) {
String month = "";
public static StringBuilder queryHolidayListMonth(int month) {
StringBuilder sb = new StringBuilder();

// Convert the month integer to a string
month = String.valueOf(mon);

Output.printColorln(Ansi.Color.YELLOW, "\nHolidays");

// Loop through the holidays for the current year, printing those in the given month
for (String key : holidays.keySet()) {
if (key.split("-")[1].compareTo(month) == 0) {
// Output.printColorln(Ansi.Color.CYAN, key + " | " + holidays.get(key));
if (key.split("-")[1].compareTo(String.format("%02d", month)) == 0) {
sb.append(key + " | " + holidays.get(key));
sb.append("\n");
}
Expand Down
17 changes: 16 additions & 1 deletion src/test/java/org/fross/cal/HolidaysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ void holidayListTestUS() {
i = i + 1;
}
Output.println("...Holiday Test Complete");
}

// Test US month holidays for April 2025
@Test
void monthHolidayListTestUS() {
// ------------------------------------------------------------------------
// For now, skip this test if the JVM does not report it's in the US
// ------------------------------------------------------------------------
if (Locale.getDefault().getDisplayCountry().toString().compareTo("United States") != 0) {
Output.println("Current locale set to: '" + Locale.getDefault().getDisplayCountry() + "' -- Skipping Test");
return;
}

StringBuilder sb = Holidays.queryHolidayListMonth(4, 2025);
assertEquals("2025-04-18 | Good Friday", sb.toString().trim());

}

Expand Down Expand Up @@ -94,4 +109,4 @@ void holidayListTestUS() {
//
// Output.println("Canada Holiday Test Complete");
// }
}
}

0 comments on commit dbfa10f

Please sign in to comment.