views:

415

answers:

3

Looking for a code sample that takes a .NET CF DateTime object, and sets the device's date and time to that date (and/or time zone). The purpose of this is to get the current "real" time off of a server via web service and set the device's time correctly.

Anyone done this before?

+4  A: 

You can use Opennetcf's DateTimeHelper class.

Alternatively you can p/invoke SetLocalTime.

kgiannakakis
+1  A: 

Regarding kgiannakakis' answer: Definitely the way to go, but if you don't want to require the entire OpenNETCF runtime DLL just to set the time, download the source code to the 1.4 version of the library (there are newer ones, but the source is not freely available above v1.4) and just pull out the DateTimeHelper Class (you may need to pull some dependencies too). And then compile it directly into your code... It will save you from unnecessary external files.

Adam Haile
A: 

P/Invoking SetSystemTime() is a better choice for this, especially if the device is in a different time zone than the server. However, there seems to be a bug in Windows Mobile 5 where it fails to properly deal with Daylight Savings Time, so sometimes the time set on the device with SetSystemTime is off by an hour.

MusiGenesis
You need to call SetLocalTime() twice. Add a 500 ms delay between the calls and then it properly sets the DST value.
Bryan
Really? This works?
MusiGenesis