Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Proyecto SIF/Veri*Factu/Ley Antifraude > Envío de registros y sus respuestas
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

 
 
Herramientas Buscar en Tema Desplegado
  #11  
Antiguo 05-11-2024
mqm mqm is offline
Miembro
 
Registrado: nov 2006
Posts: 62
Poder: 20
mqm Va por buen camino
Aqui teneis en codigo en delphi 11.

Código Delphi [-]
unit VerificaFecha;
interface

uses
  IdUDPClient, IdGlobal, SysUtils, DateUtils;

function GetNetworkTime: TDateTime;

implementation

function SwapEndianness(Value: UInt32): UInt32;
begin
  Result := ((Value and $000000FF) shl 24) or
            ((Value and $0000FF00) shl 8) or
            ((Value and $00FF0000) shr 8) or
            ((Value and $FF000000) shr 24);
end;

function GetNetworkTime: TDateTime;
const
  NTP_SERVER = 'time.windows.com';
  NTP_PORT = 123;
  NTP_PACKET_SIZE = 48;
  NTP_TIMESTAMP_OFFSET = 2208988800; // Seconds between 1900 and 1970
  SERVER_REPLY_TIME = 40; // Offset to "Transmit Timestamp"
var
  UDPClient: TIdUDPClient;
  NTPData: TIdBytes;
  Seconds, Fraction: UInt32;
  UnixTime: UInt64;
begin
  UDPClient := TIdUDPClient.Create(nil);
  try
    UDPClient.Host := NTP_SERVER;
    UDPClient.Port := NTP_PORT;
    UDPClient.BufferSize := NTP_PACKET_SIZE;

    SetLength(NTPData, NTP_PACKET_SIZE);
    FillChar(NTPData[0], NTP_PACKET_SIZE, 0);

    // Set LI, VN, Mode
    NTPData[0] := $1B;

    // Send request
    UDPClient.Connect;
    UDPClient.SendBuffer(NTPData);

    // Receive response
    UDPClient.ReceiveTimeout := 3000;
    UDPClient.ReceiveBuffer(NTPData);

    // Extract timestamp
    Seconds := SwapEndianness(PCardinal(@NTPData[SERVER_REPLY_TIME])^);
    Fraction := SwapEndianness(PCardinal(@NTPData[SERVER_REPLY_TIME + 4])^);

    UnixTime := Seconds - NTP_TIMESTAMP_OFFSET;
    Result := UnixDateDelta + UnixTime / SecsPerDay;
    Result := Result + (Fraction / High(UInt32) / SecsPerDay);

    // Convert to local time
    Result := TTimeZone.Local.ToLocalTime(Result);
  finally
    UDPClient.Free;
  end;
end;

end.

Y ejemplo de llamada :
procedure TForm3.Button1Click(Sender: TObject);
var
  NetworkTime: TDateTime;
begin
  try
    NetworkTime := GetNetworkTime;
    ShowMessage('La hora de red es: ' + DateTimeToStr(NetworkTime));
  except
    on E: Exception do
      ShowMessage('Error al obtener la hora de red: ' + E.Message);
  end;
end;

Última edición por Neftali [Germán.Estévez] fecha: 05-11-2024 a las 12:13:46. Razón: Añadir TAG's al código
Responder Con Cita
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Como le resto una hora a la fecha y hora en sql server uper MS SQL Server 3 12-10-2015 20:41:49
Agrupar por fecha, teniendo en el campo fecha y hora El_Raso Firebird e Interbase 3 18-03-2010 22:05:19
obtener solo la fecha en formato fecha y sin hora BlueSteel SQL 14 09-05-2008 16:42:19
formato fecha y hora a solo Fecha ozegarra Firebird e Interbase 6 22-02-2008 18:43:34
Extraccion fecha de un campo fecha y hora matti Firebird e Interbase 3 26-04-2007 19:48:11


La franja horaria es GMT +2. Ahora son las 17:56:10.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi