ReactOS 0.4.15-dev-7994-gb388cb6
dateandtime.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Timedate Control Panel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/cpl/timedate/dateandtime.c
5 * PURPOSE: Date & Time property page
6 * COPYRIGHT: Copyright 2004-2007 Eric Kohl
7 * Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
8 * Copyright 2006 Thomas Weidenmueller <w3seek@reactos.com>
9 *
10 */
11
12#include "timedate.h"
13
15
16BOOL
18{
19 HANDLE hToken;
20 DWORD PrevSize;
21 TOKEN_PRIVILEGES priv, previouspriv;
22 BOOL Ret = FALSE;
23
24 /*
25 * Enable the SeSystemtimePrivilege privilege
26 */
27
30 &hToken))
31 {
32 priv.PrivilegeCount = 1;
34
37 &priv.Privileges[0].Luid))
38 {
39 if (AdjustTokenPrivileges(hToken,
40 FALSE,
41 &priv,
42 sizeof(previouspriv),
43 &previouspriv,
44 &PrevSize) &&
46 {
47 /*
48 * We successfully enabled it, we're permitted to change the time.
49 * Call SetLocalTime twice to ensure correct results.
50 */
51 Ret = SetLocalTime(lpSystemTime) &&
52 SetLocalTime(lpSystemTime);
53
54 /*
55 * For the sake of security, restore the previous status again
56 */
57 if (previouspriv.PrivilegeCount > 0)
58 {
60 FALSE,
61 &previouspriv,
62 0,
63 NULL,
64 0);
65 }
66 }
67 }
68 CloseHandle(hToken);
69 }
70
71 return Ret;
72}
73
74
75static VOID
77{
79
82 &Time) == GDT_VALID &&
86 (WPARAM)&Time,
87 0))
88 {
90
94
98 (WPARAM)&Time,
99 0);
100
101 /* Broadcast the time change message */
104 0,
105 0);
106 }
107}
108
109
110static VOID
112{
113 TIME_ZONE_INFORMATION TimeZoneInfo;
114 WCHAR TimeZoneString[128];
115 WCHAR TimeZoneText[128];
116 WCHAR TimeZoneName[128];
117 DWORD TimeZoneId;
118
119 TimeZoneId = GetTimeZoneInformation(&TimeZoneInfo);
120
121 LoadStringW(hApplet, IDS_TIMEZONETEXT, TimeZoneText, 128);
122
123 switch (TimeZoneId)
124 {
127 wcscpy(TimeZoneName, TimeZoneInfo.StandardName);
128 break;
129
131 wcscpy(TimeZoneName, TimeZoneInfo.DaylightName);
132 break;
133
135 default:
136 LoadStringW(hApplet, IDS_TIMEZONEINVALID, TimeZoneName, 128);
137 break;
138 }
139
140 wsprintfW(TimeZoneString, TimeZoneText, TimeZoneName);
142}
143
144
145static VOID
147{
148 SYSTEMTIME LocalDate = {0};
149 WCHAR szBuf[64];
150 INT i;
151 UINT Month;
152
153 GetLocalTime(&LocalDate);
154
155 SendMessageW(hCombo,
157 0,
158 0);
159
160 for (Month = 1;
161 Month <= 13;
162 Month++)
163 {
165 ((Month < 13) ? LOCALE_SMONTHNAME1 + Month - 1 : LOCALE_SMONTHNAME13),
166 szBuf,
167 sizeof(szBuf) / sizeof(szBuf[0]));
168 if (i > 1)
169 {
170 i = (INT)SendMessageW(hCombo,
172 0,
173 (LPARAM)szBuf);
174 if (i != CB_ERR)
175 {
176 SendMessageW(hCombo,
178 (WPARAM)i,
179 Month);
180
181 if (Month == (UINT)LocalDate.wMonth)
182 {
183 SendMessageW(hCombo,
185 (WPARAM)i,
186 0);
187 }
188 }
189 }
190 }
191}
192
193
194static WORD
196{
197 INT i;
198 WORD Ret = (WORD)-1;
199
200 i = (INT)SendMessageW(hCombo,
202 0,
203 0);
204 if (i != CB_ERR)
205 {
206 i = (INT)SendMessageW(hCombo,
208 (WPARAM)i,
209 0);
210
211 if (i >= 1 && i <= 13)
212 Ret = (WORD)i;
213 }
214
215 return Ret;
216}
217
218
219static VOID
221 WORD Day,
222 WORD Month,
223 WORD Year)
224{
225 SendMessageW(hMonthCal,
227 MAKEWPARAM(Day,
228 Month),
229 MAKELPARAM(Year,
230 0));
231}
232
233static VOID
235 PNMMCCAUTOUPDATE lpAutoUpdate)
236{
237 UNREFERENCED_PARAMETER(lpAutoUpdate);
238
239 /* Update the controls */
241 IDC_MONTHCB));
242}
243
244
245static INT_PTR CALLBACK
247 UINT uMsg,
250{
251 switch (uMsg)
252 {
253 case WM_KEYDOWN:
254 /* Stop the timer when the user is about to change the time */
255 if ((wParam != VK_LEFT) & (wParam != VK_RIGHT))
257 break;
258 }
259
261}
262
263/* Property page dialog callback */
266 UINT uMsg,
269{
270 SYSTEMTIME st;
271 GetLocalTime(&st);
272
273 switch (uMsg)
274 {
275 case WM_INITDIALOG:
277 IDC_MONTHCB));
278
279 /* Set range and current year */
280 SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_SETRANGE, 0, MAKELONG ((short) 9999, (short) 1900));
281 SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_SETPOS, 0, MAKELONG( (short) st.wYear, 0));
282
284
285 SetTimer(hwndDlg, ID_TIMER, 1000 - st.wMilliseconds, NULL);
286 break;
287
288 case WM_TIMER:
290
291 // Reset timeout.
292 SetTimer(hwndDlg, ID_TIMER, 1000 - st.wMilliseconds, NULL);
293 break;
294
295 case WM_COMMAND:
296 switch (LOWORD(wParam))
297 {
298 case IDC_MONTHCB:
300 {
303 (WORD) -1,
305 (WORD) -1);
306 }
307 break;
308 }
309 break;
310
312 if ((HWND)lParam == GetDlgItem(hwndDlg, IDC_YEARTEXT))
314 break;
315
316 case WM_NOTIFY:
317 {
318 LPNMHDR lpnm = (LPNMHDR)lParam;
319
320 switch (lpnm->idFrom)
321 {
322 case IDC_YEAR:
323 switch (lpnm->code)
324 {
325 case UDN_DELTAPOS:
326 {
327 SHORT wYear;
328 LPNMUPDOWN updown = (LPNMUPDOWN)lpnm;
329 wYear = (SHORT)SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_GETPOS, 0, 0);
330 /* Enable the 'Apply' button */
331 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
334 (WORD) -1,
335 (WORD) -1,
336 (WORD) (wYear + updown->iDelta));
337 }
338 break;
339 }
340 break;
341
342 case IDC_TIMEPICKER:
343 switch (lpnm->code)
344 {
346 /* Stop the timer */
347 KillTimer(hwndDlg, ID_TIMER);
348
349 /* Tell the clock to stop ticking */
351 0, 0);
352
353 // TODO: Set the clock to the input time.
354
355 /* Enable the 'Apply' button */
356 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
357 break;
358 }
359 break;
360
362 switch (lpnm->code)
363 {
364 case MCCN_SELCHANGE:
365 /* Enable the 'Apply' button */
366 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
367 break;
368
369 case MCCN_AUTOUPDATE:
370 AutoUpdateMonthCal(hwndDlg,
371 (PNMMCCAUTOUPDATE)lpnm);
372 break;
373 }
374 break;
375
376 default:
377 switch (lpnm->code)
378 {
379 case PSN_SETACTIVE:
380 SetTimeZoneName(hwndDlg);
381 break;
382
383 case PSN_APPLY:
384 SetLocalSystemTime(hwndDlg);
385
386 /* Tell the clock to start ticking */
388 0, 0);
389
390 SetTimer(hwndDlg, ID_TIMER, 1000 - st.wMilliseconds, NULL);
391 return TRUE;
392 }
393 break;
394 }
395 }
396 break;
397
398 case WM_TIMECHANGE:
399 /* FIXME: We don't get this message as we're not a top-level window... */
400 SendMessageW(GetDlgItem(hwndDlg,
403 0,
404 0);
405 break;
406
407 case WM_DESTROY:
408 KillTimer(hwndDlg, ID_TIMER);
409 break;
410 }
411
412 return FALSE;
413}
#define ID_TIMER
Definition: resource.h:23
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
static INT_PTR CALLBACK DTPProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: dateandtime.c:246
static WORD GetCBSelectedMonth(HWND hCombo)
Definition: dateandtime.c:195
static VOID SetLocalSystemTime(HWND hwnd)
Definition: dateandtime.c:76
static VOID SetTimeZoneName(HWND hwnd)
Definition: dateandtime.c:111
static VOID FillMonthsComboBox(HWND hCombo)
Definition: dateandtime.c:146
static WNDPROC pOldWndProc
Definition: dateandtime.c:14
static VOID AutoUpdateMonthCal(HWND hwndDlg, PNMMCCAUTOUPDATE lpAutoUpdate)
Definition: dateandtime.c:234
INT_PTR CALLBACK DateTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: dateandtime.c:265
static VOID ChangeMonthCalDate(HWND hMonthCal, WORD Day, WORD Month, WORD Year)
Definition: dateandtime.c:220
BOOL SystemSetLocalTime(LPSYSTEMTIME lpSystemTime)
Definition: dateandtime.c:17
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
HINSTANCE hApplet
Definition: access.c:17
#define IDC_YEAR
Definition: resource.h:12
#define IDS_TIMEZONEINVALID
Definition: resource.h:33
#define IDC_CLOCKWND
Definition: resource.h:9
#define IDC_TIMEZONE
Definition: resource.h:10
#define IDS_TIMEZONETEXT
Definition: resource.h:32
#define IDC_MONTHCALENDAR
Definition: resource.h:8
#define IDC_MONTHCB
Definition: resource.h:6
#define IDC_YEARTEXT
Definition: resource.h:13
#define IDC_TIMEPICKER
Definition: resource.h:11
BOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpPrivilegeName, PLUID lpLuid)
Definition: misc.c:782
BOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength)
Definition: security.c:374
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:294
#define CloseHandle
Definition: compat.h:739
#define GetCurrentProcess()
Definition: compat.h:759
#define CALLBACK
Definition: compat.h:35
BOOL WINAPI SetLocalTime(IN CONST SYSTEMTIME *lpSystemTime)
Definition: time.c:356
VOID WINAPI GetLocalTime(OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:286
DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation)
Definition: timezone.c:262
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
INT WINAPI GetLocaleInfoW(LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len)
Definition: lang.c:1108
static PLARGE_INTEGER Time
Definition: time.c:105
unsigned int UINT
Definition: ndis.h:50
#define TIME_ZONE_ID_UNKNOWN
Definition: rtltypes.h:252
#define TIME_ZONE_ID_STANDARD
Definition: rtltypes.h:253
#define TIME_ZONE_ID_DAYLIGHT
Definition: rtltypes.h:254
#define LOCALE_USER_DEFAULT
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define LOWORD(l)
Definition: pedump.c:82
short SHORT
Definition: pedump.c:59
#define INT
Definition: polytest.cpp:20
#define PropSheet_Changed(d, w)
Definition: prsht.h:344
#define PSN_APPLY
Definition: prsht.h:117
#define PSNRET_NOERROR
Definition: prsht.h:129
#define PSN_SETACTIVE
Definition: prsht.h:115
#define UDM_SETPOS
Definition: commctrl.h:2143
#define UDM_SETRANGE
Definition: commctrl.h:2141
struct _NM_UPDOWN * LPNMUPDOWN
#define DTN_DATETIMECHANGE
Definition: commctrl.h:4366
#define DTM_SETSYSTEMTIME
Definition: commctrl.h:4331
#define GDT_VALID
Definition: commctrl.h:4460
#define UDM_GETPOS
Definition: commctrl.h:2144
#define UDN_DELTAPOS
Definition: commctrl.h:2169
#define DateTime_GetSystemtime(hdp, pst)
Definition: commctrl.h:4330
#define WM_NOTIFY
Definition: richedit.h:61
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
int iDelta
Definition: commctrl.h:2166
WORD wYear
Definition: winbase.h:905
WORD wMilliseconds
Definition: winbase.h:912
WORD wMonth
Definition: winbase.h:906
WCHAR DaylightName[32]
Definition: winbase.h:1210
WCHAR StandardName[32]
Definition: winbase.h:1207
$ULONG PrivilegeCount
Definition: setypes.h:1023
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]
Definition: setypes.h:1024
UINT_PTR idFrom
Definition: winuser.h:3158
UINT code
Definition: winuser.h:3159
#define MCCM_GETDATE
Definition: timedate.h:82
#define MCCN_AUTOUPDATE
Definition: timedate.h:97
#define MCCM_SETDATE
Definition: timedate.h:81
#define CLM_STOPCLOCK
Definition: timedate.h:73
#define MCCN_SELCHANGE
Definition: timedate.h:86
#define CLM_STARTCLOCK
Definition: timedate.h:74
#define MCCM_RESET
Definition: timedate.h:83
#define LONG_PTR
Definition: treelist.c:79
#define GWLP_WNDPROC
Definition: treelist.c:66
int32_t INT_PTR
Definition: typedefs.h:64
int32_t INT
Definition: typedefs.h:58
#define MAKELONG(a, b)
Definition: typedefs.h:249
#define HIWORD(l)
Definition: typedefs.h:247
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define TIME_ZONE_ID_INVALID
Definition: winbase.h:286
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
LONG_PTR LPARAM
Definition: windef.h:208
UINT_PTR WPARAM
Definition: windef.h:207
#define LOCALE_SMONTHNAME13
Definition: winnls.h:103
#define LOCALE_SMONTHNAME1
Definition: winnls.h:91
#define SE_SYSTEMTIME_NAME
Definition: winnt_old.h:377
#define CB_SETITEMDATA
Definition: winuser.h:1966
#define MAKEWPARAM(l, h)
Definition: winuser.h:4009
#define WM_CTLCOLORSTATIC
Definition: winuser.h:1772
#define MAKELPARAM(l, h)
Definition: winuser.h:4008
#define COLOR_WINDOW
Definition: winuser.h:918
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
int WINAPIV wsprintfW(_Out_ LPWSTR, _In_ _Printf_format_string_ LPCWSTR,...)
HBRUSH WINAPI GetSysColorBrush(_In_ int)
#define HWND_BROADCAST
Definition: winuser.h:1204
#define WM_COMMAND
Definition: winuser.h:1740
#define CB_ERR
Definition: winuser.h:2435
#define CB_SETCURSEL
Definition: winuser.h:1961
#define CB_RESETCONTENT
Definition: winuser.h:1959
#define WM_INITDIALOG
Definition: winuser.h:1739
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
UINT_PTR WINAPI SetTimer(_In_opt_ HWND, _In_ UINT_PTR, _In_ UINT, _In_opt_ TIMERPROC)
#define CBN_SELCHANGE
Definition: winuser.h:1979
LRESULT WINAPI SendDlgItemMessageW(_In_ HWND, _In_ int, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
#define WM_TIMECHANGE
Definition: winuser.h:1634
#define WM_SETTEXT
Definition: winuser.h:1617
#define WM_TIMER
Definition: winuser.h:1742
#define CB_ADDSTRING
Definition: winuser.h:1936
struct tagNMHDR * LPNMHDR
#define CB_GETITEMDATA
Definition: winuser.h:1950
HWND WINAPI GetParent(_In_ HWND)
#define VK_LEFT
Definition: winuser.h:2224
#define VK_RIGHT
Definition: winuser.h:2226
#define DWLP_MSGRESULT
Definition: winuser.h:870
#define WM_DESTROY
Definition: winuser.h:1609
#define WM_KEYDOWN
Definition: winuser.h:1715
LRESULT(CALLBACK * WNDPROC)(HWND, UINT, WPARAM, LPARAM)
Definition: winuser.h:2906
BOOL WINAPI KillTimer(_In_opt_ HWND, _In_ UINT_PTR)
#define CB_GETCURSEL
Definition: winuser.h:1943
#define SetWindowLongPtrW
Definition: winuser.h:5346
LRESULT WINAPI CallWindowProcW(_In_ WNDPROC, _In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
#define TOKEN_ADJUST_PRIVILEGES
Definition: setypes.h:930
#define TOKEN_QUERY
Definition: setypes.h:928
#define SE_PRIVILEGE_ENABLED
Definition: setypes.h:63
__wchar_t WCHAR
Definition: xmlstorage.h:180