Help setting System Time 
Author Message
 Help setting System Time

Hello all, I am having a little bit of trouble making a program work.
What I am trying to write is a basic little applet  to let the user set
the system time from a date/time picker control. I also intend for the
applet to display the current time when it is first opened.

Here is what I have:  an MFC dialog based application.  There is a main
dialog, with a date/time picker control, an OK button and a Cancel
button.  The date time picker control is hooked to a CTime object
m_CurrentTime in the dialog class. The date/time picker is set to "time"
format, and is user editable. There is also a SYSTEMTIME object, st,
defined as a dialog class member.

The dialog constructor assigns the current time to m_CurrentTime as
follows:

m_CurrentTime = CTime::GetCurrentTime();

this part seems to work OK. When the applet starts, the current system
time is displayed in the date/timer picker.

Then, in the OnOK function, I do this, to supposedly set the system time
to the time in CurrentTime.

m_CurrentTime.GetAsSystemTime( st );
::SetSystemTime( &st );

This program is built using VC++ 6.0, SP3, on Windows 95.  When I
execute it, and set a new time and click OK, the following happens:

The system clock seems to get updated, but it takes 5 to 10 seconds
after the program closes, before the taskbar clock shows the updated
time, which is inevitably something different from what I select in the
date/time picker.  Also, after running this program, my system date is
now wrong.

Can anybody give me any clue on what exactly I am doing wrong?  Thanks
in advance!

Phil

--
remove "REMOVE-NOSPAM" from my e-mail address to reply.



Thu, 01 Aug 2002 03:00:00 GMT  
 Help setting System Time
Something I forgot to mention in my first message...  After my program
completes, if I immediately right click on the clock in the taskbar and
select "adjust date/time" the resulting dialog will already have the new
(wrong) time in it.

Is this just a windows quirk, that it takes the actual displayed clock
several seconds to sync with the time that has been set, or is this a result
of some problem in my program?  I tend to think it's my program, because
when I set the time using the standard windows date/time applet, the clock
updates as soon as I click "apply."

Any ideas / suggestions / thoughts would be greatly appreciated.

Phil

--
remove "REMOVE-NOSPAM" from my e-mail address to reply.



Thu, 01 Aug 2002 03:00:00 GMT  
 Help setting System Time


Quote:
> Something I forgot to mention in my first message...  After my
program
> completes, if I immediately right click on the clock in the taskbar
and
> select "adjust date/time" the resulting dialog will already have the
new
> (wrong) time in it.

> Is this just a windows quirk, that it takes the actual displayed
clock
> several seconds to sync with the time that has been set, or is this
a result
> of some problem in my program?  I tend to think it's my program,
because
> when I set the time using the standard windows date/time applet, the
clock
> updates as soon as I click "apply."

Look at WM_TIMECHANGE. You must broadcast this message to let other
apps know that you've changed system time. That'll immediately
synchronize taskbar clock. BTW, since you write a system time
dependent app, it's a good idea to also handle this message. For
example, suppose your app and standard Date/Time Properties Control
Panel applet are run side by side. You'd want to see changes in one
whenever you make them in the other, and vice versa. For this to work,
you must both send and handle WM_TIMECHANGE.

With best wishes,
    Igor Tandetnik



Thu, 01 Aug 2002 03:00:00 GMT  
 Help setting System Time
Aside note:
Since you are using a SYSTEMTIME, which is better than CTime, get rid of CTime
altogether.  Just call the ::GetLocalTime API function to fill a SYSTEMTIME
structure directly with the current local time.

Definitely implement broadcast and handling of WM_TIMECHANGE message, as Igor
Tandetnik suggested.  EXCEPT, do not broadcast the message when the user is
running Windows 2000.



Thu, 01 Aug 2002 03:00:00 GMT  
 Help setting System Time

Quote:

> Aside note:
> Since you are using a SYSTEMTIME, which is better than CTime, get rid of CTime
> altogether.  Just call the ::GetLocalTime API function to fill a SYSTEMTIME
> structure directly with the current local time.

The only problem with that is, I'm not entirely sure how to get the value out of
the date/time picker, into a SYSTEMTIME. That's the only reason for the CTime
object, really.

Quote:
> Definitely implement broadcast and handling of WM_TIMECHANGE message, as Igor
> Tandetnik suggested.  EXCEPT, do not broadcast the message when the user is
> running Windows 2000.

Ok, I'll look into WM_TIMECHANGE.  Thanks.

--
remove "REMOVE-NOSPAM" from my e-mail address to reply.



Thu, 01 Aug 2002 03:00:00 GMT  
 Help setting System Time

Quote:

> Look at WM_TIMECHANGE. You must broadcast this message to let other

Ok, thanks.  I'll look into that.

Phil

--
remove "REMOVE-NOSPAM" from my e-mail address to reply.



Thu, 01 Aug 2002 03:00:00 GMT  
 Help setting System Time


Fri, 19 Jun 1992 00:00:00 GMT  
 Help setting System Time
Ok, I changed my OnOK() to this:

m_CurrentTime.GetAsSystemTime( st );
::SetSystemTime( &st );
::SendMessage( HWND_TOPMOST, WM_TIMECHANGE, 0, 0 );

now the clock updates immediately, as it should.   But, I'm still getting the
WRONG time! Ugh...  Is there any conversion involved in any of these time
functions, that I'm not accounting for? I see that SetSystemTime expects the time
in UTC.  Is that a factor? Am I getting the time expressed in my local time zone
earlier in the program, and need to convert to UTC before calling SetSystemTime()?

Or am I overlooking something else?  Thanks again for the help!

Phil

--
remove "REMOVE-NOSPAM" from my e-mail address to reply.



Thu, 01 Aug 2002 03:00:00 GMT  
 Help setting System Time


Fri, 19 Jun 1992 00:00:00 GMT  
 Help setting System Time

Ok, I changed my OnOK() to this:

m_CurrentTime.GetAsSystemTime( st );
::SetSystemTime( &st );
::SendMessage( HWND_TOPMOST, WM_TIMECHANGE, 0, 0 );

now the clock updates immediately, as it should.   But, I'm still getting
the
WRONG time! Ugh...  Is there any conversion involved in any of these time
functions, that I'm not accounting for? I see that SetSystemTime expects the
time
in UTC.  Is that a factor? Am I getting the time expressed in my local time
zone
earlier in the program, and need to convert to UTC before calling
SetSystemTime()?

Or am I overlooking something else?  Thanks again for the help!

Phil
--------
remove "REMOVE-NOSPAM" from my e-mail address to reply.



Thu, 01 Aug 2002 03:00:00 GMT  
 Help setting System Time


Fri, 19 Jun 1992 00:00:00 GMT  
 Help setting System Time
OK, guys, I got it figured out. It works now, as intended.  Thanks for the
help.

Phil

--
remove "REMOVE-NOSPAM" from my e-mail address to reply.



Thu, 01 Aug 2002 03:00:00 GMT  
 Help setting System Time


Fri, 19 Jun 1992 00:00:00 GMT  
 Help setting System Time


Quote:
> Ok, I changed my OnOK() to this:

> m_CurrentTime.GetAsSystemTime( st );
> ::SetSystemTime( &st );
> ::SendMessage( HWND_TOPMOST, WM_TIMECHANGE, 0, 0 );

> now the clock updates immediately, as it should.   But, I'm still
getting the
> WRONG time! Ugh...  Is there any conversion involved in any of these
time
> functions, that I'm not accounting for? I see that SetSystemTime
expects the time
> in UTC.  Is that a factor? Am I getting the time expressed in my
local time zone
> earlier in the program, and need to convert to UTC before calling

SetSystemTime()?

Try SetLocalTime instead

With best wishes,
    Igor Tandetnik



Thu, 01 Aug 2002 03:00:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Help setting System Time

2. Setting system time.

3. Set System time question

4. How to set System Time on Windows NT?

5. Changing registry settings and system time

6. Setting computer system time

7. Set system time to a COleDateTime?

8. setting system time

9. Set System Time

10. Setting system time

11. Set System time?

12. {Newbie] Setting system time from time_t

 

 
Powered by phpBB® Forum Software