Page 1 of 4

Get real date from the Internet

PostPosted: Tue Nov 07, 2017 8:35 pm
by Rick Lipkin
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

Re: Get real date from the Internet

PostPosted: Tue Nov 07, 2017 9:56 pm
by Wanderson
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} )

Re: Get real date from the Internet

PostPosted: Wed Nov 08, 2017 1:09 am
by ADutheil
I do it by running the following command: w32tm /stripchart /samples:1 /computer:0.pool.ntp.org /dataonly

Re: Get real date from the Internet

PostPosted: Mon Sep 26, 2022 9:25 am
by Horizon
Hi,

Is there any new news about this?

Thanks.

Re: Get real date from the Internet

PostPosted: Mon Sep 26, 2022 7:27 pm
by Antonio Linares

Re: Get real date from the Internet

PostPosted: Tue Sep 27, 2022 1:44 am
by nageswaragunupudi
This is one simple sample.
Not the best though.
We will be discussing other better alternatives.

Code: Select all  Expand view
#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
 


Image

Re: Get real date from the Internet

PostPosted: Tue Sep 27, 2022 6:14 am
by Antonio Linares
This is working great :-)
https://www.fivetechsoft.com/now.php
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   MsgInfo( Now() )  

return nil

function Now()

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

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

Re: Get real date from the Internet

PostPosted: Tue Sep 27, 2022 6:28 am
by Antonio Linares
Let me remind you that we have another very usefull GetIP()
https://www.fivetechsoft.com/getip.php
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   MsgInfo( GetIP() )  

return nil

function GetIP()

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

getip.php
Code: Select all  Expand view
<?php
  echo $_SERVER['REMOTE_ADDR'];
?>

Re: Get real date from the Internet

PostPosted: Wed Sep 28, 2022 5:57 am
by Jimmy
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 ... :roll:
when change to 3600 it poll every Hour and DATE() Function will work correct

***
Code: Select all  Expand view
HKEY_LOCAL_MACHINE \SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient

Re: Get real date from the Internet

PostPosted: Wed Sep 28, 2022 8:42 pm
by gabo
This topic is interesting but only the teacher Linares can understand it
[url]
https://stackoverflow.com/questions/667 ... rom-server
[/url]
Regards

Re: Get real date from the Internet

PostPosted: Wed Sep 28, 2022 9:17 pm
by Antonio Linares
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: Select all  Expand view
#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;
}

Re: Get real date from the Internet

PostPosted: Thu Sep 29, 2022 1:25 pm
by Horizon
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: Select all  Expand view
#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.

Re: Get real date from the Internet

PostPosted: Thu Sep 29, 2022 3:31 pm
by Antonio Linares
Dear Hakan,

This way is working fine with Borland only. Just two warnings that we can safe ignore them
Code: Select all  Expand view
#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 );
}

Re: Get real date from the Internet

PostPosted: Thu Sep 29, 2022 3:40 pm
by Antonio Linares
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.

Re: Get real date from the Internet

PostPosted: Thu Sep 29, 2022 4:42 pm
by Antonio Linares
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: Select all  Expand view
#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 );
}