From f047138eaf2253b513941a1086241733d1769d32 Mon Sep 17 00:00:00 2001
From: vincent <vincent@groupe-sa.fr>
Date: Mon, 4 Jan 2016 14:54:23 +0100
Subject: [PATCH] Add nextHoliday method.

---
 src/holiday.cpp       | 76 +++++++++++++++++++++++++++++++++----------
 src/holiday.hpp       |  2 ++
 test/holiday_test.cpp | 12 +++++++
 test/holiday_test.hpp |  1 +
 4 files changed, 73 insertions(+), 18 deletions(-)

diff --git a/src/holiday.cpp b/src/holiday.cpp
index 3276839..87235c2 100644
--- a/src/holiday.cpp
+++ b/src/holiday.cpp
@@ -1,6 +1,30 @@
 #include "holiday.hpp"
 
 #include <QList>
+#include <QDebug>
+
+/******************************************************************************/
+
+QList<QDate>
+Holiday::
+yearsHolidays(int year)
+{
+    QDate easter = Holiday::easter(year);
+    QList<QDate> holidays  = QList<QDate>()
+                             << QDate(year, 1, 1)     // Jour de l'an
+                             << easter.addDays(1)  // Lundi de Paques
+                             << easter.addDays(39) // Jeaudi de l'Ascension
+                             << easter.addDays(50) // Lundi de Pentecote
+                             << QDate(year, 5, 1)     // Fete du travail
+                             << QDate(year, 5, 8)     // Fete de la victoire
+                             << QDate(year, 7, 14)    // Fete nationale
+                             << QDate(year, 8, 15)    // Assomption
+                             << QDate(year, 11, 1)    // Toussaint
+                             << QDate(year, 11, 11)   // Armistice de 1918
+                             << QDate(year, 12, 25)   // Noel
+                             ;
+    return holidays;
+}
 
 /******************************************************************************/
 
@@ -8,24 +32,7 @@ bool
 Holiday::
 isHoliday(const QDate & today)
 {
-    int y = today.year();
-    QList<QDate> holidays  = QList<QDate>()
-                             << QDate(y, 1, 1)   // Jour de l'an
-                             << QDate(y, 5, 1)   // Fete du travail
-                             << QDate(y, 5, 8)   // Fete de la victoire
-                             << QDate(y, 7, 14)  // Fete nationale
-                             << QDate(y, 8, 15)  // Assomption
-                             << QDate(y, 11, 1)  // Toussaint
-                             << QDate(y, 11, 11) // Armistice de 1918
-                             << QDate(y, 12, 25) // Noel
-                             ;
-    QDate easter = Holiday::easter(y);
-
-    return holidays.contains(today) ||
-           today == easter.addDays(1) ||  // Lundi de Paques
-           today == easter.addDays(39) || // Jeaudi de l'Ascension
-           today == easter.addDays(50)    // Lundi de Pentecote
-           ;
+    return yearsHolidays(today.year()).contains(today);
 }
 
 /******************************************************************************/
@@ -54,3 +61,36 @@ easter(int y)
 }
 
 /******************************************************************************/
+
+QDate
+Holiday::
+next(const QDate & date)
+{
+    int year = date.year();
+    QList<QDate> backwardHolidays = yearsHolidays(year);
+    /*for (int i = 0; i < backwardHolidays.size() / 2; ++i)
+    {
+        backwardHolidays.swap(i, backwardHolidays.size() - (1 + i));
+        }*/
+    int days_before_holiday = date.daysTo(backwardHolidays.takeLast());
+
+    if (days_before_holiday < 0)
+    {
+        /* 'date' is after xmas: next holiday is New Year's Eve next
+           year. */
+        return QDate(year + 1, 1, 1);
+    }
+    foreach (QDate holiday, backwardHolidays)
+    {
+        days_before_holiday = date.daysTo(holiday);
+        if (days_before_holiday > 0)
+        {
+            return holiday;
+        }
+    }
+    qDebug() << "Can't find a valid next holiday" << date;
+    qFatal("Should not be here");
+    return QDate();
+}
+
+/******************************************************************************/
diff --git a/src/holiday.hpp b/src/holiday.hpp
index 03f1c87..7af9d93 100644
--- a/src/holiday.hpp
+++ b/src/holiday.hpp
@@ -6,8 +6,10 @@
 class Holiday
 {
 public:
+    static QList<QDate> yearsHolidays(int year);
     static bool isHoliday(const QDate & date);
     static QDate easter(int year);
+    static QDate next(const QDate & date);
 };
 
 #endif
diff --git a/test/holiday_test.cpp b/test/holiday_test.cpp
index 5c067cb..f1657ec 100644
--- a/test/holiday_test.cpp
+++ b/test/holiday_test.cpp
@@ -66,4 +66,16 @@ holiday()
 
 /******************************************************************************/
 
+void
+HolidayTest::
+nextHoliday()
+{
+    QDate today = QDate(2016, 1, 1);
+    QCOMPARE(Holiday::next(today), QDate(2016, 3, 28));
+    today = QDate(2016, 2, 1);
+    QCOMPARE(Holiday::next(today), QDate(2016, 3, 28));
+}
+
+/******************************************************************************/
+
 QTEST_MAIN(HolidayTest)
diff --git a/test/holiday_test.hpp b/test/holiday_test.hpp
index 83d9377..b73f533 100644
--- a/test/holiday_test.hpp
+++ b/test/holiday_test.hpp
@@ -10,6 +10,7 @@ class HolidayTest : public QObject
 private slots:
     void easter();
     void holiday();
+    void nextHoliday();
 };
 
 #endif