Ejemplo n.º 1
0
def date_and_time(value, formatting="N j, Y, h:i a"):
    """
    Reformats a date string in a humanized format, AP style by default.
    """
    try:
        dt = dateparse(value)
    except ValueError:
        return value
    return dateformater(dt, formatting)
Ejemplo n.º 2
0
def date_and_time(value, formatting="N j, Y, h:i a"):
    """
    Reformats a date string in a humanized format, AP style by default.
    """
    try:
        dt = dateparse(value)
    except ValueError:
        return value
    return dateformater(dt, formatting)
Ejemplo n.º 3
0
def short_ap_date(value, date_format=None, upper=False):
    """
    Reformats a date string as in an abbreviated AP format.
    
        Example:
        
             >> short_ap_date('2010-04-03')
            'Apr. 2, 2011'
    
    If the date format cannot be automatically detected, you can specify it
    with the keyword argument.
    """
    # Split any date ranges and create a list
    value = value.replace("–", "-")
    date_parts = value.split(" - ")
    date_list = []
    for date_string in date_parts:
        try:
            if date_format:
                dt = datetime.strptime(date_string, date_format)
            else:
                dt = dateparse(date_string)
        except ValueError:
            return value
        # Check if this date is a "month-only" date
        # that needs to be specially formatted.
        if re.match('^\w{3,}\.?\s\d{4}$', date_string):
            dt = dateformater(dt, "M Y")
        # Otherwise just use the standard format
        else:
            dt = dateformater(dt, "M j, Y")
        # All months except May are abbreviated
        # and need a period added.
        if not dt.startswith("May"):
            dt = dt[:3] + "." + dt[3:]
        if upper == True:
            date_list.append(dt.upper())
        else:
            date_list.append(dt)
    return " – ".join(date_list)
Ejemplo n.º 4
0
def short_ap_date(value, date_format=None):
    """
    Reformats a date string as in an abbreviated AP format.
    
        Example:
        
             >> short_ap_date('2010-04-03')
            'Apr. 2, 2011'
    
    If the date format cannot be automatically detected, you can specify it
    with the keyword argument.
    """
    # Split any date ranges and create a list
    value = value.replace("–", "-")
    date_parts = value.split(" - ")
    date_list = []
    for date_string in date_parts:
        try:
            if date_format:
                dt = datetime.strptime(date_string, date_format)
            else:
                dt = dateparse(date_string)
        except ValueError:
            return value
        # Check if this date is a "month-only" date
        # that needs to be specially formatted.
        if re.match('^\w{3,}\.?\s\d{4}$', date_string):
            dt = dateformater(dt, "M Y")
        # Otherwise just use the standard format
        else:
            dt = dateformater(dt, "M j, Y")
        # All months except May are abbreviated
        # and need a period added.
        if not dt.startswith("May"):
            dt = dt[:3] + "." + dt[3:]
        date_list.append(dt)
    return " – ".join(date_list)