from datetime import datetime


def read_mne_raw_edf_header(edf_obj, **kwargs):
    """
    Header extraction function for RawEDF and Raw objects.
    Reads the number of channels, channel names and sample rate properties
    If existing, reads the date information as well.

    Returns:
        Header information as dict
    """
    # Each tuple below follows the format:
    # 1) output name, 2) edf_obj name, 3) function to apply to the read
    # value, 4) whether a missing value should raise an error.
    header_map = [("n_channels", "nchan", int, True),
                  ("channel_names", "ch_names", list, True),
                  ("sample_rate", "sfreq", float, True),
                  ("date", "meas_date", datetime.utcfromtimestamp, False)]
    if isinstance(edf_obj.info["meas_date"], (tuple, list)):
        assert edf_obj.info["meas_date"][1] == 0
        edf_obj.info["meas_date"] = edf_obj.info["meas_date"][0]
    header = {}
    for renamed, org, transform, raise_err in header_map:
        value = edf_obj.info.get(org)
        try:
            value = transform(value)
        except Exception as e:
            if raise_err:
                raise ValueError("Missing or invalid value in EDF for key {} "
                                 "- got {}".format(org, value)) from e
        header[renamed] = value
    return header