FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Get real date from the Internet
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Get real date from the Internet
Posted: Tue Nov 07, 2017 08:35 PM

To All

My software license routine relies on getting the ACTUAL date .. and right now I am using Date() which relies on the date on the machine clock. It is easy to set any date you want on the ( bios ) machine clock.

Is there a way to go out to the internet to return the Real and Actual date ? and not rely on the Machine Bios clock ?

Thanks
Rick Lipkin

Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Get real date from the Internet
Posted: Tue Nov 07, 2017 09:56 PM
Rick Lipkin wrote:To All

My software license routine relies on getting the ACTUAL date .. and right now I am using Date() which relies on the date on the machine clock. It is easy to set any date you want on the ( bios ) machine clock.

Is there a way to go out to the internet to return the Real and Actual date ? and not rely on the Machine Bios clock ?

Thanks
Rick Lipkin


I use this in Brasil.

Function HrDtInternet()
Local oHttp, ;
cResp1 := "", ;
cResp := "", ;
cHora := "", ;
cData := ""

IF !IsInternet()
MsgWait( "Não foi possível conectar a Internet para"+CRLF+;
"buscar a Data e Hora de Brasilia"+CRLF+;
"A Data e a Hora serão setados conforme"+CRLF+;
"o que tiver configurado no micro","Internet", 2)
Return( {DtoC(Date()), Time()} )
ENDIF

Try
oHttp := CreateObject("winhttp.winhttprequest.5.1")
oHttp:Open("GET","http://www.horacerta.com.br/index.php?city=sao_paulo",.f.)
oHttp:Send()
cResp1 := oHttp:ResponseText()
Catch
Return( {DtoC(Date()), Time()} )
End Try
*
* MemoWrit( "HoraNet.txt", cResp1 )
*
*<input name="mostrador" type="text" size="25" value="19/09/2011 - 01:02:00 PM" />
*
cResp := SubStr( cResp1 , At( '<input name="mostrador"', cResp1 ) )
cResp := Substr( cResp , 1, At( '/>', cResp )-2 )
cDados := SubStr( cResp , At( 'value="', cResp )+7 )
*
cData := SubStr( cDados, 1, 10)
cHora := SubStr( cDados, 14 )
*
If "PM" $ cHora .AND. SubStr(cHora,1,2) != "12"
cHora := Str(Val(SubStr(cHora,1,2))+12,2)+SubStr(cHora,3,7)
End
*
Return( {cData, cHora} )
Posts: 368
Joined: Sun May 31, 2009 06:25 PM
Re: Get real date from the Internet
Posted: Wed Nov 08, 2017 01:09 AM

I do it by running the following command: w32tm /stripchart /samples:1 /computer:0.pool.ntp.org /dataonly

Regards,



André Dutheil

FWH 13.04 + HB 3.2 + MSVS 10
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: Get real date from the Internet
Posted: Mon Sep 26, 2022 09:25 AM

Hi,

Is there any new news about this?

Thanks.

Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Mon Sep 26, 2022 07:27 PM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Get real date from the Internet
Posted: Tue Sep 27, 2022 01:44 AM
This is one simple sample.
Not the best though.
We will be discussing other better alternatives.

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

function Main()

   local hDateTime

   hDateTime   := WorldClock( "cst" )
   XBROWSER hDateTime TITLE hDateTime[ "timeZoneName" ]

return nil

function WorldClock( cTimeZone )

   local hDateTime, cUrl, c, t

   DEFAULT cTimeZone := "utc"
   cTimeZone   := Lower( cTimeZone )
   if !( cTimeZone $ "wst,cst,est,utc,gmt,cet" )
      cTimeZone   := "utc"
   endif
   //
   cUrl  := "http://worldclockapi.com/api/json/" + cTimeZone + "/now"
   hb_jsonDecode( WebPageContents( cUrl, .t. ), @hDateTime )

#ifdef __XHARBOUR__
   HSetCaseMatch( hDateTime, .f. )
   hDateTime[ "currentDateTime" ] :=    STOT( Left( CharRem( "-T:", hDateTime[ "currentDateTime" ] ), 12 ) )
#else
   hb_hSetCaseMatch( hDateTime, .f. )
   hDateTime[ "currentDateTime" ] := HB_STOT( Left( CharRem( "-T:", hDateTime[ "currentDateTime" ] ), 12 ) )
#endif

return hDateTime


Regards



G. N. Rao.

Hyderabad, India
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Tue Sep 27, 2022 06:14 AM
This is working great :-)
https://www.fivetechsoft.com/now.php
Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()

   MsgInfo( Now() )  

return nil

function Now()

return WebPageContents( "https://www.fivetechsoft.com/now.php" )

Where now.php is:
Code (fw): Select all Collapse
<?php
  echo gmdate('l jS \of F Y h:i:s A');
?>
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Tue Sep 27, 2022 06:28 AM
Let me remind you that we have another very usefull GetIP()
https://www.fivetechsoft.com/getip.php
Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()

   MsgInfo( GetIP() )  

return nil

function GetIP()

return WebPageContents( "https://www.fivetechsoft.com/getip.php" )

getip.php
Code (fw): Select all Collapse
<?php
  echo $_SERVER['REMOTE_ADDR'];
?>
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: Get real date from the Internet
Posted: Wed Sep 28, 2022 05:57 AM
hi,

Windows can use a NTP-Server to sync DATE / TIME
default "time.windows.com" is used ... i prefer local Service for Germany "ptbtime1.ptb.de"

but you must change Value for "SpecialPollInterval"*** which default Value is 604800 Seconds ... :-)
when change to 3600 it poll every Hour and DATE() Function will work correct

***
Code (fw): Select all Collapse
HKEY_LOCAL_MACHINE \SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient
greeting,

Jimmy
Posts: 128
Joined: Tue Jan 03, 2006 08:31 PM
Re: Get real date from the Internet
Posted: Wed Sep 28, 2022 08:42 PM
This topic is interesting but only the teacher Linares can understand it
https://stackoverflow.com/questions/667 ... rom-server
" rel="noopener">
https://stackoverflow.com/questions/667 ... rom-server

Regards
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Wed Sep 28, 2022 09:17 PM
We could use this AI playground proposal and turn it into a FWH C function. As an EXE is working fine:
( I just asked the AI playground for this "how to retrieve the time from internet using C language")
test
Current time: Wed Sep 28 23:17:15 2022

if you see it of utility please say so and we will convert it into a FWH function.
Code (fw): Select all Collapse
#include <stdio.h>
#include <stdlib.h>
#include <ws2tcpip.h>
#include <time.h>

int main()
{
    struct addrinfo hints;
    struct addrinfo *result;
    int sockfd;
    int rv;
    char buf[64];
    time_t t;
    WSADATA wsaData;   
    int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET; /* Allow IPv4 */
    hints.ai_socktype = SOCK_STREAM; /* Stream socket */
    hints.ai_flags = AI_CANONNAME; /* Return canonical name */
    
    rv = getaddrinfo("www.google.com", "http", &hints, &result);
    if (rv != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }
    
    /* Create socket */
    sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (sockfd == -1) {
        perror("socket");
        exit(1);
    }
    
    /* Connect */
    if (connect(sockfd, result->ai_addr, result->ai_addrlen) == -1) {
        perror("connect");
        exit(1);
    }
    
    /* Get time */
    t = time(NULL);
    snprintf(buf, sizeof(buf), "GET / HTTP/1.0\r\n\r\n");
    send(sockfd, buf, strlen(buf), 0);
    recv(sockfd, buf, sizeof(buf), 0);
    printf("Current time: %s", ctime(&t));
    
    close(sockfd);
    freeaddrinfo(result);
    
    return 0;
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: Get real date from the Internet
Posted: Thu Sep 29, 2022 01:25 PM
Antonio Linares wrote:We could use this AI playground proposal and turn it into a FWH C function. As an EXE is working fine:
( I just asked the AI playground for this "how to retrieve the time from internet using C language")
test
Current time: Wed Sep 28 23:17:15 2022

if you see it of utility please say so and we will convert it into a FWH function.
Code (fw): Select all Collapse
#include <stdio.h>
#include <stdlib.h>
#include <ws2tcpip.h>
#include <time.h>

int main()
{
    struct addrinfo hints;
    struct addrinfo *result;
    int sockfd;
    int rv;
    char buf[64];
    time_t t;
    WSADATA wsaData;   
    int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET; /* Allow IPv4 */
    hints.ai_socktype = SOCK_STREAM; /* Stream socket */
    hints.ai_flags = AI_CANONNAME; /* Return canonical name */
    
    rv = getaddrinfo("www.google.com", "http", &hints, &result);
    if (rv != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }
    
    /* Create socket */
    sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (sockfd == -1) {
        perror("socket");
        exit(1);
    }
    
    /* Connect */
    if (connect(sockfd, result->ai_addr, result->ai_addrlen) == -1) {
        perror("connect");
        exit(1);
    }
    
    /* Get time */
    t = time(NULL);
    snprintf(buf, sizeof(buf), "GET / HTTP/1.0\r\n\r\n");
    send(sockfd, buf, strlen(buf), 0);
    recv(sockfd, buf, sizeof(buf), 0);
    printf("Current time: %s", ctime(&t));
    
    close(sockfd);
    freeaddrinfo(result);
    
    return 0;
}


Antonio,

It would be amazing.
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Thu Sep 29, 2022 03:31 PM
Dear Hakan,

This way is working fine with Borland only. Just two warnings that we can safe ignore them
Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()

   MsgInfo( Now() )

return nil

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ws2tcpip.h>
#include <time.h>
#include <winsock.h>

HB_FUNC( NOW )
{
    struct addrinfo hints;
    struct addrinfo *result;
    int sockfd;
    int rv;
    char buf[64];
    time_t t;
    WSADATA wsaData;  

    WSAStartup(MAKEWORD(2,2), &wsaData);

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET; /* Allow IPv4 */
    hints.ai_socktype = SOCK_STREAM; /* Stream socket */
    hints.ai_flags = AI_CANONNAME; /* Return canonical name */
   
    rv = getaddrinfo("www.google.com", "http", &hints, &result);
    if (rv != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }
   
    /* Create socket */
    sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (sockfd == -1) {
        perror("socket");
        exit(1);
    }
   
    /* Connect */
    if (connect(sockfd, result->ai_addr, result->ai_addrlen) == -1) {
        perror("connect");
        exit(1);
    }
   
    /* Get time */
    t = time(NULL);
    snprintf(buf, sizeof(buf), "GET / HTTP/1.0\r\n\r\n");
    send(sockfd, buf, strlen(buf), 0);
    recv(sockfd, buf, sizeof(buf), 0);
    snprintf(buf, sizeof( buf ), "Current time: %s", ctime(&t));
   
    closesocket(sockfd);
    freeaddrinfo(result);
   
    hb_retc( buf ); 
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Thu Sep 29, 2022 03:40 PM

I am able to build it and test it using Borland.

With MSVC 2022 can't get it yet. Lots of warnings and errors. Trying to find why.

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Thu Sep 29, 2022 04:42 PM
This version compiles with fine with Borland and still fails using Microsoft. I guess we are on the right way. Hopefully someone comes with a solution :-)
Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()

   MsgInfo( Now() )

return nil

#pragma BEGINDUMP

#ifdef _CRT_SECURE_NO_WARNINGS
#undef _CRT_SECURE_NO_WARNINGS
#endif
#define _CRT_SECURE_NO_WARNINGS 1

#include <hbapi.h>

#ifndef __BORLANDC__
   #define _WIN32_WINNT <= 0x0502
#endif 

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#ifndef __BORLANDC__

typedef struct addrinfo
{
    int                 ai_flags;       // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST
    int                 ai_family;      // PF_xxx
    int                 ai_socktype;    // SOCK_xxx
    int                 ai_protocol;    // 0 or IPPROTO_xxx for IPv4 and IPv6
    size_t              ai_addrlen;     // Length of ai_addr
    char *              ai_canonname;   // Canonical name for nodename
    __field_bcount(ai_addrlen) struct sockaddr *   ai_addr;        // Binary address
    struct addrinfo *   ai_next;        // Next structure in linked list
}
ADDRINFOA, *PADDRINFOA;

#define AI_CANONNAME    0x00000002  // Return canonical name in first ai_canonname

INT getaddrinfo(
  PCSTR           pNodeName,
  PCSTR           pServiceName,
  const ADDRINFOA *pHints,
  PADDRINFOA      *ppResult
);

char * gai_strerror( int ecode );
void freeaddrinfo( PADDRINFOA pAddrInfo );

#endif

HB_FUNC( NOW )
{
    struct addrinfo hints;
    struct addrinfo *result;
    int sockfd;
    int rv;
    char buf[64];
    time_t t;
    WSADATA wsaData;  

    WSAStartup(MAKEWORD(2,2), &wsaData);

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET; /* Allow IPv4 */
    hints.ai_socktype = SOCK_STREAM; /* Stream socket */
    hints.ai_flags = AI_CANONNAME; /* Return canonical name */
   
    rv = getaddrinfo("www.google.com", "http", &hints, &result);
    if (rv != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }
   
    /* Create socket */
    sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (sockfd == -1) {
        perror("socket");
        exit(1);
    }
   
    /* Connect */
    if (connect(sockfd, result->ai_addr, result->ai_addrlen) == -1) {
        perror("connect");
        exit(1);
    }
   
    /* Get time */
    t = time(NULL);
    snprintf(buf, sizeof(buf), "GET / HTTP/1.0\r\n\r\n");
    send(sockfd, buf, strlen(buf), 0);
    recv(sockfd, buf, sizeof(buf), 0);
    snprintf(buf, sizeof( buf ), "Current time: %s", ctime(&t));
   
    closesocket(sockfd);
    freeaddrinfo(result);
   
    hb_retc( buf ); 
}
regards, saludos

Antonio Linares
www.fivetechsoft.com