from datetime import datetime
from dateutil import relativedelta


def calculate_time_difference(startDate, endDate):
    """
    *Return the time difference between two dates as a string*

    **Key Arguments:**
        - ``startDate`` -- the first date in YYYY-MM-DDTHH:MM:SS format
        - ``endDate`` -- the final date YYYY-MM-DDTHH:MM:SS format

    **Return:**
        - ``relTime`` -- the difference between the two dates in Y,M,D,h,m,s (string)

    **Usage:**
        .. code-block:: python 

            from fundamentals import times
            diff = times.calculate_time_difference(startDate="2015-10-13 10:02:12", endDate="2017-11-04 16:47:05")
            print diff

            # OUT: 2yrs 22dys 6h 44m 53s
    """
    ################ > IMPORTS ################

    ################ > VARIABLE SETTINGS ######

    ################ >ACTION(S) ################
    if "T" not in startDate:
        startDate = startDate.strip().replace(" ", "T")
    if "T" not in endDate:
        endDate = endDate.strip().replace(" ", "T")
    startDate = datetime.strptime(startDate, '%Y-%m-%dT%H:%M:%S')
    endDate = datetime.strptime(endDate, '%Y-%m-%dT%H:%M:%S')
    d = relativedelta.relativedelta(endDate, startDate)

    relTime = ""
    if d.years > 0:
        relTime += str(d.years) + "yrs "
    if d.months > 0:
        relTime += str(d.months) + "mths "
    if d.days > 0:
        relTime += str(d.days) + "dys "
    if d.hours > 0:
        relTime += str(d.hours) + "h "
    if d.minutes > 0:
        relTime += str(d.minutes) + "m "
    if d.seconds > 0:
        relTime += str(d.seconds) + "s"
    ###############################

    if relTime == "":
        relTime = "0s"

    return relTime