Browse Source

Implement WindowsClock set functionality

Patrick-Christopher 1 year ago
parent
commit
49c6e7a6dd

+ 6 - 0
include/ls-std/time/common/DateParameterMapper.hpp

@@ -13,6 +13,9 @@
 #include "DateParameter.hpp"
 #include <ls-std/os/dynamic-goal.hpp>
 #include <ls-std/time/type/DateParameterTypes.hpp>
+#ifdef _WIN32
+  #include <windows.h>
+#endif
 
 namespace ls::std::time
 {
@@ -25,6 +28,9 @@ namespace ls::std::time
 
 #if defined(unix) || defined(__APPLE__)
       [[nodiscard]] static ls::std::time::type::UnixTimestamp toUnixTimestamp(const ls::std::time::DateParameter &_dateParameter);
+#endif
+#ifdef _WIN32
+      [[nodiscard]] static SYSTEMTIME toWindowsSystemTime(const ls::std::time::DateParameter &_dateParameter);
 #endif
   };
 }

+ 17 - 0
source/ls-std/time/common/DateParameterMapper.cpp

@@ -37,3 +37,20 @@ UnixTimestamp DateParameterMapper::toUnixTimestamp(const DateParameter &_datePar
   return (UnixTimestamp) mktime(timeInfo);
 }
 #endif
+
+#ifdef _WIN32
+SYSTEMTIME DateParameterMapper::toWindowsSystemTime(const DateParameter &_dateParameter)
+{
+  SYSTEMTIME systemTime{};
+  systemTime.wYear = _dateParameter.getYear();
+  systemTime.wMonth = _dateParameter.getMonth();
+  systemTime.wDayOfWeek = 0;
+  systemTime.wDay = _dateParameter.getDay();
+  systemTime.wHour = _dateParameter.getHour();
+  systemTime.wMinute = _dateParameter.getMinute();
+  systemTime.wSecond = _dateParameter.getSecond();
+  systemTime.wMilliseconds = 0;
+
+  return systemTime;
+}
+#endif

+ 5 - 1
source/ls-std/time/system-time/WindowsClock.cpp

@@ -7,9 +7,12 @@
 *
 * */
 
+#include <ls-std/time/common/DateParameterMapper.hpp>
 #include <ls-std/time/system-time/WindowsClock.hpp>
+#include <windows.h>
 
 using ls::std::time::DateParameter;
+using ls::std::time::DateParameterMapper;
 using ls::std::time::WindowsClock;
 
 WindowsClock::WindowsClock() = default;
@@ -18,5 +21,6 @@ WindowsClock::~WindowsClock() noexcept = default;
 
 bool WindowsClock::setTime(const DateParameter &_dateParameter)
 {
-  return true; // TODO: implement
+  SYSTEMTIME systemTime = DateParameterMapper::toWindowsSystemTime(_dateParameter);
+  return SetLocalTime(&systemTime);
 }

+ 38 - 0
test/cases/time/common/DateParameterMapperTest.cpp

@@ -11,6 +11,9 @@
 #include <ls-std/ls-std-time.hpp>
 #include <utility>
 #include <vector>
+#ifdef _WIN32
+  #include <windows.h>
+#endif
 
 using ls::std::time::DateParameter;
 using ls::std::time::DateParameterMapper;
@@ -50,4 +53,39 @@ namespace
 
   INSTANTIATE_TEST_SUITE_P(toUnixTimestamp, DateParameterMapperTest_Unix, Values(DateParameterMapperTest_Unix::getTestParameterList().at(0), DateParameterMapperTest_Unix::getTestParameterList().at(1)));
 #endif
+#ifdef _WIN32
+  class DateParameterMapperTest_Windows : public TestWithParam<DateParameter>
+  {
+    public:
+
+      DateParameterMapperTest_Windows() = default;
+      ~DateParameterMapperTest_Windows() override = default;
+
+      static vector<DateParameter> getTestParameterList()
+      {
+        vector<DateParameter> testParameterList{};
+        testParameterList.emplace_back(1990, 10, 26, 11, 25, 00);
+        testParameterList.emplace_back(1989, 6, 1, 10, 52, 00);
+
+        return testParameterList;
+      }
+  };
+
+  TEST_P(DateParameterMapperTest_Windows, toWindowsSystemTime)
+  {
+    DateParameter dateParameter = GetParam();
+    SYSTEMTIME systemTime = DateParameterMapper::toWindowsSystemTime(dateParameter);
+
+    ASSERT_EQ(systemTime.wYear, GetParam().getYear());
+    ASSERT_EQ(systemTime.wMonth, GetParam().getMonth());
+    ASSERT_EQ(systemTime.wDayOfWeek, 0);
+    ASSERT_EQ(systemTime.wDay, GetParam().getDay());
+    ASSERT_EQ(systemTime.wHour, GetParam().getHour());
+    ASSERT_EQ(systemTime.wMinute, GetParam().getMinute());
+    ASSERT_EQ(systemTime.wSecond, GetParam().getSecond());
+    ASSERT_EQ(systemTime.wMilliseconds, 0);
+  }
+
+  INSTANTIATE_TEST_SUITE_P(toWindowsSystemTime, DateParameterMapperTest_Windows, Values(DateParameterMapperTest_Windows::getTestParameterList().at(0), DateParameterMapperTest_Windows::getTestParameterList().at(1)));
+#endif
 }