Reactos
at master 63 lines 2.2 kB view raw
1// 2// systime.cpp 3// 4// Copyright (c) Microsoft Corporation. All rights reserved. 5// 6// The _getsystime() and _setsystime() functions. 7// 8#include <corecrt_internal_time.h> 9 10 11 12// Gets the current system time and stores it in 'result'. Returns the number 13// of milliseconds (the 'result' cannot store milliseconds). 14extern "C" unsigned __cdecl _getsystime(struct tm* const result) 15{ 16 _VALIDATE_RETURN(result != nullptr, EINVAL, 0) 17 18 SYSTEMTIME local_time; 19 GetLocalTime(&local_time); 20 21 result->tm_isdst = -1; // mktime() computes whether this is 22 // during Standard or Daylight time. 23 result->tm_sec = static_cast<int>(local_time.wSecond); 24 result->tm_min = static_cast<int>(local_time.wMinute); 25 result->tm_hour = static_cast<int>(local_time.wHour); 26 result->tm_mday = static_cast<int>(local_time.wDay); 27 result->tm_mon = static_cast<int>(local_time.wMonth - 1); 28 result->tm_year = static_cast<int>(local_time.wYear - 1900); 29 result->tm_wday = static_cast<int>(local_time.wDayOfWeek); 30 31 // Normalize uninitialized fields: 32 _mktime32(result); 33 34 return (local_time.wMilliseconds); 35} 36 37 38 39// Sets the system time to the time specified by 'source' and 'milliseconds. 40// Returns zero on success; returns a system error code on failure. 41extern "C" unsigned __cdecl _setsystime(struct tm* const source, unsigned const milliseconds) 42{ 43 _ASSERTE(source != nullptr); 44 if (source == nullptr) 45 return ERROR_INVALID_PARAMETER; 46 47 // Normalize uninitialized fields: 48 _mktime32(source); 49 50 SYSTEMTIME local_time; 51 local_time.wYear = static_cast<WORD>(source->tm_year + 1900); 52 local_time.wMonth = static_cast<WORD>(source->tm_mon + 1); 53 local_time.wDay = static_cast<WORD>(source->tm_mday); 54 local_time.wHour = static_cast<WORD>(source->tm_hour); 55 local_time.wMinute = static_cast<WORD>(source->tm_min); 56 local_time.wSecond = static_cast<WORD>(source->tm_sec); 57 local_time.wMilliseconds = static_cast<WORD>(milliseconds); 58 59 if (!SetLocalTime(&local_time)) 60 return static_cast<int>(GetLastError()); 61 62 return 0; 63}