DateTime Returns Incorrect Time

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I change the time outside the .Net app hosting DateTime, I need to
re-start app in order for DateTime to pickup the correct time.

Is there a work around? Is there a fix.
 
Mark,

You need to use native code to update the system date & time on device.

Here's how:

using System.Runtime.InteropServices;

public struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}

[DllImport("coredll.dll")]
//public extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
public extern static void GetLocalTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]
//public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
public extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);

// note - I'm setting some up/down controls with the values returned in the
// code below
private void _showCurrentLocalDateTime()
{
SYSTEMTIME st = new SYSTEMTIME();
try
{
GetLocalTime(ref st);

this.nudDay.Value = (int)st.wDay;
this.nudMonth.Value = (int)st.wMonth;
this.nudYear.Value = (int)st.wYear;
this.nudMinute.Value = (int)st.wMinute;
this.dudHour.SelectedIndex = (int)st.wHour;
}
catch (Exception e)
{
}
}
// note, I'm getting the values to set from some custom up/down controls in
the
// code below....
private void _updateLocalDateTime()
{
SYSTEMTIME st = new SYSTEMTIME();
GetLocalTime(ref st);

st.wMonth = (ushort)this.nudMonth.Value;
st.wDay = (ushort)this.nudDay.Value;
st.wYear = (ushort)this.nudYear.Value;
st.wMinute = (ushort)this.nudMinute.Value;
st.wHour = (ushort)this.dudHour.SelectedIndex;

SetLocalTime(ref st);

}

-Darren
 
Darren,

Thanks for the quick response. Perhaps I was ambiguous in my post. Here is
the scenario:

1. CF Application is running.
2. DateTime.Now retrieves correct time from OS.
3. I change the time outside the application, e.g., Control Panel.
4. DateTime.Now does not retrieve current time from OS.

I understand that this is a known .NET CF bug. I was wondering if others
have come up with a creative workaround while awaiting Microsoft.

Mark
 
Back
Top