FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour time.windows.com
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
time.windows.com
Posted: Wed Jun 25, 2014 05:58 PM

Masters, és posible sincronizar el reloj de windows 7 de la misma manera que con time.windows.com?

Gracias, saludos

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1303
Joined: Tue Jul 21, 2009 08:12 AM
Re: time.windows.com
Posted: Wed Jun 25, 2014 08:22 PM
Please, see:

Code (fw): Select all Collapse
#include "fivewin.ch"
#include "xbrowse.ch"

function main()

MsgInfo( GETSNTP( "204.123.2.72" ), "GETSNTP() de Antonio Linares" )


msgalert(test(), "CLASS TSNP")

msgalert(testtime(), "VIA HTTP")


return nil



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


        oSntp := TSntp():New( cIP, nPort, lSync )

        If oSntp:GetData()
           // orignal code:   ? oSntp:Date(), "date"
           cNetDate := oSntp:Date()
        Else
           cNetDate := "99/99/99"
        EndIf

Return  cNetDate




FUNCTION testTime()
   LOCAL oHttp, hTime,cTime
   oHttp:=CreateObject( "winhttp.winHttpRequest.5.1" )
   oHttp:Open("GET","http://json-time.appspot.com/time.json",.F.)
   oHttp:Send()
   oHttp:waitForResponse()
   IF oHttp:Status == 200
      hb_jsondecode(oHttp:ResponseText(),@hTime) //Parse JSON to hash
      cTime:=Str(hTime["hour"],2)+":"+Str(hTime["minute"],2)+":"+Str(hTime["second"],2)
      //--- just for test output data
      xBrowse(hTime) //All data in hash,
      MsgInfo(cTime+" GMT") //Time GMT
      MsgInfo(hTime["datetime"]) //Date time
      //--
   ENDIF
RETURN cTime



FUNCTION Time3()

LOCAL nTime := fCreate( "chkdtime.bat" )
LOCAL cBuff := ""
LOCAL lRetu := .F.
LOCAL cDbar := "/" //
LOCAL aEOL := { Chr(13) + Chr(10), Chr(13), Chr(10) }

fWrite( nTime, "w32tm /stripchart /samples:1 /dataonly /computer:time.window.com >chkdtime.txt" )
fClose( nTime )
ShellExecute( 0, "open", "chkdtime.bat",,, )
WaitSeconds( 2 )
IF ( nTime := fOpen( "chkdtime.txt" ) ) > 0
    WHILE hb_fReadLine( nTime, @cBuff, aEOL  ) = 0
        IF cDbar $ cBuff
            cBuff := subStr( cBuff, at( cDbar, cBuff ) - 2 )
            IF cToD( subStr( cBuff, 1, at( " ", cBuff ) - 1 ) ) > cToD( "01/01/14" )
                msgStop( "O PERÍODO DE AVALIAÇÃO TERMINOU" + CRLF + "SOLICITE UMA VERSÃO ATUALIZADA AO DESENVOLVEDOR", "Atenção" )
                lRetu := .T.
            ENDIF
            EXIT
        ENDIF
    ENDDO
    fClose( nTime )
ENDIF
fErase( "chkdtime.bat" )
fErase( "chkdtime.txt" )
RETURN ( lRetu )





// 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    := HB_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
           ::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" )



FUNCTION HB_GETSNTP()

 LOCAL cH := GETSNTP()

 alert(ch)


RETURN 12345678901234567890






#pragma BEGINDUMP

#include <hbapi.h>
#include <winsock.h>
#include <time.h>

#define MAXLEN 1024

HB_FUNC( GETSNTP )
{
   char * hostname = ( char * ) hb_parc( 1 );
   unsigned char msg[ 48 ] = { 010, 0, 0, 0, 0, 0, 0, 0, 0 };   // the packet we send
   unsigned long buf[ MAXLEN ]; // the buffer we get back
   struct sockaddr_in server_addr;
   int  s;  // socket
   WSADATA wsa;
   struct timeval timeout;
   fd_set fds;
   time_t tmit;


   WSAStartup( 0x101, &wsa );

   s = socket( PF_INET, SOCK_DGRAM, getprotobyname( "udp" )->p_proto );

   memset( &server_addr, 0, sizeof( server_addr ) );
   server_addr.sin_family = AF_INET;
   server_addr.sin_addr.s_addr = inet_addr( hostname );
   server_addr.sin_port = htons( 123 ); // ntp port

   sendto( s, msg, sizeof( msg ), 0, ( struct sockaddr * ) &server_addr, sizeof( server_addr ) );

   FD_ZERO( &fds );
   FD_SET( s, &fds );
   timeout.tv_sec = 10;
   timeout.tv_usec = 0;

   if( select( 0, &fds, NULL, NULL, &timeout ) )
   {
      recv( s, ( void * ) buf, sizeof( buf ), 0 );

      tmit = ntohl( buf[ 10 ] );
      tmit-= 2208988800U;
   }
   else
      MessageBox( 0, "can't read from NTP server", "ERROR", 0 );

   WSACleanup();

   hb_retc( ctime( &tmit ) );
}

#pragma ENDDUMP
Muchas gracias. Many thanks.



Un saludo, Best regards,



Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]



Implementando MSVC 2010, FWH64 y ADO.



Abandonando uso xHarbour y SQLRDD.
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: time.windows.com
Posted: Thu Jun 26, 2014 01:01 PM

Gracias Lucas, me retorna:

ERROR:

CAN´T READ FROM NTP SERVER

VIA HTTP

NIL

Regards, saludos.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: time.windows.com
Posted: Thu Jun 26, 2014 04:26 PM
Lucas, esta function hace el mismo que a de Maestro Antonio?

Code (fw): Select all Collapse
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" )


Gracias, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 392
Joined: Tue Jul 29, 2008 01:55 PM
Re: time.windows.com
Posted: Fri Jun 27, 2014 01:29 AM

Hola Lucas

Trato de probar este ejemplo pero no puedo ligar GETSNTP, existe esta funcion para xHarbour ?

Saludos

Visite Chiapas, el paraiso de México.

Continue the discussion