简体   繁体   中英

Convert US/Eastern to UTC including daylight savings Python

I was really surprised to not find an easy way to do this in Python. We wrote this in December and it passed, because it was winter:

def get_utc_time(dt_or_str, number_of_days=None, time_format=DATETIME_FORMAT):
    """

    :param dt_or_str:
    :return:
    """
    eastern = pytz.timezone('US/Eastern')

    # try:
    if isinstance(dt_or_str, datetime):
        new_date_str = dt_or_str.strftime(time_format)
        new_date_obj = dt_or_str
    else:
        new_date_str = dt_or_str
        new_date_obj = datetime.strptime(dt_or_str, time_format)

    if not number_of_days:
        utc_time = eastern.localize(
            datetime.strptime(new_date_str, time_format),
            is_dst=None).astimezone(pytz.utc)
    else:
        est_time = new_date_obj - timedelta(days=number_of_days)
        utc_time = eastern.localize(est_time, is_dst=None).astimezone(pytz.utc)

    utc_time = utc_time.replace(tzinfo=None)

What this did is add 5 hours to your time to make it UTC. It turns out in Spring/Summer Eastern time is only 4 hours behind UTC, so our code is now broken.

This is the test we wrote that does not work in Summer:

def test_get_utc_time_incoming_string(self):
    result = get_utc_time("2017-02-02 04:38")
    self.assertEqual(result, datetime.datetime(2017, 2, 2, 8, 38))

How can you convert EST to UTC that will work year round? I don't want to hard code it since daylight savings period changes each year.

Found this on an answer:

def is_daylight_savings(timezone_name):
    tz = pytz.timezone(timezone_name)
    now = pytz.utc.localize(datetime.utcnow())
    return now.astimezone(tz).dst() != timedelta(0)

so now this:

def get_utc_time(dt_or_str, number_of_days=None, time_format=DATETIME_FORMAT):
    """
    :param dt_or_str:
    :return:
    """
    eastern = pytz.timezone(TIMEZONE)
    it_is_daylight_savings = is_daylight_savings(TIMEZONE)

    if isinstance(dt_or_str, datetime):
        new_date_str = dt_or_str.strftime(time_format)
        new_date_obj = dt_or_str
    else:
        new_date_str = dt_or_str
        new_date_obj = datetime.strptime(dt_or_str, time_format)

    if not number_of_days:
        utc_time = eastern.localize(
            datetime.strptime(new_date_str, time_format),
            is_dst=it_is_daylight_savings).astimezone(pytz.utc)
    else:
        est_time = new_date_obj - timedelta(days=number_of_days)
        utc_time = eastern.localize(est_time, is_dst=it_is_daylight_savings).astimezone(pytz.utc)

    utc_time = utc_time.replace(tzinfo=None)

    return utc_time

as freedom of speech dies, freedom to be rude increases exponentionally

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM