Reputation: 598
I am trying to set Windows Mobile Device Time using coredll.dll. I have created the functions which will set the time but I am confused about how should I call the function on button click to set the time. Below is the object and method to set the time but not sure how to call it in button click
//This will be the method you need to set the system time
[DllImport("coredll.dll", SetLastError=true)]
static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);
//This is the object you need to fill before you use SetLocalTime
public struct SYSTEMTIME
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
}
//And this is the final method to execute
public static void SetSystemDateTime(DateTime dt)
{
SYSTEMTIME systime;
systime.year = (short)dt.Year;
systime.month = (short)dt.Month;
systime.day = (short)dt.Day;
systime.hour = (short)dt.Hour;
systime.minute = (short)dt.Minute;
systime.second = (short)dt.Second;
systime.milliseconds = (short)dt.Millisecond;
systime.dayOfWeek = (short)dt.DayOfWeek;
SetLocalTime(systime);
}
How should I call this function in the below button click method?
private void btnSync_Click(object sender, EventArgs e)
{
//What Should I write here?
}
Upvotes: 0
Views: 2410
Reputation: 5959
Youe first need to instanciate a DateTime variable and fill that with the values you want to set.
DateTime localDateTime = DateTime.Now;
now localDateTime holds the (computer) local date and time values. You can then alter the properties of the DateTime structure to your needs. For example change the hour:
//localDateTime.Hour=13; // cannot be changed, so create a new DateTime
DateTime newDT = new DateTime(localDateTime.year, localDateTime.month, localDateTime.day,
13, localDateTime.minute, localDateTime.second, localDateTime.millisecond);
This will change the value of the DateTime Hour to 13. The changed localDateTime can now be used to change the local time inside your click handler:
private void btnSync_Click(object sender, EventArgs e)
{
DateTime localDateTime = DateTime.Now;
//localDateTime.Hour=13; // cannot be changed, so create a new DateTime
DateTime newDT = new DateTime(localDateTime.year, localDateTime.month, localDateTime.day,
13, localDateTime.minute, localDateTime.second, localDateTime.millisecond);
SetSystemDateTime(newDT
// set datetime a second time to avoid issues when crossing DST intervals
System.Threading.Thread.Sleep(500);
SetSystemDateTime(newDT);
}
BTW: I would name SetSystemDateTime better as SetLocalDateTime as it changes the local time and not the system time. The local time is calculated from the system time plus the computers timezone offset setting (ie GMT+1) plus the current Daylight Savings Time offest (if any). The other way around works also, where setting the localtime changes the system time with regarding the offsets.
Note: the local time may change due to TZ or DST changes but the system time is the same on all computers (in Honkong or New York). Otherwise the IT world would be out of sync.
There is also a SetSystemTime API function that is called using the same pattern:
[DllImport("coredll.dll", SetLastError=true)]
static extern bool SetSystemTime(ref SYSTEMTIME lpSystemTime);
Although one might think about adjusting dayofweek when changing the hour value. But there is no need if you change any of the other values.
Remember to call SetSystemTime or SetLocalTime two times to avoid issues when crossing DST areas. See also http://www.hjgode.de/wp/2010/10/08/windows-mobile-setsystemtime-and-dst-einsteins-relativity-theory/
Upvotes: 1
Reputation: 211
You can drag a DateTimePicker
element from the VS toolbox and then drop it into your form. It will be named: dateTimePicker1
.
If you have a button name btnSync
in your form, to set your device date and time when the button is clicked you can do like this:
private void btnSync_Click(object sender, EventArgs e)
{
DateTime dtToBeSet = dateTimePicker1.Value;
SetSystemDateTime(dtToBeSet);
}
If you are not using DateTimePicker
, you have anyway to retrieve similarly your values from your UI elements, create a DateTime
and then set it .
Upvotes: 0