import pytz
from datetime import datetime


def maccor_timestamp(x):
    """
    Helper function with exception handling for cases where the
    maccor cycler mis-prints the datetime stamp for the row. This
    happens when data is being recorded rapidly as the date switches over
    ie. between 10/21/2019 23:59:59 and 10/22/2019 00:00:00.

    Args:
        x (str): The datetime string for maccor in format '%m/%d/%Y %H:%M:%S'

    Returns:
        datetime.Datetime: Datetime object in iso format (daylight savings aware)

    """
    pacific = pytz.timezone('US/Pacific')
    utc = pytz.timezone('UTC')
    try:
        iso = pacific.localize(datetime.strptime(x, '%m/%d/%Y %H:%M:%S'),
                               is_dst=True).astimezone(utc).isoformat()
    except ValueError:
        x = x + ' 00:00:00'
        iso = pacific.localize(datetime.strptime(x, '%m/%d/%Y %H:%M:%S'),
                               is_dst=True).astimezone(utc).isoformat()
    return iso