How to Get a Timestamp from an NTP Server

Updated Aug 12, 2025

Environment

Software

  • LabVIEW

This article explains how to retrieve a timestamp from an NTP server using LabVIEW, with the Meinberg LANTIME M1000 as an example. The LANTIME M1000 is a high-precision NTP/PTP time server that supports UDP-based NTP requests. To obtain a timestamp, a standard NTP client must send a request packet to UDP port 123.

The process involves four main steps:

  1. Configure the NTP server’s IP address and port, and create a UDP socket
  2. Construct and send an NTP request packet via UDP
  3. Receive the response and extract the "Transmit Timestamp" from the NTP packet
  4. Convert the NTP time to UNIX time, then to a LabVIEW timestamp

To retrieve a timestamp from an NTP server using unicast UDP, follow these steps:

1. Configure the NTP Server IP and Create a UDP Socket

Set the NTP server’s IP address and port number. For standard NTP communication, use UDP port 123.
In this example, the NTP server IP is 10.152.4.60.

2. Construct and Send an NTP Request Packet

An NTP request is a 48-byte packet sent to UDP port 123. The basic structure is:

  • Byte 1: LI (Leap Indicator), VN (Version Number), Mode
    Example: 0x1B (LI=0, VN=3, Mode=3 for client mode)
  • Bytes 2–48: Typically zero-filled

This results in a 48-byte request where the first byte is 27 in decimal (from 0x1B), and the remaining bytes are zeros.

3. Receive the Response and Extract the Transmit Timestamp

The NTP response is also a 48-byte packet. The last 8 bytes contain the Transmit Timestamp:

  • First 4 bytes: Integer part of the timestamp
  • Last 4 bytes: Fractional part, which should be divided by 232232 to get the decimal portion

4. Convert NTP Time to LabVIEW Timestamp

To convert the NTP time to a LabVIEW timestamp, use the following formulas:

  • UNIX Time = NTP Time − 2208988800
  • LabVIEW Timestamp = UNIX Time + 2082844800
  • Therefore:
    LabVIEW Timestamp = NTP Time − 126144000

Using the provided NTP TimeSync.vi, you can retrieve timestamps from a standard NTP server.

If you want to perform the same operation in Python, here’s a sample code: 

import socket
import struct
import datetime

def get_ntp_time(ntp_server="10.152.4.60"):
    port = 123
    address = (ntp_server, port)
    msg = b'\x1b' + 47 * b'\0'  # Create a 48-byte NTP request packet (first byte: LI=0, VN=3, Mode=3)

    client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    client.settimeout(5)  # Set timeout to 5 seconds

    try:
        print(f"Sending NTP request → {ntp_server}:{port}")
        client.sendto(msg, address)  # Send request to NTP server
        msg, _ = client.recvfrom(1024)  # Receive response
        print("Response received")

        # Extract NTP timestamp (Transmit Timestamp is at index 10)
        t = struct.unpack("!12I", msg)[10]
        t -= 2208988800  # Convert NTP time to UNIX time
        ntp_time = datetime.datetime.fromtimestamp(t).astimezone()
        print("NTP time (local):", ntp_time)

        # Reverse DNS lookup to get hostname
        try:
            hostname = socket.gethostbyaddr(ntp_server)[0]
            print("Server name (hostname):", hostname)
        except socket.herror:
            print("Hostname not found (reverse lookup failed)")

    except socket.timeout:
        print("Timeout: No response from NTP server")
    except Exception as e:
        print(f"Error occurred: {e}")
    finally:
        client.close()

get_ntp_time()