time_control.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "time_control.h"
00013
00014 #include <application/windoze_helper.h>
00015 #include <basis/utf_conversion.h>
00016
00017 #include <time.h>
00018 #if defined(__WIN32__) || defined(__UNIX__)
00019 #include <sys/timeb.h>
00020 #endif
00021 #ifdef __UNIX__
00022 #include <unistd.h>
00023 #endif
00024
00025 using namespace basis;
00026 using namespace structures;
00027
00028 namespace timely {
00029
00030 void time_control::sleep_ms(basis::un_int msec)
00031 {
00032 #ifdef __UNIX__
00033 usleep(msec * 1000);
00034 #endif
00035 #ifdef __WIN32__
00036 Sleep(msec);
00037 #endif
00038 }
00039
00040 bool time_control::set_time(const time_locus &new_time)
00041 {
00042 #ifdef __WIN32__
00043 SYSTEMTIME os_time;
00044 os_time.wYear = WORD(new_time.year);
00045 os_time.wMonth = new_time.month;
00046 os_time.wDayOfWeek = new_time.day_of_week;
00047 os_time.wDay = new_time.day_of_year;
00048 os_time.wHour = new_time.hour;
00049 os_time.wMinute = new_time.minute;
00050 os_time.wSecond = new_time.second;
00051 os_time.wMilliseconds = 0;
00052
00053
00054 HANDLE petoken;
00055 OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
00056 | TOKEN_QUERY, &petoken);
00057
00058
00059
00060 LUID our_id;
00061 LookupPrivilegeValue(NULL, to_unicode_temp("SeSystemTimePrivilege"), &our_id);
00062
00063 TOKEN_PRIVILEGES privs;
00064 privs.PrivilegeCount = 1;
00065 privs.Privileges[0].Luid = our_id;
00066 privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
00067
00068 AdjustTokenPrivileges(petoken, false, &privs, sizeof(TOKEN_PRIVILEGES),
00069 NULL, NULL);
00070
00071 SetLocalTime(&os_time);
00072
00073
00074 AdjustTokenPrivileges(petoken, true, &privs, sizeof(TOKEN_PRIVILEGES),
00075 NULL, NULL);
00076
00077
00078
00079 ::PostMessage(HWND_BROADCAST, WM_TIMECHANGE, 0, 0);
00080
00081
00082 CloseHandle(petoken);
00083
00084 return true;
00085 #elif defined(__UNIX__)
00086
00087
00088
00089 time_locus ted = new_time;
00090 return ted.year ? 0:1;
00091
00092 #else
00093 return false;
00094 #endif
00095 }
00096
00097 }
00098