https://git.reactos.org/?p=reactos.git;a=commitdiff;h=505b9f8052de95d6c4734b7a94cdc2a88d1356b4

commit 505b9f8052de95d6c4734b7a94cdc2a88d1356b4
Author:     Eric Kohl <eric.k...@reactos.org>
AuthorDate: Sun May 31 22:33:49 2020 +0200
Commit:     Eric Kohl <eric.k...@reactos.org>
CommitDate: Sun May 31 22:33:49 2020 +0200

    [SCHEDSVC] Implement the job start time calculation for given days of month
---
 base/services/schedsvc/job.c | 69 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 60 insertions(+), 9 deletions(-)

diff --git a/base/services/schedsvc/job.c b/base/services/schedsvc/job.c
index 2f24b95c4d7..558ac3797dd 100644
--- a/base/services/schedsvc/job.c
+++ b/base/services/schedsvc/job.c
@@ -32,6 +32,8 @@ RTL_RESOURCE JobListLock;
 LIST_ENTRY StartListHead;
 RTL_RESOURCE StartListLock;
 
+static WORD wDaysArray[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 
31};
+
 
 /* FUNCTIONS *****************************************************************/
 
@@ -449,13 +451,26 @@ done:
 }
 
 
+static
+WORD
+DaysOfMonth(
+    WORD wMonth,
+    WORD wYear)
+{
+    if (wMonth == 2 && wYear % 4 == 0 && wYear % 400 != 0)
+        return 29;
+
+    return wDaysArray[wMonth];
+}
+
+
 VOID
 CalculateNextStartTime(
     _In_ PJOB pJob)
 {
     SYSTEMTIME CurrentSystemTime, StartSystemTime;
     FILETIME StartFileTime;
-    WORD wDaysOffset, wTempOffset, i, wJobDayOfWeek;
+    WORD wDaysOffset, wTempOffset, i, wJobDayOfWeek, wJobDayOfMonth;
     DWORD_PTR CurrentTimeMs;
     BOOL bDaysOffsetValid;
 
@@ -469,13 +484,6 @@ CalculateNextStartTime(
     CurrentTimeMs = (DWORD_PTR)CurrentSystemTime.wHour * 3600000 +
                     (DWORD_PTR)CurrentSystemTime.wMinute * 60000;
 
-    CopyMemory(&StartSystemTime, &CurrentSystemTime, sizeof(SYSTEMTIME));
-
-    StartSystemTime.wMilliseconds = 0;
-    StartSystemTime.wSecond = 0;
-    StartSystemTime.wHour = (WORD)(pJob->JobTime / 3600000);
-    StartSystemTime.wMinute = (WORD)((pJob->JobTime % 3600000) / 60000);
-
     bDaysOffsetValid = FALSE;
     wDaysOffset = 0;
     if ((pJob->DaysOfWeek == 0) && (pJob->DaysOfMonth == 0))
@@ -497,7 +505,7 @@ CalculateNextStartTime(
             {
                 if (pJob->DaysOfWeek & (1 << i))
                 {
-                    /* Adjust the ranges */
+                    /* Adjust the range */
                     wJobDayOfWeek = (i + 1) % 7;
                     TRACE("wJobDayOfWeek: %hu\n", wJobDayOfWeek);
                     TRACE("CurrentSystemTime.wDayOfWeek: %hu\n", 
CurrentSystemTime.wDayOfWeek);
@@ -533,12 +541,55 @@ CalculateNextStartTime(
         if (pJob->DaysOfMonth != 0)
         {
             FIXME("Support DaysOfMonth!\n");
+            for (i = 0; i < 31; i++)
+            {
+                if (pJob->DaysOfMonth & (1 << i))
+                {
+                    /* Adjust the range */
+                    wJobDayOfMonth = i + 1;
+                    FIXME("wJobDayOfMonth: %hu\n", wJobDayOfMonth);
+                    FIXME("CurrentSystemTime.wDay: %hu\n", 
CurrentSystemTime.wDay);
+
+                    if ((CurrentSystemTime.wDay > wJobDayOfMonth) ||
+                        ((CurrentSystemTime.wDay == wJobDayOfMonth) && 
(CurrentTimeMs >= pJob->JobTime)))
+                    {
+                        wTempOffset = DaysOfMonth(CurrentSystemTime.wMonth, 
CurrentSystemTime.wYear) -
+                                      CurrentSystemTime.wDay + wJobDayOfMonth;
+                        FIXME("wTempOffset: %hu\n", wTempOffset);
+                    }
+                    else
+                    {
+                        wTempOffset = wJobDayOfMonth - CurrentSystemTime.wDay;
+                        FIXME("wTempOffset: %hu\n", wTempOffset);
+                    }
+
+                    /* Use the smallest offset */
+                    if (bDaysOffsetValid == FALSE)
+                    {
+                        wDaysOffset = wTempOffset;
+                        bDaysOffsetValid = TRUE;
+                    }
+                    else
+                    {
+                        if (wTempOffset < wDaysOffset)
+                            wDaysOffset = wTempOffset;
+                    }
+                }
+            }
         }
     }
 
     TRACE("wDaysOffset: %hu\n", wDaysOffset);
 
+    CopyMemory(&StartSystemTime, &CurrentSystemTime, sizeof(SYSTEMTIME));
+
+    StartSystemTime.wMilliseconds = 0;
+    StartSystemTime.wSecond = 0;
+    StartSystemTime.wHour = (WORD)(pJob->JobTime / 3600000);
+    StartSystemTime.wMinute = (WORD)((pJob->JobTime % 3600000) / 60000);
+
     SystemTimeToFileTime(&StartSystemTime, &StartFileTime);
+
     pJob->StartTime.u.LowPart = StartFileTime.dwLowDateTime;
     pJob->StartTime.u.HighPart = StartFileTime.dwHighDateTime;
     if (bDaysOffsetValid && wDaysOffset != 0)

Reply via email to