from datetime import datetime
import pytz


def to_utc(d: datetime, timezone: str) -> datetime:
    """
    helper to convert from datetime(naive or aware) with timezone name to utc datetime.
    throws error when failed to utc
    :param d: from datetime
    :param timezone: timezone name like "US/Eastern"
    :return: datetime converted to utc
    """
    tz_obj = pytz.timezone(timezone)
    if d.tzinfo is None:
        d_aware_tz = tz_obj.localize(d, is_dst=None)
    else:
        d_aware_tz = d
    return d_aware_tz.astimezone(pytz.utc)