ReactOS  0.4.13-dev-100-gc8611ae
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 
16 BOOL
18  BOOL SystemTime)
19 {
20  HANDLE hToken;
21  DWORD PrevSize;
22  TOKEN_PRIVILEGES priv, previouspriv;
23  BOOL Ret = FALSE;
24 
25  /*
26  * Enable the SeSystemtimePrivilege privilege
27  */
28 
31  &hToken))
32  {
33  priv.PrivilegeCount = 1;
35 
38  &priv.Privileges[0].Luid))
39  {
40  if (AdjustTokenPrivileges(hToken,
41  FALSE,
42  &priv,
43  sizeof(previouspriv),
44  &previouspriv,
45  &PrevSize) &&
47  {
48  /*
49  * We successfully enabled it, we're permitted to change the time.
50  * Check the second parameter for SystemTime and if TRUE set System Time.
51  * Otherwise, if FALSE set the Local Time.
52  * Call SetLocalTime twice to ensure correct results.
53  */
54  if (SystemTime)
55  {
56  Ret = SetSystemTime(lpSystemTime);
57  }
58  else
59  {
60  Ret = SetLocalTime(lpSystemTime) &&
61  SetLocalTime(lpSystemTime);
62  }
63 
64  /*
65  * For the sake of security, restore the previous status again
66  */
67  if (previouspriv.PrivilegeCount > 0)
68  {
69  AdjustTokenPrivileges(hToken,
70  FALSE,
71  &previouspriv,
72  0,
73  NULL,
74  0);
75  }
76  }
77  }
78  CloseHandle(hToken);
79  }
80 
81  return Ret;
82 }
83 
84 
85 static VOID
87 {
89 
92  &Time) == GDT_VALID &&
96  (WPARAM)&Time,
97  0))
98  {
99  /* Set Local Time with SystemTime = FALSE */
101 
105 
108  MCCM_RESET,
109  (WPARAM)&Time,
110  0);
111 
112  /* Broadcast the time change message */
115  0,
116  0);
117  }
118 }
119 
120 
121 static VOID
123 {
124  TIME_ZONE_INFORMATION TimeZoneInfo;
125  WCHAR TimeZoneString[128];
126  WCHAR TimeZoneText[128];
127  WCHAR TimeZoneName[128];
128  DWORD TimeZoneId;
129 
130  TimeZoneId = GetTimeZoneInformation(&TimeZoneInfo);
131 
132  LoadStringW(hApplet, IDS_TIMEZONETEXT, TimeZoneText, 128);
133 
134  switch (TimeZoneId)
135  {
138  wcscpy(TimeZoneName, TimeZoneInfo.StandardName);
139  break;
140 
142  wcscpy(TimeZoneName, TimeZoneInfo.DaylightName);
143  break;
144 
146  default:
147  LoadStringW(hApplet, IDS_TIMEZONEINVALID, TimeZoneName, 128);
148  break;
149  }
150 
151  wsprintfW(TimeZoneString, TimeZoneText, TimeZoneName);
152  SendDlgItemMessageW(hwnd, IDC_TIMEZONE, WM_SETTEXT, 0, (LPARAM)TimeZoneString);
153 }
154 
155 
156 static VOID
158 {
159  SYSTEMTIME LocalDate = {0};
160  WCHAR szBuf[64];
161  INT i;
162  UINT Month;
163 
164  GetLocalTime(&LocalDate);
165 
166  SendMessageW(hCombo,
168  0,
169  0);
170 
171  for (Month = 1;
172  Month <= 13;
173  Month++)
174  {
176  ((Month < 13) ? LOCALE_SMONTHNAME1 + Month - 1 : LOCALE_SMONTHNAME13),
177  szBuf,
178  sizeof(szBuf) / sizeof(szBuf[0]));
179  if (i > 1)
180  {
181  i = (INT)SendMessageW(hCombo,
182  CB_ADDSTRING,
183  0,
184  (LPARAM)szBuf);
185  if (i != CB_ERR)
186  {
187  SendMessageW(hCombo,
189  (WPARAM)i,
190  Month);
191 
192  if (Month == (UINT)LocalDate.wMonth)
193  {
194  SendMessageW(hCombo,
195  CB_SETCURSEL,
196  (WPARAM)i,
197  0);
198  }
199  }
200  }
201  }
202 }
203 
204 
205 static WORD
207 {
208  INT i;
209  WORD Ret = (WORD)-1;
210 
211  i = (INT)SendMessageW(hCombo,
212  CB_GETCURSEL,
213  0,
214  0);
215  if (i != CB_ERR)
216  {
217  i = (INT)SendMessageW(hCombo,
219  (WPARAM)i,
220  0);
221 
222  if (i >= 1 && i <= 13)
223  Ret = (WORD)i;
224  }
225 
226  return Ret;
227 }
228 
229 
230 static VOID
232  WORD Day,
233  WORD Month,
234  WORD Year)
235 {
236  SendMessageW(hMonthCal,
237  MCCM_SETDATE,
238  MAKEWPARAM(Day,
239  Month),
240  MAKELPARAM(Year,
241  0));
242 }
243 
244 static VOID
246  PNMMCCAUTOUPDATE lpAutoUpdate)
247 {
248  UNREFERENCED_PARAMETER(lpAutoUpdate);
249 
250  /* Update the controls */
252  IDC_MONTHCB));
253 }
254 
255 
256 static INT_PTR CALLBACK
258  UINT uMsg,
259  WPARAM wParam,
260  LPARAM lParam)
261 {
262  switch (uMsg)
263  {
264  case WM_KEYDOWN:
265  /* Stop the timer when the user is about to change the time */
266  if ((wParam != VK_LEFT) & (wParam != VK_RIGHT))
268  break;
269  }
270 
271  return CallWindowProcW(pOldWndProc, hwnd, uMsg, wParam, lParam);
272 }
273 
274 /* Property page dialog callback */
277  UINT uMsg,
278  WPARAM wParam,
279  LPARAM lParam)
280 {
281  SYSTEMTIME st;
282  GetLocalTime(&st);
283 
284  switch (uMsg)
285  {
286  case WM_INITDIALOG:
288  IDC_MONTHCB));
289 
290  SetTimer(hwndDlg, ID_TIMER, 1000, NULL);
291 
292  /* Set range and current year */
293  SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_SETRANGE, 0, MAKELONG ((short) 9999, (short) 1900));
294  SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_SETPOS, 0, MAKELONG( (short) st.wYear, 0));
295 
297  break;
298 
299  case WM_TIMER:
301  break;
302 
303  case WM_COMMAND:
304  switch (LOWORD(wParam))
305  {
306  case IDC_MONTHCB:
307  if (HIWORD(wParam) == CBN_SELCHANGE)
308  {
311  (WORD) -1,
313  (WORD) -1);
314  }
315  break;
316  }
317  break;
318 
319  case WM_CTLCOLORSTATIC:
320  if ((HWND)lParam == GetDlgItem(hwndDlg, IDC_YEARTEXT))
322  break;
323 
324  case WM_NOTIFY:
325  {
326  LPNMHDR lpnm = (LPNMHDR)lParam;
327 
328  switch (lpnm->idFrom)
329  {
330  case IDC_YEAR:
331  switch (lpnm->code)
332  {
333  case UDN_DELTAPOS:
334  {
335  SHORT wYear;
336  LPNMUPDOWN updown = (LPNMUPDOWN)lpnm;
337  wYear = (SHORT)SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_GETPOS, 0, 0);
338  /* Enable the 'Apply' button */
339  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
342  (WORD) -1,
343  (WORD) -1,
344  (WORD) (wYear + updown->iDelta));
345  }
346  break;
347  }
348  break;
349 
350  case IDC_TIMEPICKER:
351  switch (lpnm->code)
352  {
353  case DTN_DATETIMECHANGE:
354  /* Stop the timer */
355  KillTimer(hwndDlg, ID_TIMER);
356 
357  /* Tell the clock to stop ticking */
359  0, 0);
360 
361  /* Enable the 'Apply' button */
362  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
363  break;
364  }
365  break;
366 
367  case IDC_MONTHCALENDAR:
368  switch (lpnm->code)
369  {
370  case MCCN_SELCHANGE:
371  /* Enable the 'Apply' button */
372  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
373  break;
374 
375  case MCCN_AUTOUPDATE:
376  AutoUpdateMonthCal(hwndDlg,
377  (PNMMCCAUTOUPDATE)lpnm);
378  break;
379  }
380  break;
381 
382  default:
383  switch (lpnm->code)
384  {
385  case PSN_SETACTIVE:
386  SetTimeZoneName(hwndDlg);
387  break;
388 
389  case PSN_APPLY:
390  SetLocalSystemTime(hwndDlg);
391  SetTimer(hwndDlg, ID_TIMER, 1000, NULL);
392 
393  /* Tell the clock to start ticking */
395  0, 0);
396  return TRUE;
397  }
398  break;
399  }
400  }
401  break;
402 
403  case WM_TIMECHANGE:
404  /* FIXME: We don't get this message as we're not a top-level window... */
405  SendMessageW(GetDlgItem(hwndDlg,
407  MCCM_RESET,
408  0,
409  0);
410  break;
411 
412  case WM_DESTROY:
413  KillTimer(hwndDlg, ID_TIMER);
414  break;
415  }
416 
417  return FALSE;
418 }
int WINAPIV wsprintfW(_Out_ LPWSTR, _In_ _Printf_format_string_ LPCWSTR,...)
#define CB_SETITEMDATA
Definition: winuser.h:1927
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation)
Definition: timezone.c:262
#define MCCM_SETDATE
Definition: timedate.h:98
#define LONG_PTR
Definition: treelist.c:79
#define TRUE
Definition: types.h:120
#define MAKEWPARAM(l, h)
Definition: winuser.h:3916
#define UDM_GETPOS
Definition: commctrl.h:2116
#define CloseHandle
Definition: compat.h:398
BOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpPrivilegeName, PLUID lpLuid)
Definition: misc.c:782
#define ERROR_SUCCESS
Definition: deptool.c:10
WORD wMonth
Definition: winbase.h:871
BOOL WINAPI SetSystemTime(IN CONST SYSTEMTIME *lpSystemTime)
Definition: time.c:402
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
static WNDPROC pOldWndProc
Definition: dateandtime.c:14
#define TIME_ZONE_ID_UNKNOWN
Definition: rtltypes.h:250
#define MCCM_RESET
Definition: timedate.h:100
#define LOCALE_USER_DEFAULT
#define CB_GETITEMDATA
Definition: winuser.h:1911
#define WM_CTLCOLORSTATIC
Definition: winuser.h:1748
struct _NM_UPDOWN * LPNMUPDOWN
#define MAKELPARAM(l, h)
Definition: winuser.h:3915
#define LOCALE_SMONTHNAME13
Definition: winnls.h:103
#define INT
Definition: polytest.cpp:20
#define CALLBACK
Definition: compat.h:27
#define TIME_ZONE_ID_INVALID
Definition: winbase.h:283
$ULONG PrivilegeCount
Definition: setypes.h:969
#define VK_LEFT
Definition: winuser.h:2178
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
HINSTANCE hApplet
Definition: access.c:17
UINT_PTR WPARAM
Definition: windef.h:207
#define PSN_APPLY
Definition: prsht.h:117
int32_t INT_PTR
Definition: typedefs.h:62
#define PSN_SETACTIVE
Definition: prsht.h:115
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
#define COLOR_WINDOW
Definition: winuser.h:908
int32_t INT
Definition: typedefs.h:56
#define CB_GETCURSEL
Definition: winuser.h:1904
WPARAM wParam
Definition: combotst.c:138
#define IDC_TIMEPICKER
Definition: resource.h:11
#define DWLP_MSGRESULT
Definition: winuser.h:864
BOOL SystemSetTime(LPSYSTEMTIME lpSystemTime, BOOL SystemTime)
Definition: dateandtime.c:17
struct tagNMHDR * LPNMHDR
UINT code
Definition: winuser.h:3112
INT WINAPI GetLocaleInfoW(LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len)
Definition: lang.c:1098
static VOID SetTimeZoneName(HWND hwnd)
Definition: dateandtime.c:122
#define IDC_YEARTEXT
Definition: resource.h:13
WORD wYear
Definition: winbase.h:870
#define SE_PRIVILEGE_ENABLED
Definition: setypes.h:63
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
#define LOCALE_SMONTHNAME1
Definition: winnls.h:91
unsigned int BOOL
Definition: ntddk_ex.h:94
short SHORT
Definition: pedump.c:59
UINT_PTR idFrom
Definition: winuser.h:3111
#define IDC_MONTHCB
Definition: resource.h:6
#define SE_SYSTEMTIME_NAME
Definition: winnt_old.h:376
#define CB_ADDSTRING
Definition: winuser.h:1897
#define MAKELONG(a, b)
Definition: typedefs.h:248
smooth NULL
Definition: ftsmooth.c:416
#define WM_KEYDOWN
Definition: winuser.h:1691
LONG_PTR LPARAM
Definition: windef.h:208
VOID WINAPI GetLocalTime(OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:276
#define UDM_SETRANGE
Definition: commctrl.h:2113
#define WM_SETTEXT
Definition: winuser.h:1599
#define CB_RESETCONTENT
Definition: winuser.h:1920
#define CB_ERR
Definition: winuser.h:2389
UINT_PTR WINAPI SetTimer(_In_opt_ HWND, _In_ UINT_PTR, _In_ UINT, _In_opt_ TIMERPROC)
#define WM_DESTROY
Definition: winuser.h:1591
#define TIME_ZONE_ID_STANDARD
Definition: rtltypes.h:251
#define IDC_CLOCKWND
Definition: resource.h:9
#define IDS_TIMEZONETEXT
Definition: resource.h:32
#define TOKEN_QUERY
Definition: setypes.h:874
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define PropSheet_Changed(d, w)
Definition: prsht.h:326
#define WM_TIMER
Definition: winuser.h:1718
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1168
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
LRESULT WINAPI SendDlgItemMessageW(_In_ HWND, _In_ int, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
INT_PTR CALLBACK DateTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: dateandtime.c:276
#define MCCN_SELCHANGE
Definition: timedate.h:103
#define PSNRET_NOERROR
Definition: prsht.h:129
static VOID SetLocalSystemTime(HWND hwnd)
Definition: dateandtime.c:86
#define DTM_SETSYSTEMTIME
Definition: commctrl.h:4303
BOOL WINAPI KillTimer(_In_opt_ HWND, _In_ UINT_PTR)
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static VOID ChangeMonthCalDate(HWND hMonthCal, WORD Day, WORD Month, WORD Year)
Definition: dateandtime.c:231
#define UDN_DELTAPOS
Definition: commctrl.h:2141
#define ID_TIMER
Definition: resource.h:23
#define DateTime_GetSystemtime(hdp, pst)
Definition: commctrl.h:4302
#define CB_SETCURSEL
Definition: winuser.h:1922
#define IDC_YEAR
Definition: resource.h:12
#define WM_COMMAND
Definition: winuser.h:1716
#define GWLP_WNDPROC
Definition: treelist.c:66
static WORD GetCBSelectedMonth(HWND hCombo)
Definition: dateandtime.c:206
BOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength)
Definition: security.c:376
#define VK_RIGHT
Definition: winuser.h:2180
LRESULT(CALLBACK * WNDPROC)(HWND, UINT, WPARAM, LPARAM)
Definition: winuser.h:2859
int iDelta
Definition: commctrl.h:2138
HWND WINAPI GetParent(_In_ HWND)
WCHAR DaylightName[32]
Definition: winbase.h:1163
#define GDT_VALID
Definition: commctrl.h:4432
#define IDC_MONTHCALENDAR
Definition: resource.h:8
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
#define UDM_SETPOS
Definition: commctrl.h:2115
unsigned int UINT
Definition: ndis.h:50
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:296
#define CBN_SELCHANGE
Definition: winuser.h:1940
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]
Definition: setypes.h:970
#define MCCM_GETDATE
Definition: timedate.h:99
static VOID FillMonthsComboBox(HWND hCombo)
Definition: dateandtime.c:157
LRESULT WINAPI CallWindowProcW(_In_ WNDPROC, _In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
#define HIWORD(l)
Definition: typedefs.h:246
#define IDC_TIMEZONE
Definition: resource.h:10
#define CLM_STOPCLOCK
Definition: timedate.h:63
#define SetWindowLongPtrW
Definition: winuser.h:5215
#define CLM_STARTCLOCK
Definition: timedate.h:64
HBRUSH WINAPI GetSysColorBrush(_In_ int)
BOOL WINAPI SetLocalTime(IN CONST SYSTEMTIME *lpSystemTime)
Definition: time.c:346
#define DTN_DATETIMECHANGE
Definition: commctrl.h:4338
static VOID AutoUpdateMonthCal(HWND hwndDlg, PNMMCCAUTOUPDATE lpAutoUpdate)
Definition: dateandtime.c:245
#define HWND_BROADCAST
Definition: winuser.h:1190
#define TOKEN_ADJUST_PRIVILEGES
Definition: setypes.h:876
#define TIME_ZONE_ID_DAYLIGHT
Definition: rtltypes.h:252
#define WM_INITDIALOG
Definition: winuser.h:1715
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
#define IDS_TIMEZONEINVALID
Definition: resource.h:33
WCHAR StandardName[32]
Definition: winbase.h:1160
static PLARGE_INTEGER Time
Definition: time.c:105
#define WM_NOTIFY
Definition: richedit.h:61
#define MCCN_AUTOUPDATE
Definition: timedate.h:114
static INT_PTR CALLBACK DTPProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: dateandtime.c:257
#define WM_TIMECHANGE
Definition: winuser.h:1616