from typing import Union
import time


def cast_timestamp_seconds_since_epoch_to_text(seconds: Union[int, float, str],
                                               timezone: str = "UTC",
                                               time_in_nanoseconds: bool = False,
                                               date_time_format: str = '%Y-%m-%d %H:%M:%S') -> str:
    """
    >>> assert cast_timestamp_seconds_since_epoch_to_text(0,"UTC") == '1970-01-01 00:00:00'
    >>> assert cast_timestamp_seconds_since_epoch_to_text("0","LOCAL")
    >>> assert cast_timestamp_seconds_since_epoch_to_text(1590674483 ,"UTC") == '2020-05-28 14:01:23'
    >>> assert cast_timestamp_seconds_since_epoch_to_text(1590674574765797619 ,"UTC", time_in_nanoseconds=True) == '2020-05-28 14:02:55'
    """
    seconds = float(seconds)

    if time_in_nanoseconds:
        seconds = seconds / 1E9

    seconds = int(round(seconds))

    if timezone == "UTC":
        epochseconds = time.gmtime(seconds)
    else:
        epochseconds = time.localtime(seconds)

    return time.strftime(date_time_format, epochseconds)