/ src / modules / LightSwitch / LightSwitchService / LightSwitchUtils.h
LightSwitchUtils.h
 1  #pragma once
 2  #include <windows.h>
 3  
 4  constexpr bool ShouldBeLight(int nowMinutes, int lightTime, int darkTime)
 5  {
 6      // Normalize values into [0, 1439]
 7      int normalizedLightTime = (lightTime % 1440 + 1440) % 1440;
 8      int normalizedDarkTime = (darkTime % 1440 + 1440) % 1440;
 9      int normalizedNowMinutes = (nowMinutes % 1440 + 1440) % 1440;
10  
11      // Case 1: Normal range, e.g. light mode comes before dark mode in the same day
12      if (normalizedLightTime < normalizedDarkTime)
13          return normalizedNowMinutes >= normalizedLightTime && normalizedNowMinutes < normalizedDarkTime;
14  
15      // Case 2: Wrap-around range, e.g. light mode starts in the evening and dark mode starts in the morning
16      return normalizedNowMinutes >= normalizedLightTime || normalizedNowMinutes < normalizedDarkTime;
17  }
18  
19  inline int GetNowMinutes()
20  {
21      SYSTEMTIME st;
22      GetLocalTime(&st);
23      return st.wHour * 60 + st.wMinute;
24  }