Index: trunk/FACT++/src/datalogger.cc
===================================================================
--- trunk/FACT++/src/datalogger.cc	(revision 12534)
+++ trunk/FACT++/src/datalogger.cc	(revision 12535)
@@ -386,4 +386,7 @@
     bool fShouldAutoStart;
     bool fAutoStarted;
+
+    //Current day variable. Used to close nightly files when night changes
+    int fCurrentDay;
 public:
     int Write(const Time &time, const std::string &txt, int qos=kMessage);
@@ -751,4 +754,9 @@
     //initialize member data
     fFilePath = ".";
+
+    //calculate time "centered" around noon instead of midnight
+    const Time timeNow;
+    const Time nowMinusTwelve = timeNow-boost::posix_time::hours(12);
+    fCurrentDay = nowMinusTwelve.M()*31 + nowMinusTwelve.D();//assume 31 days per month. we do not really care, only want unique number per day of the year
 
     //Give a name to this machine's specific states
@@ -1179,8 +1187,31 @@
     // of only one.
     //
-    if (Time().h() == 12 && !fDailyFileDayChangedAlready)
-    {
+    // Last but not least, the datalogger MUST receive at least one service update during
+    // noon otherwise this is skipped (which is not good). Thus I would suggest that the datalogger
+    // should always subscribe to at least its statistics services.
+    //
+    //calculate time "centered" around noon instead of midnight
+    const Time timeNow;
+    const Time nowMinusTwelve = timeNow-boost::posix_time::hours(12);
+    int newDayNumber = nowMinusTwelve.M()*31 + nowMinusTwelve.D();//assume 31 days per month. we do not really care, only want unique number per day of the year
+
+//    if (Time().h() == 12 && !fDailyFileDayChangedAlready)
+    if (newDayNumber !=  fCurrentDay)
+    {
+        fCurrentDay = newDayNumber;
+        //crawl through the subcriptions and close any open nightly file
+        SubscriptionsListType::iterator x;
+        map<string, SubscriptionType>::iterator y;
+        for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
+        {//find current service is subscriptions
+            for (y=x->second.begin(); y!=x->second.end();y++)
+                if (y->second.nightlyFile.IsOpen())
+                {
+                    y->second.nightlyFile.Close();
+                }
+        }
+
         if (fDebugIsOn)
-            Debug("Its Noon! Closing and reopening nightly text files");
+            Debug("Day have changed! Closing and reopening nightly files");
 
         fNightlyLogFile << endl;
@@ -1208,8 +1239,8 @@
         }
 
-        fDailyFileDayChangedAlready = true;
-    }
-    if (Time().h() != 12 && fDailyFileDayChangedAlready)
-        fDailyFileDayChangedAlready = false;
+ //       fDailyFileDayChangedAlready = true;
+    }
+//    if (Time().h() != 12 && fDailyFileDayChangedAlready)
+//        fDailyFileDayChangedAlready = false;
 
     //create the converter for that service
@@ -1325,9 +1356,9 @@
         //check if the last received event was before noon and if current one is after noon.
         //if so, close the file so that it gets reopened.
-        if (sub.nightlyFile.IsOpen())
-            if ((sub.lastReceivedEvent != Time::None) && (sub.lastReceivedEvent.h() < 12) && (cTime.h() >= 12))
-            {
-                sub.nightlyFile.Close();
-            }
+//        if (sub.nightlyFile.IsOpen())
+//            if ((sub.lastReceivedEvent != Time::None) && (sub.lastReceivedEvent.h() < 12) && (cTime.h() >= 12))
+//            {
+//                sub.nightlyFile.Close();
+//            }
         sub.lastReceivedEvent = cTime;//ici
         if (!sub.nightlyFile.IsOpen() || !sub.runFile.IsOpen() || sub.runNumber != sub.runFile.fRunNumber)
