import datetime


def systemtime_to_datetime(systemtime):
    """
    Convert the LabOne "systemtime" returned in LabOne data headers from
    microseconds since Unix epoch to a datetime object with microsecond
    precision.

    Example:

      import zhinst.examples as ziex
      import zhinst.utils as ziutils
      data = ziex.common.example_sweeper.run_example('dev2006')
      systemtime = data[0][0]['header']['systemtime'][0]
      t_datetime = ziutils.systemtime_to_datetime(systemtime)
      t_datetime.strftime('%Y-%m-%dT%H:%M:%S.%f')
    """
    systemtime_sec, systemtime_microsec = divmod(systemtime, 1e6)
    # Create a datetime object from epoch timestamp with 0 microseconds.
    t = datetime.datetime.fromtimestamp(systemtime_sec)
    # Set the number of microseconds in the datetime object.
    t = t.replace(microsecond=int(systemtime_microsec))
    return t