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
#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
#include "FiveWin.ch"
function Main()
MsgInfo( Now() )
return nil
function Now()
return WebPageContents( "https://www.fivetechsoft.com/now.php" )
<?php
echo gmdate('l jS \of F Y h:i:s A');
?>
#include "FiveWin.ch"
function Main()
MsgInfo( GetIP() )
return nil
function GetIP()
return WebPageContents( "https://www.fivetechsoft.com/getip.php" )
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
HKEY_LOCAL_MACHINE \SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient
test
Current time: Wed Sep 28 23:17:15 2022
#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 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;
}
#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 );
}
#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 );
}
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 55 guests