delphi2007 教程

delphi2007 教程

首页 新随笔 联系 聚合 管理
  1013 Posts :: 0 Stories :: 28 Comments :: 0 Trackbacks
获得internet 时间的问题? Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiAPI/html/delphi_20061204151729125.html
在网上下载的源码都提示找不到控件.  
   
  能给个最简单的吗?  
  还有说明一下控件在要哪儿添加.和使用方法.

给分自己行吗???  
  结帖了。

给分自己行吗???  
  ------------  
  不可以  
  JF

//直接使用   TClientSocket  
  unit   TimeDllU;  
   
  interface  
   
  uses  
      Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,  
      ScktComp;  
   
  type  
      TDTForm   =   class(TForm)  
          DTSock:   TClientSocket;  
          procedure   DTSockRead(Sender:   TObject;   Socket:   TCustomWinSocket);  
      private  
          {   Private   declarations   }  
      public  
          {   Public   declarations   }  
      end;  
   
  const  
      TimerServer:   string='203.129.68.14';//香港时间服务器  
   
  var  
      DTForm:   TDTForm=nil;  
      DT:   TDateTime=-1;  
      DTReady:   Boolean=False;  
   
      procedure   TimeDllInit();   stdcall  
      function   TimeDllGetTime(doadj:   Boolean):   TDateTime;   stdcall  
      procedure   TimeDllFinish();   stdcall  
   
  implementation  
   
  {$R   *.DFM}  
   
  procedure   TimeDllInit();  
  begin  
      DTForm   :=   TDTForm.Create(Application);  
  end;  
   
  procedure   TimeDllFinish();  
  begin  
      DTForm.Free();  
  end;  
   
  var  
      pTimeZoneInformation:   TTimeZoneInformation;  
  function   TimeDllGetTime(doadj:   Boolean):   TDateTime;  
  var  
      systim:   SYSTEMTIME;  
      hToken:   THANDLE;  
      tkp:   TOKEN_PRIVILEGES;  
      tmp:   DWORD;  
      preTick:   DWORD;  
  begin  
      DT   :=   -1;  
      DTReady   :=   False;  
      try  
          DTForm.DTSock.Host   :=   TimerServer;  
          DTForm.DTSock.Open();  
          preTick   :=   GetTickCount();  
          While   GetTickCount()   -   preTick   <   5000   do  
          begin  
              Sleep(10);  
              Application.ProcessMessages();  
              if   DTReady   then  
                  Break;  
          end;  
      except  
      else  
          ;  
      end;  
      if   DTReady   then  
      begin  
          GetTimeZoneInformation(pTimeZoneInformation);  
          DT   :=   DT   -   pTimeZoneInformation.Bias/(24*60);   //(国际标准时间转换到本地时间)  
          if   doadj   then  
              if   DT   >   38880   then  
              begin  
                  DecodeDate(DT,   systim.wYear,   systim.wMonth,   systim.wDay);  
                  DecodeTime(DT,   systim.wHour,   systim.wMinute,   systim.wSecond,   systim.wMilliSeconds);  
                  if   OpenProcessToken(GetCurrentProcess(),   TOKEN_ADJUST_PRIVILEGES   or   TOKEN_QUERY,   hToken)   then  
                  begin  
                      LookupPrivilegeValue(nil,   'SeSystemTimePrivilege',   tkp.Privileges[0].Luid);  
                      tkp.PrivilegeCount   :=   1;   //   one   privilege   to   set  
                      tkp.Privileges[0].Attributes   :=   SE_PRIVILEGE_ENABLED;  
                      tmp   :=   0;  
                      AdjustTokenPrivileges(hToken,   FALSE,   tkp,   0,   nil,   tmp);  
                  end;  
                  SetLocalTime(systim);  
              end;  
      end;  
      Result   :=   DT;  
  end;  
   
  function   MouthStr2Int(ms:   string):   Word;  
  const  
      MouthStrs:   array   [1..12]   of   string   =  
      (  
          'JAN',  
          'FEB',  
          'MAR',  
          'APR',  
          'MAY',  
          'JUN',  
          'JUL',  
          'AUG',  
          'SEP',  
          'OCT',  
          'NOV',  
          'DEC'  
      );  
  var  
      i:   integer;  
  begin  
      ms   :=   UpperCase(ms);  
      for   i   :=   1   to   12   do  
      begin  
          if   ms   =   MouthStrs[i]   then  
          begin  
              Result   :=   i;  
              Exit;  
          end;  
      end;  
      Result   :=   0;  
  end;  
   
  procedure   TDTForm.DTSockRead(Sender:   TObject;   Socket:   TCustomWinSocket);  
  var  
      sTime   :   string;  
      systim:   SYSTEMTIME;  
      i:   integer;  
      ti:   TDateTime;  
  begin  
      sTime   :=   Socket.ReceiveText;  
      if   Length(sTime)   <   32   then  
      begin  
          i   :=   Pos('   ',   sTime);  
          if   i   =   0   then  
              Exit;  
          systim.wDay   :=   StrToInt(Copy(sTime,   1,   i-1));  
          Delete(sTime,   1,   i);  
          i   :=   Pos('   ',   sTime);  
          if   i   =   0   then  
              Exit;  
          systim.wMonth   :=   MouthStr2Int(Copy(sTime,   1,   i-1));  
          Delete(sTime,   1,   i);  
          i   :=   Pos('   ',   sTime);  
          if   i   =   0   then  
              Exit;  
          systim.wYear   :=   StrToInt(Copy(sTime,   1,   i-1));  
          Delete(sTime,   1,   i);  
   
          i   :=   Pos('   ',   sTime);  
          if   i   =   0   then  
              Exit;  
          ti   :=   StrToTime(Copy(sTime,   1,   i-1));  
          Delete(sTime,   1,   i);  
   
          if   UpperCase(Copy(sTime,   1,   3))   =   'HKT'   then  
          begin  
              DT   :=   EncodeDate(systim.wYear,   systim.wMonth,   systim.wDay);  
              DT   :=   DT   +   ti;  
              DT   :=   DT   -   (8/24);   //   HK   Time   to   UTC   (香港时间转换到国际标准时间)  
              DTReady   :=   True;  
          end;  
      end;  
  end;  
   
  end.  
 

//   改了下,用全角空格对齐  
  unit   TimeDllU;  
   
  interface  
   
  uses  
   Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,  
   ScktComp;  
   
  type  
   TDTForm   =   class(TForm)  
    DTSock:   TClientSocket;  
    procedure   DTSockRead(Sender:   TObject;   Socket:   TCustomWinSocket);  
   private  
    {   Private   declarations   }  
   public  
    {   Public   declarations   }  
   end;  
   
  const  
   TimerServer:   string='203.129.68.14';//香港时间服务器  
   
  var  
   DTForm:   TDTForm=nil;  
   DT:   TDateTime=-1;  
   DTReady:   Boolean=False;  
   
   procedure   TimeDllInit();   stdcall  
   function   TimeDllGetTime(doadj:   Boolean):   TDateTime;   stdcall  
   procedure   TimeDllFinish();   stdcall  
   
  implementation  
   
  {$R   *.DFM}  
   
  procedure   TimeDllInit();  
  begin  
   DTForm   :=   TDTForm.Create(Application);  
  end;  
   
  procedure   TimeDllFinish();  
  begin  
   DTForm.Free();  
  end;  
   
  var  
   pTimeZoneInformation:   TTimeZoneInformation;  
  function   TimeDllGetTime(doadj:   Boolean):   TDateTime;  
  var  
   systim:   SYSTEMTIME;  
   hToken:   THANDLE;  
   tkp:   TOKEN_PRIVILEGES;  
   tmp:   DWORD;  
   preTick:   DWORD;  
  begin  
   DT   :=   -1;  
   DTReady   :=   False;  
   try  
    DTForm.DTSock.Host   :=   TimerServer;  
    DTForm.DTSock.Open();  
    preTick   :=   GetTickCount();  
    While   GetTickCount()   -   preTick   <   5000   do  
    begin  
     Sleep(10);  
     Application.ProcessMessages();  
     if   DTReady   then  
      Break;  
    end;  
   except  
   else  
    ;  
   end;  
   if   DTReady   then  
   begin  
    GetTimeZoneInformation(pTimeZoneInformation);  
    DT   :=   DT   -   pTimeZoneInformation.Bias/(24*60);   //(国际标准时间转换到本地时间)  
    if   doadj   then  
     if   DT   >   38880   then  
     begin  
      DecodeDate(DT,   systim.wYear,   systim.wMonth,   systim.wDay);  
      DecodeTime(DT,   systim.wHour,   systim.wMinute,   systim.wSecond,   systim.wMilliSeconds);  
      if   OpenProcessToken(GetCurrentProcess(),   TOKEN_ADJUST_PRIVILEGES   or   TOKEN_QUERY,   hToken)   then  
      begin  
       LookupPrivilegeValue(nil,   'SeSystemTimePrivilege',   tkp.Privileges[0].Luid);  
       tkp.PrivilegeCount   :=   1;   //   one   privilege   to   set  
       tkp.Privileges[0].Attributes   :=   SE_PRIVILEGE_ENABLED;  
       tmp   :=   0;  
       AdjustTokenPrivileges(hToken,   FALSE,   tkp,   0,   nil,   tmp);  
      end;  
      SetLocalTime(systim);  
     end;  
   end;  
   Result   :=   DT;  
  end;  
   
  function   MouthStr2Int(ms:   string):   Word;  
  const  
   MouthStrs:   array   [1..12]   of   string   =  
   (  
    'JAN',  
    'FEB',  
    'MAR',  
    'APR',  
    'MAY',  
    'JUN',  
    'JUL',  
    'AUG',  
    'SEP',  
    'OCT',  
    'NOV',  
    'DEC'  
   );  
  var  
   i:   integer;  
  begin  
   ms   :=   UpperCase(ms);  
   for   i   :=   1   to   12   do  
   begin  
    if   ms   =   MouthStrs[i]   then  
    begin  
     Result   :=   i;  
     Exit;  
    end;  
   end;  
   Result   :=   0;  
  end;  
   
  procedure   TDTForm.DTSockRead(Sender:   TObject;   Socket:   TCustomWinSocket);  
  var  
   sTime   :   string;  
   systim:   SYSTEMTIME;  
   i:   integer;  
   ti:   TDateTime;  
  begin  
   sTime   :=   Socket.ReceiveText;  
   if   Length(sTime)   <   32   then  
   begin  
    i   :=   Pos('   ',   sTime);  
    if   i   =   0   then  
     Exit;  
    systim.wDay   :=   StrToInt(Copy(sTime,   1,   i-1));  
    Delete(sTime,   1,   i);  
    i   :=   Pos('   ',   sTime);  
    if   i   =   0   then  
     Exit;  
    systim.wMonth   :=   MouthStr2Int(Copy(sTime,   1,   i-1));  
    Delete(sTime,   1,   i);  
    i   :=   Pos('   ',   sTime);  
    if   i   =   0   then  
     Exit;  
    systim.wYear   :=   StrToInt(Copy(sTime,   1,   i-1));  
    Delete(sTime,   1,   i);  
   
    i   :=   Pos('   ',   sTime);  
    if   i   =   0   then  
     Exit;  
    ti   :=   StrToTime(Copy(sTime,   1,   i-1));  
    Delete(sTime,   1,   i);  
   
    if   UpperCase(Copy(sTime,   1,   3))   =   'HKT'   then  
    begin  
     DT   :=   EncodeDate(systim.wYear,   systim.wMonth,   systim.wDay);  
     DT   :=   DT   +   ti;  
     DT   :=   DT   -   (8/24);   //   HK   Time   to   UTC   (香港时间转换到国际标准时间)  
     DTReady   :=   True;  
    end;  
   end;  
  end;  
   
  end.  
 

Unit  
  NMTime  
   
  Description  
  The   TNMTime   component   is   used   for   getting   the   time   from   Internet   time   servers,   as   described   in   RFC   868.

分给我.

posted on 2008-09-23 09:37 delphi2007 阅读(118) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。