Get SNTP Server Time and Set LocalTime(update:2010.08.09)

xBase/clipper
回覆文章
admin
Site Admin
文章: 53
註冊時間: 2014-09-23, 10:58

Get SNTP Server Time and Set LocalTime(update:2010.08.09)

文章 admin »

以下示範如何取得 SNTP Server 時間,而且可以作本地時間與 SNTP 時間同步功能:

The Library is compile use BCC5 for xHarbour and Fivewin.

代碼: 選擇全部

Func    Test() 
Local   aNet      := {},; 
        nPort     := 123,; 
        cIp       := "time.windows.com",; 
        lSync     := .T.,; 
        oSntp     := NIL 

        ? "test begin" 

        // test-1 
        // lSync = .T., 取得時間後, 讓 LocalTime 與 NTP Server 時間同步 
        oSntp := TSntp():New( cIP, nPort, lSync ) 
        // or oSntp := TSntp():New(); oSntp:cIp := '....'; oSntp:nPort = .... 
        If oSntp:GetData() 
           ? oSntp:Date(), "date" 
           ? oSntp:Time(), "time" 
           ? oSntp:nWeek, "Week" 
           ? oSntp:nDayOfYear, "Day Of Year" 
        Else 
           ? "Fail" 
        EndIf 

        // test-2 
        If oSntp:GetData() 
           ? oSntp:Date(), "date" 
           ? oSntp:Time(), "time" 
        Else 
           ? "Fail" 
        EndIf 

        ? "test end" 

Return  NIL 

// TSNTP.PRG 
// Copyright by WenSheng come from TAIWAN 
#include "hbclass.ch" 
#include "common.ch" 
CLASS   TSNTP 

        DATA    cServer,;       // server name/ip 
                nPort,;         // server port 
                lSync,;         // Sync flag 
                lError 

        DATA    nYear,; 
                nMonth,; 
                nDay,; 
                nWeek,; 
                nDayOfYear 

        DATA    nHours,; 
                nMinute,; 
                nSeconds 

        METHOD  New(cServer, nPort, lSync) 
        METHOD  GetData() 
        MESSAGE DATE    METHOD  _Date() 
        MESSAGE TIME    METHOD  _Time() 

ENDCLASS 

// 
METHOD  New( cServer, nPort, lSync )    CLASS   TSNTP 
DEFAULT cServer to "time.windows.com"   // 主機名稱 
DEFAULT nPort   to 123                  // 主機 port 
DEFAULT lSync   to .F.                  // 時間同步 

        ::cServer   := cServer 
        ::nPort     := nPort 
        ::lSync     := lSync 
        ::lError    := .F. 

RETURN  Self 

// 
METHOD  GetData()   CLASS   TSNTP 
Local   xRet    := GetSNTP( ::cServer, ::nPort, ::lSync ) 
        If ValType(xRet) == "N" 
           ::lError   := .T. 
           ::nYear    := 0 
           ::nMonth   := 0 
           ::nDay     := 0 
           ::nHours   := 0 
           ::nMinute  := 0 
           ::nSeconds := 0 
           ::nWeek    := 0 
           ::nDayOfYear := 0 
        Else 
           ::lError := .F. 
           //          0        1         2 
           //          12345678901234567890 
           // 取得資料 yyyymmddhhmmsswyda 
           ::nYear    := Val( Substr( xRet,  1, 4 )) 
           ::nMonth   := Val( Substr( xRet,  5, 2 )) 
           ::nDay     := Val( Substr( xRet,  7, 2 )) 
           ::nHours   := Val( Substr( xRet,  9, 2 )) 
           ::nMinute  := Val( Substr( xRet, 11, 2 )) 
           ::nSeconds := Val( Substr( xRet, 13, 2 )) 
           ::nWeek    := Val( Substr( xRet, 15, 1 )) 
           ::nDayOfYear := Val( Substr( xRet, 16, 3 )) 
        EndIf 

RETURN  ! ::lError 

// 
METHOD  _Date()     CLASS   TSNTP 
RETURN  StoD( PadL( ::nYear, 4, "0" )+; 
              PadL( ::nMonth, 2, "0" )+; 
              PadL( ::nDay, 2, "0" )) 

// 
METHOD  _Time()     CLASS   TSNTP 
RETURN  PadL( ::nHours, 2, "0" )+":"+; 
        PadL( ::nMinute, 2, "0" )+":"+; 
        PadL( ::nSeconds, 2, "0" ) 
line ID: ssbbstw
WeChat ID: ssbbstw
回覆文章