Get real date from the Internet
- Rick Lipkin
- Posts: 2668
- Joined: Fri Oct 07, 2005 1:50 pm
- Location: Columbia, South Carolina USA
Get real date from the Internet
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
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
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
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
Hi,
Is there any new news about this?
Thanks.
Is there any new news about this?
Thanks.
Regards,
Hakan ONEMLI
Harbour & MSVC 2022 & FWH 23.04
Hakan ONEMLI
Harbour & MSVC 2022 & FWH 23.04
- Antonio Linares
- Site Admin
- Posts: 42513
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Has thanked: 31 times
- Been thanked: 73 times
- Contact:
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
Re: Get real date from the Internet
This is one simple sample.
Not the best though.
We will be discussing other better alternatives.
![Image](https://imagizer.imageshack.com/v2/xq90/923/jVZSPG.png)
Not the best though.
We will be discussing other better alternatives.
Code: Select all | Expand
#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](https://imagizer.imageshack.com/v2/xq90/923/jVZSPG.png)
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
- Antonio Linares
- Site Admin
- Posts: 42513
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Has thanked: 31 times
- Been thanked: 73 times
- Contact:
Re: Get real date from the Internet
This is working great ![Smile :-)](./images/smilies/icon_smile.gif)
https://www.fivetechsoft.com/now.php
Where now.php is:
![Smile :-)](./images/smilies/icon_smile.gif)
https://www.fivetechsoft.com/now.php
Code: Select all | Expand
#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
<?php
echo gmdate('l jS \of F Y h:i:s A');
?>
- Antonio Linares
- Site Admin
- Posts: 42513
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Has thanked: 31 times
- Been thanked: 73 times
- Contact:
Re: Get real date from the Internet
Let me remind you that we have another very usefull GetIP()
https://www.fivetechsoft.com/getip.php
getip.php
https://www.fivetechsoft.com/getip.php
Code: Select all | Expand
#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
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
Re: Get real date from the Internet
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
***
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 ...
![Rolling Eyes :roll:](./images/smilies/icon_rolleyes.gif)
when change to 3600 it poll every Hour and DATE() Function will work correct
***
Code: Select all | Expand
HKEY_LOCAL_MACHINE \SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient
greeting,
Jimmy
Jimmy
Re: Get real date from the Internet
This topic is interesting but only the teacher Linares can understand it
[url]
https://stackoverflow.com/questions/667 ... rom-server
[/url]
Regards
[url]
https://stackoverflow.com/questions/667 ... rom-server
[/url]
Regards
- Antonio Linares
- Site Admin
- Posts: 42513
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Has thanked: 31 times
- Been thanked: 73 times
- Contact:
Re: Get real date from the Internet
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")
if you see it of utility please say so and we will convert it into a FWH function.
( 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
#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
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
#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
Hakan ONEMLI
Harbour & MSVC 2022 & FWH 23.04
- Antonio Linares
- Site Admin
- Posts: 42513
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Has thanked: 31 times
- Been thanked: 73 times
- Contact:
Re: Get real date from the Internet
Dear Hakan,
This way is working fine with Borland only. Just two warnings that we can safe ignore them
This way is working fine with Borland only. Just two warnings that we can safe ignore them
Code: Select all | Expand
#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 );
}
- Antonio Linares
- Site Admin
- Posts: 42513
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Has thanked: 31 times
- Been thanked: 73 times
- Contact:
Re: Get real date from the Internet
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.
With MSVC 2022 can't get it yet. Lots of warnings and errors. Trying to find why.
- Antonio Linares
- Site Admin
- Posts: 42513
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Has thanked: 31 times
- Been thanked: 73 times
- Contact:
Re: Get real date from the Internet
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 ![Smile :-)](./images/smilies/icon_smile.gif)
![Smile :-)](./images/smilies/icon_smile.gif)
Code: Select all | Expand
#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 );
}