Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting Server to Local Time

6 views
Skip to first unread message

Paul Duncan

unread,
Aug 3, 2005, 2:39:35 PM8/3/05
to

using System
;using System.Runtime.InteropServices;
public void UpdateTime(DateTime ServerTime)
{
TimeZone localZone = TimeZone.CurrentTimeZone;
DateTime localTime = localZone.ToLocalTime(ServerTime);
TimeSpan localOffset = localZone.GetUtcOffset(localTime );
ServerTime = ServerTime.Add(localOffset);
SetTime(ServerTime);
}
[StructLayoutAttribute(LayoutKind.Sequential)]
private 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;
}
[DllImport("kernel32.dll")]
static extern bool SetLocalTime(ref SYSTEMTIME time);

private void SetTime(DateTime NewTime)
{
SYSTEMTIME st;
st.year = (short)NewTime.Year;
st.month = (short)NewTime.Month;
st.dayOfWeek = (short)NewTime.DayOfWeek;
st.day = (short)NewTime.Day;
st.hour = (short)NewTime.Hour;
st.minute = (short)NewTime.Minute;
st.second = (short)NewTime.Second;
st.milliseconds = (short)NewTime.Millisecond;
SetLocalTime(ref st);
}


*** Sent via Developersdex http://www.developersdex.com ***

pdunc

unread,
Aug 3, 2005, 2:48:27 PM8/3/05
to
Hi all,

I need to update client machines with the correct time if it is inaccurate.
The server I'm using will always have the correct time so I'll be using that
as the source of correct time. The code below works most of the time but is
an an hour off sometimes (daylightsavings time).

For example, If I change the date on a local to machine to Jan 2005, my code
will update the time an hour ahead of where it should be. Any help would be
appreciated.

Thanks,Paul

Code is below

0 new messages