from datetime import datetime
import pytz


def datetime_in_milliseconds(dt):
    """
    :param dt: Instance of datetime.datetime
    :return: Unix timestamp in milliseconds since epoch
    """
    try:
        return int(dt.timestamp() * 1000)
    except AttributeError:
        _EPOCH = datetime(1970, 1, 1, tzinfo=pytz.utc)
        return int((dt - _EPOCH).total_seconds() * 1000)