Reading and Accessing Internet Server Time

67 views
Skip to first unread message

valt...@valtecom.com.br

unread,
Dec 5, 2025, 2:23:58 PM (10 days ago) Dec 5
to Harbour Minigui
Good afternoon, group members,
I'm writing from Brazil in South America, and here we use Brasilia time, our capital, as a reference. I've already asked this question here and haven't received an answer; perhaps today I'll get a precise answer.

I use Harbour and Minigui Extend STD, and I'd like to know if there's a function in Harbour or Minigui where I can read and access the date and time of Brasilia. The reason is that my systems read the date and time from the installed computer, but in some clients, for some unexplained reason, someone changes the computer's date and time, and the system then saves this incorrect information. I'd like a way to read and access the Brasilia date and time so I don't depend on the local computer's date and time, thus avoiding errors that are beyond my control. Otherwise, I'll have to create a day-opening and closing routine, and my systems aren't prepared for this method, and the change would be quite significant.

I hope I've made myself clear.

Thank you.

Valteçom
Uberaba MG Brazil.

Marcos Jarrin

unread,
Dec 5, 2025, 3:55:03 PM (9 days ago) Dec 5
to Harbour Minigui
Hello
This code returns the date and time.
Compatible with Borland 582

#include "minigui.ch"

function Main()
   MsgInfo( Now() )
return nil

#pragma BEGINDUMP

#include <hbapi.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

#ifdef __BORLANDC__
  #include <winsock2.h>
  #include <windows.h> // For LoadLibrary/GetProcAddress
#else
  #include <winsock2.h>
  #include <ws2tcpip.h>
#endif

HB_FUNC( NOW )
{
    // Declare ALL variables at the beginning (requirement for Borland C++ 5.82)
    char buf[256];
    time_t now;
    struct tm *tm_info;
    char time_str[64];
    WSADATA wsaData;
    struct hostent *he;
    int wsainitialized = 0;

    // Initialize variables
    now = time(NULL);
    tm_info = localtime(&now);

    // Format time manually
    if (tm_info) {
        sprintf(time_str, "%02d/%02d/%04d %02d:%02d:%02d",
                tm_info->tm_mday,
                tm_info->tm_mon + 1,
                tm_info->tm_year + 1900,
                tm_info->tm_hour,
                tm_info->tm_min,
                tm_info->tm_sec);
    } else {
        strcpy(time_str, "Error getting time");
    }

    // Check network connection using gethostbyname (more compatible with BCC 5.82)
    if (WSAStartup(MAKEWORD(2,2), &wsaData) == 0) {
        wsainitialized = 1;

        // Try to resolve google.com
        he = gethostbyname("www.google.com");
        if (he != NULL) {
            sprintf(buf, "Time: %s - Network: Connected", time_str);
        } else {
            sprintf(buf, "Time: %s - Network: No connection", time_str);
        }

        WSACleanup();
    } else {
        sprintf(buf, "Time: %s - Winsock: Error", time_str);
    }

    hb_retc(buf);
}

#pragma ENDDUMP


Regards,
Marcos Jarrin

Ivanil Marcelino

unread,
Dec 5, 2025, 5:20:24 PM (9 days ago) Dec 5
to valt...@valtecom.com.br, Harbour Minigui
FUNCTION GetHoraBrasilia()
    LOCAL oPg, cBuf, hBuf
    LOCAL cURL := "http://worldtimeapi.org/api/timezone/America/Sao_Paulo"
    oPg := CreateObject( "Microsoft.XMLHTTP" )
    oPg:Open( "GET", cURL, .f. )     // síncrono
    oPg:Send()
    cBuf := oPg:responseBody
    hb_jsonDecode( cBuf, @hBuf )
    IF ValType( hBuf ) != "H"
        RETURN ctod("")
    ENDIF
    Return ctot(hBuf[ "datetime" ],'yyyy-mm-dd')

--
Visit our website on https://www.hmgextended.com/ or https://www.hmgextended.org/
---
You received this message because you are subscribed to the Google Groups "Harbour Minigui" group.
To unsubscribe from this group and stop receiving emails from it, send an email to minigui-foru...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/minigui-forum/a7e820d9-e706-473f-b723-498cc1dc8e6an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages