feisty meow concerns codebase  2.140
time_control.cpp
Go to the documentation of this file.
1 // Name : time_control
2 // Author : Chris Koeritz
3 /******************************************************************************
4 * Copyright (c) 1994-$now By Author. This program is free software; you can *
5 * redistribute it and/or modify it under the terms of the GNU General Public *
6 * License as published by the Free Software Foundation; either version 2 of *
7 * the License or (at your option) any later version. This is online at: *
8 * http://www.fsf.org/copyleft/gpl.html *
9 * Please send any updates to: fred@gruntose.com *
10 \*****************************************************************************/
11 
12 #include "time_control.h"
13 
15 #include <basis/utf_conversion.h>
16 
17 #include <time.h>
18 #if defined(__WIN32__) || defined(__UNIX__)
19  #include <sys/timeb.h>
20 #endif
21 #ifdef __UNIX__
22  #include <unistd.h>
23 #endif
24 
25 using namespace basis;
26 using namespace structures;
27 
28 namespace timely {
29 
30 void time_control::sleep_ms(basis::un_int msec)
31 {
32 #ifdef __UNIX__
33  usleep(msec * 1000);
34 #endif
35 #ifdef __WIN32__
36  Sleep(msec);
37 #endif
38 }
39 
40 bool time_control::set_time(const time_locus &new_time)
41 {
42 #ifdef __WIN32__
43  SYSTEMTIME os_time;
44  os_time.wYear = WORD(new_time.year);
45  os_time.wMonth = new_time.month;
46  os_time.wDayOfWeek = new_time.day_of_week;
47  os_time.wDay = new_time.day_of_year;
48  os_time.wHour = new_time.hour;
49  os_time.wMinute = new_time.minute;
50  os_time.wSecond = new_time.second;
51  os_time.wMilliseconds = 0; // currently unused.
52 
53  // get our process token for manipulation.
54  HANDLE petoken;
55  OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
56  | TOKEN_QUERY, &petoken);
57  // get our
58 //something or other
59 // identifier so we can adjust our privileges.
60  LUID our_id;
61  LookupPrivilegeValue(NULL, to_unicode_temp("SeSystemTimePrivilege"), &our_id);
62  // make up a privilege structure for the adjustment.
63  TOKEN_PRIVILEGES privs;
64  privs.PrivilegeCount = 1;
65  privs.Privileges[0].Luid = our_id;
66  privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
67  // enable system-time privileges.
68  AdjustTokenPrivileges(petoken, false, &privs, sizeof(TOKEN_PRIVILEGES),
69  NULL, NULL);
70 
71  SetLocalTime(&os_time); // actually set the time.
72 
73  // disable the time adjustment privileges again.
74  AdjustTokenPrivileges(petoken, true, &privs, sizeof(TOKEN_PRIVILEGES),
75  NULL, NULL);
76 
77  // let all the main windows know that the time got adjusted.
78 //do we need to do this ourselves?
79  ::PostMessage(HWND_BROADCAST, WM_TIMECHANGE, 0, 0);
80 
81 //hmmm: make sure this seems right.
82  CloseHandle(petoken);
83 
84  return true;
85 #elif defined(__UNIX__)
86 //no implem yet.
87 
88 //temp to shut up warnings
89 time_locus ted = new_time;
90 return ted.year ? 0:1;
91 
92 #else
93  return false;
94 #endif
95 }
96 
97 } // namespace.
98 
time_number minute
The number of minutes after the hour.
Definition: earth_time.h:88
time_number second
The number of seconds after the current minute.
Definition: earth_time.h:89
time_number hour
The hour represented in military time: 0 through 23.
Definition: earth_time.h:87
time_number day_of_year
Numerical day, where January 1st is equal to zero.
Definition: earth_time.h:144
days day_of_week
The day of the week.
Definition: earth_time.h:143
months month
The current month.
Definition: earth_time.h:141
An object that represents a particular point in time.
Definition: earth_time.h:188
time_number year
The year, using the gregorian calendar.
Definition: earth_time.h:190
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
unsigned int un_int
Abbreviated name for unsigned integers.
Definition: definitions.h:62
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
#include <time.h>
Definition: earth_time.cpp:37
Support for unicode builds.
Aids in achievement of platform independence.
void * HANDLE