This article reprinted: http://www.csframework.com/archive/2/arc-2-20130101-1971.htm
C # code:
/// <Summary>
/// Set local time
/// </Summary>
Public Class Localtimesync
{
[Dllimport ( " Kernel32.dll " )]
Public Static Extern Bool Setsystemtime ( Ref Systemtime required IME );
[Dllimport ( " Kernel32.dll " )]
Public Static Extern Void Getsystemtime ( Ref Systemtime required IME );
[Structlayout (layoutkind. Sequential)]
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 Wmiliseconds;
}
/// <Summary>
/// Set local time
/// </Summary>
Public Static Void Synctime (datetime currenttime)
{
Systemtime required IME = New Systemtime ();
Required ime. wyear = convert. touint16 (currenttime. year );
Required ime. wmonth = convert. touint16 (currenttime. month );
Required ime. wday = convert. touint16 (currenttime. Day );
Systime. wdayofweek = convert. touint16 (currenttime. dayofweek );
Systime. wminute = convert. touint16 (currenttime. Minute );
Systime. wsecond = convert. touint16 (currenttime. Second );
Systime. wmiliseconds = convert. touint16 (currenttime. millisecond );
// Processing Beijing Time
Int Nbeijinghour = currenttime. Hour- 8 ;
If (Nbeijinghour <= 0 )
{
Nbeijinghour = 24 ;
Required ime. wday = convert. touint16 (currenttime. Day- 1 );
// Systime. wdayofweek = convert. touint16 (current. dayofweek-1 );
}
Else
{
Required ime. wday = convert. touint16 (currenttime. Day );
Systime. wdayofweek = convert. touint16 (currenttime. dayofweek );
}
Systime. whour = convert. touint16 (nbeijinghour );
Setsystemtime ( Ref Systime ); // Set local time
}
}
//Source: C/S framework Network (www.csframework.com) QQ: 1980854898
C # code:
/// <Summary>
/// Sync server time
/// </Summary>
Public Void Syncservertime ()
{
String SQL = " Select getdate () " ;
Object O = dataprovider. instance. executescalar (globals. def_attend_system_db, SQL );
Datetime dt = convertex. todatetimeex (O );
// Localtimesync. synctime (DT );
// Comparison Time Consistency
If (Convertex. tocharyyyymmddhhmm (datetime. Now )! = Convertex. tocharyyyymmddhhmm (DT ))
Localtimesync. synctime (DT ); // Synchronization server time
}