Get real date from the Internet

Get real date from the Internet

Postby Rick Lipkin » Tue Nov 07, 2017 8: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
User avatar
Rick Lipkin
 
Posts: 2666
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Get real date from the Internet

Postby Wanderson » Tue Nov 07, 2017 9: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} )
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Get real date from the Internet

Postby ADutheil » Wed Nov 08, 2017 1:09 am

I do it by running the following command: w32tm /stripchart /samples:1 /computer:0.pool.ntp.org /dataonly
ADutheil
 
Posts: 368
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: Get real date from the Internet

Postby Horizon » Mon Sep 26, 2022 9:25 am

Hi,

Is there any new news about this?

Thanks.
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: Get real date from the Internet

Postby Antonio Linares » Mon Sep 26, 2022 7:27 pm

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby nageswaragunupudi » Tue Sep 27, 2022 1:44 am

This is one simple sample.
Not the best though.
We will be discussing other better alternatives.

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

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10655
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Get real date from the Internet

Postby Antonio Linares » Tue Sep 27, 2022 6:14 am

This is working great :-)
https://www.fivetechsoft.com/now.php
Code: Select all  Expand view  RUN
#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  RUN
<?php
  echo gmdate('l jS \of F Y h:i:s A');
?>
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Antonio Linares » Tue Sep 27, 2022 6:28 am

Let me remind you that we have another very usefull GetIP()
https://www.fivetechsoft.com/getip.php
Code: Select all  Expand view  RUN
#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  RUN
<?php
  echo $_SERVER['REMOTE_ADDR'];
?>
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

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

***
Code: Select all  Expand view  RUN
HKEY_LOCAL_MACHINE \SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: Get real date from the Internet

Postby gabo » Wed Sep 28, 2022 8:42 pm

This topic is interesting but only the teacher Linares can understand it
[url]
https://stackoverflow.com/questions/667 ... rom-server
[/url]
Regards
gabo
 
Posts: 128
Joined: Tue Jan 03, 2006 8:31 pm

Re: Get real date from the Internet

Postby Antonio Linares » Wed Sep 28, 2022 9: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: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Horizon » Thu Sep 29, 2022 1: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: Select all  Expand view  RUN
#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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: Get real date from the Internet

Postby Antonio Linares » Thu Sep 29, 2022 3:31 pm

Dear Hakan,

This way is working fine with Borland only. Just two warnings that we can safe ignore them
Code: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Antonio Linares » Thu Sep 29, 2022 3: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
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Antonio Linares » Thu Sep 29, 2022 4: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: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 32 guests