prueba con este: A mi me funciona correctamente en delphi 7
Código:
unit Unit2;
interface
uses
Windows, SysUtils, IdHTTP, IdSSLOpenSSL, DateUtils;
function LeeFechaHoraInternet: string;
implementation
function GetTimeZoneOffset: TDateTime;
var
TZInfo: TTimeZoneInformation;
Bias: Integer;
begin
case GetTimeZoneInformation(TZInfo) of
TIME_ZONE_ID_STANDARD, TIME_ZONE_ID_DAYLIGHT:
Bias := TZInfo.Bias + TZInfo.DaylightBias; // Ajuste según horario de verano
else
Bias := 0; // Si no se puede determinar la zona horaria, se asume UTC
end;
Result := Bias / MinsPerDay; // Convertir minutos a días
end;
function LeeFechaHoraInternet: string;
var
Http: TIdHTTP;
SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
Respuesta: string;
UnixTicks: Int64;
FechaHoraUTC, FechaHoraLocal: TDateTime;
TimeZoneOffset: TDateTime;
begin
Result := FormatDateTime('yyyy-mm-dd"T"hh:nn:ss"Z"', Now); // Valor predeterminado
Http := TIdHTTP.Create(nil);
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
SSLHandler.SSLOptions.Method := sslvTLSv1_2; // Usar TLS 1.2
SSLHandler.SSLOptions.SSLVersions := [sslvTLSv1_2];
SSLHandler.SSLOptions.Mode := sslmClient; // Modo cliente
// Configurar cliente con este manejador SSL
// IdHTTP1.IOHandler := SSLHandler;
// IdHTTP1.Request.UserAgent := 'Mozilla/5.0';
// Realiza la conexión HTTP/HTTPS
// IdHTTP1.Get('https://yourserver.com');
Http.IOHandler := SSLHandler;
Http.Request.Accept := 'application/json';
Http.Request.UserAgent := 'Mozilla/5.0 (compatible; Delphi)';
try
// Realizar la solicitud HTTP
Respuesta := Http.Get('https://www2.roa.es/cgi-bin/horautc');
// Respuesta := Http.Get('https://www.google.com');
// Convertir la respuesta (ticks Unix) a DateTime
UnixTicks := StrToInt64(Trim(Respuesta));
FechaHoraUTC := UnixDateDelta + (UnixTicks div SecsPerDay) + (UnixTicks mod SecsPerDay) / SecsPerDay;
// Obtener el desfase horario
TimeZoneOffset := GetTimeZoneOffset;
FechaHoraLocal := FechaHoraUTC - TimeZoneOffset;
// Devolver la fecha y hora en formato ISO 8601
Result := FormatDateTime('yyyy-mm-dd"T"hh:nn:ss"Z"', FechaHoraLocal);
except
on E: Exception do
// En caso de error, devolver la hora actual
result := E.Message;
// Result := FormatDateTime('yyyy-mm-dd"T"hh:nn:ss"Z"', Now);
end;
finally
Http.Free;
SSLHandler.Free;
end;
end;
end.