from warnings import warn


def write_last_state(datetime_last, fname):
    """
    writes SwissMetNet data in format datetime,avg_value, std_value

    Parameters
    ----------
    datetime_last : datetime object
        date and time of the last state
    fname : str
        file name where to store the data

    Returns
    -------
    fname : str
        the name of the file where data has written

    """
    try:
        with open(fname, 'w', newline='') as txtfile:
            txtfile.write(datetime_last.strftime('%Y%m%d%H%M%S'))
            txtfile.close()

            return fname
    except EnvironmentError:
        warn('Unable to write on file '+fname)
        return None