def year_quarter(target_date_obj):
    """Returns the yearly quarter (coded as 0,1,2,3) in which a given
    target datetime object lies.

    Args:
      target_date_obj: target date as a datetime object
    """
    m = target_date_obj.month
    if m >= 12 or m <= 2: # December - February
        return 0
    elif m >= 3 and m <= 5: # March - May
        return 1
    elif m >= 6 and m <= 8: # June - August
        return 2
    elif m >= 9 and m <= 11: #  September - November
        return 3
    else:
        raise ValueError(f"Invalid month {m}")