ReactOS  0.4.14-dev-606-g14ebc0b
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 {
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  {
59  AdjustTokenPrivileges(hToken,
60  FALSE,
61  &previouspriv,
62  0,
63  NULL,
64  0);
65  }
66  }
67  }
68  CloseHandle(hToken);
69  }
70 
71  return Ret;
72 }
73 
74 
75 static VOID
77 {
79 
82  &Time) == GDT_VALID &&
86  (WPARAM)&Time,
87  0))
88  {
90 
94 
97  MCCM_RESET,
98  (WPARAM)&Time,
99  0);
100 
101  /* Broadcast the time change message */
104  0,
105  0);
106  }
107 }
108 
109 
110 static 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);
141  SendDlgItemMessageW(hwnd, IDC_TIMEZONE, WM_SETTEXT, 0, (LPARAM)TimeZoneString);
142 }
143 
144 
145 static 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,
171  CB_ADDSTRING,
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,
184  CB_SETCURSEL,
185  (WPARAM)i,
186  0);
187  }
188  }
189  }
190  }
191 }
192 
193 
194 static WORD
196 {
197  INT i;
198  WORD Ret = (WORD)-1;
199 
200  i = (INT)SendMessageW(hCombo,
201  CB_GETCURSEL,
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 
219 static VOID
221  WORD Day,
222  WORD Month,
223  WORD Year)
224 {
225  SendMessageW(hMonthCal,
226  MCCM_SETDATE,
227  MAKEWPARAM(Day,
228  Month),
229  MAKELPARAM(Year,
230  0));
231 }
232 
233 static VOID
235  PNMMCCAUTOUPDATE lpAutoUpdate)
236 {
237  UNREFERENCED_PARAMETER(lpAutoUpdate);
238 
239  /* Update the controls */
241  IDC_MONTHCB));
242 }
243 
244 
245 static INT_PTR CALLBACK
247  UINT uMsg,
248  WPARAM wParam,
249  LPARAM lParam)
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 
260  return CallWindowProcW(pOldWndProc, hwnd, uMsg, wParam, lParam);
261 }
262 
263 /* Property page dialog callback */
266  UINT uMsg,
267  WPARAM wParam,
268  LPARAM lParam)
269 {
270  SYSTEMTIME st;
271  GetLocalTime(&st);
272 
273  switch (uMsg)
274  {
275  case WM_INITDIALOG:
277  IDC_MONTHCB));
278 
279  SetTimer(hwndDlg, ID_TIMER, 1000, NULL);
280 
281  /* Set range and current year */
282  SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_SETRANGE, 0, MAKELONG ((short) 9999, (short) 1900));
283  SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_SETPOS, 0, MAKELONG( (short) st.wYear, 0));
284 
286  break;
287 
288  case WM_TIMER:
290  break;
291 
292  case WM_COMMAND:
293  switch (LOWORD(wParam))
294  {
295  case IDC_MONTHCB:
296  if (HIWORD(wParam) == CBN_SELCHANGE)
297  {
300  (WORD) -1,
302  (WORD) -1);
303  }
304  break;
305  }
306  break;
307 
308  case WM_CTLCOLORSTATIC:
309  if ((HWND)lParam == GetDlgItem(hwndDlg, IDC_YEARTEXT))
311  break;
312 
313  case WM_NOTIFY:
314  {
315  LPNMHDR lpnm = (LPNMHDR)lParam;
316 
317  switch (lpnm->idFrom)
318  {
319  case IDC_YEAR:
320  switch (lpnm->code)
321  {
322  case UDN_DELTAPOS:
323  {
324  SHORT wYear;
325  LPNMUPDOWN updown = (LPNMUPDOWN)lpnm;
326  wYear = (SHORT)SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_GETPOS, 0, 0);
327  /* Enable the 'Apply' button */
328  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
331  (WORD) -1,
332  (WORD) -1,
333  (WORD) (wYear + updown->iDelta));
334  }
335  break;
336  }
337  break;
338 
339  case IDC_TIMEPICKER:
340  switch (lpnm->code)
341  {
342  case DTN_DATETIMECHANGE:
343  /* Stop the timer */
344  KillTimer(hwndDlg, ID_TIMER);
345 
346  /* Tell the clock to stop ticking */
348  0, 0);
349 
350  /* Enable the 'Apply' button */
351  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
352  break;
353  }
354  break;
355 
356  case IDC_MONTHCALENDAR:
357  switch (lpnm->code)
358  {
359  case MCCN_SELCHANGE:
360  /* Enable the 'Apply' button */
361  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
362  break;
363 
364  case MCCN_AUTOUPDATE:
365  AutoUpdateMonthCal(hwndDlg,
366  (PNMMCCAUTOUPDATE)lpnm);
367  break;
368  }
369  break;
370 
371  default:
372  switch (lpnm->code)
373  {
374  case PSN_SETACTIVE:
375  SetTimeZoneName(hwndDlg);
376  break;
377 
378  case PSN_APPLY:
379  SetLocalSystemTime(hwndDlg);
380  SetTimer(hwndDlg, ID_TIMER, 1000, NULL);
381 
382  /* Tell the clock to start ticking */
384  0, 0);
385  return TRUE;
386  }
387  break;
388  }
389  }
390  break;
391 
392  case WM_TIMECHANGE:
393  /* FIXME: We don't get this message as we're not a top-level window... */
394  SendMessageW(GetDlgItem(hwndDlg,
396  MCCM_RESET,
397  0,
398  0);
399  break;
400 
401  case WM_DESTROY:
402  KillTimer(hwndDlg, ID_TIMER);
403  break;
404  }
405 
406  return FALSE;
407 }
int WINAPIV wsprintfW(_Out_ LPWSTR, _In_ _Printf_format_string_ LPCWSTR,...)
#define CB_SETITEMDATA
Definition: winuser.h:1948
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:70
#define LONG_PTR
Definition: treelist.c:79
#define TRUE
Definition: types.h:120
#define MAKEWPARAM(l, h)
Definition: winuser.h:3984
#define UDM_GETPOS
Definition: commctrl.h:2140
#define CloseHandle
Definition: compat.h:406
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:878
#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:72
#define LOCALE_USER_DEFAULT
#define CB_GETITEMDATA
Definition: winuser.h:1932
#define WM_CTLCOLORSTATIC
Definition: winuser.h:1754
struct _NM_UPDOWN * LPNMUPDOWN
#define MAKELPARAM(l, h)
Definition: winuser.h:3983
#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:2199
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
BOOL SystemSetLocalTime(LPSYSTEMTIME lpSystemTime)
Definition: dateandtime.c:17
#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:1925
WPARAM wParam
Definition: combotst.c:138
#define IDC_TIMEPICKER
Definition: resource.h:11
#define DWLP_MSGRESULT
Definition: winuser.h:864
struct tagNMHDR * LPNMHDR
UINT code
Definition: winuser.h:3134
INT WINAPI GetLocaleInfoW(LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len)
Definition: lang.c:1098
static VOID SetTimeZoneName(HWND hwnd)
Definition: dateandtime.c:111
#define IDC_YEARTEXT
Definition: resource.h:13
WORD wYear
Definition: winbase.h:877
#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:3133
#define IDC_MONTHCB
Definition: resource.h:6
#define SE_SYSTEMTIME_NAME
Definition: winnt_old.h:376
#define CB_ADDSTRING
Definition: winuser.h:1918
#define MAKELONG(a, b)
Definition: typedefs.h:248
smooth NULL
Definition: ftsmooth.c:416
#define WM_KEYDOWN
Definition: winuser.h:1697
LONG_PTR LPARAM
Definition: windef.h:208
VOID WINAPI GetLocalTime(OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:286
#define UDM_SETRANGE
Definition: commctrl.h:2137
#define WM_SETTEXT
Definition: winuser.h:1599
#define CB_RESETCONTENT
Definition: winuser.h:1941
#define CB_ERR
Definition: winuser.h:2410
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:1724
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1138
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:265
#define MCCN_SELCHANGE
Definition: timedate.h:75
#define PSNRET_NOERROR
Definition: prsht.h:129
static VOID SetLocalSystemTime(HWND hwnd)
Definition: dateandtime.c:76
#define DTM_SETSYSTEMTIME
Definition: commctrl.h:4327
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:220
#define UDN_DELTAPOS
Definition: commctrl.h:2165
#define ID_TIMER
Definition: resource.h:23
#define DateTime_GetSystemtime(hdp, pst)
Definition: commctrl.h:4326
#define CB_SETCURSEL
Definition: winuser.h:1943
#define IDC_YEAR
Definition: resource.h:12
#define WM_COMMAND
Definition: winuser.h:1722
#define GWLP_WNDPROC
Definition: treelist.c:66
static WORD GetCBSelectedMonth(HWND hCombo)
Definition: dateandtime.c:195
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:2201
LRESULT(CALLBACK * WNDPROC)(HWND, UINT, WPARAM, LPARAM)
Definition: winuser.h:2881
int iDelta
Definition: commctrl.h:2162
HWND WINAPI GetParent(_In_ HWND)
WCHAR DaylightName[32]
Definition: winbase.h:1170
#define GDT_VALID
Definition: commctrl.h:4456
#define IDC_MONTHCALENDAR
Definition: resource.h:8
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
#define UDM_SETPOS
Definition: commctrl.h:2139
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:1961
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]
Definition: setypes.h:970
#define MCCM_GETDATE
Definition: timedate.h:71
static VOID FillMonthsComboBox(HWND hCombo)
Definition: dateandtime.c:146
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:62
#define SetWindowLongPtrW
Definition: winuser.h:5321
#define CLM_STARTCLOCK
Definition: timedate.h:63
HBRUSH WINAPI GetSysColorBrush(_In_ int)
BOOL WINAPI SetLocalTime(IN CONST SYSTEMTIME *lpSystemTime)
Definition: time.c:356
#define DTN_DATETIMECHANGE
Definition: commctrl.h:4362
static VOID AutoUpdateMonthCal(HWND hwndDlg, PNMMCCAUTOUPDATE lpAutoUpdate)
Definition: dateandtime.c:234
#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:1721
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:1167
static PLARGE_INTEGER Time
Definition: time.c:105
#define WM_NOTIFY
Definition: richedit.h:61
#define MCCN_AUTOUPDATE
Definition: timedate.h:86
static INT_PTR CALLBACK DTPProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: dateandtime.c:246
#define WM_TIMECHANGE
Definition: winuser.h:1616