Ejemplo n.º 1
0
def get_date_from_quarter_string(timestamp):
    """
    Returns day 1 of quarter following <timestamp> argument.
    Example: for timestamp = '1q2015' returns date object with date '2015-04-01'
    <timestamp describes a date using a quarter-year  notation [1-4]q[YYYY], [1-4]Q[YYYY], [YYYY]q[1-4], [YYYY]Q[1-4]
    <timestamp> examples: 1Q2005, 2q2005, 2005Q3, 2005q4
    """
    # not todo: may use regex to catch year/quarter

    # break into (year, quarter) or (quarter, year)
    parts = timestamp.upper().split('Q')

    if len(parts) != 2:
        # not in a quarter format, break
        return None
    else:
        # make sure the order is (year, quater), swap to this format if otherwise
        if len(parts[0]) == 1:
            parts[0], parts[1] = parts[1], parts[0]

        # convert to integer checking the range
        year, quarter = int(parts[0]), int(parts[1])
        if quarter not in range(1, 5):
            raise ValueError("Quarter must be 1, 2, 3 or 4")

        # first quarter: month 4. last quarter: first month of next year
        month = quarter * 3
        dt = date(year=year, month=month, day=1)
        return shift_month_ahead(dt), "quarter"
Ejemplo n.º 2
0
def get_next_quarter_end_date(dt):
    """
    Returns the date corresponding to the next quarter end from <dt>.
    For example, if the current month is 1, it is currently in a valid
    quarter end. Months 2 and 3 would be changed to 4.
    """
    while (dt.month - 1) % 3 != 0:
        dt = shift_month_ahead(dt)

    return dt
def decompose_private_txt_filename (path, form):
    """
    Returns reporting date of file in <path> in ISO format. 
    """
    basename = os.path.basename(path)    
    year = int(get_parent_dirname(path))
    if form == '101':
        # not-todo: this may be handled using regex 
        # reads from f101_01.txt, f101_12.txt         
        month = int(basename[5:7]) 
        isodate = date2iso(shift_month_ahead(datetime.date(year, month, 1)))        
    if form == '102':  
        # reads from f102_1.txt, f102_4.txt
        quarter = int(basename[5])
        isodate = date2iso(quarter2date(year, quarter))    
    return isodate
Ejemplo n.º 4
0
def decompose_private_txt_filename(path, form):
    """
    Returns reporting date of file in <path> in ISO format. 
    """
    basename = os.path.basename(path)
    year = int(get_parent_dirname(path))
    if form == '101':
        # not-todo: this may be handled using regex
        # reads from f101_01.txt, f101_12.txt
        month = int(basename[5:7])
        isodate = date2iso(shift_month_ahead(datetime.date(year, month, 1)))
    if form == '102':
        # reads from f102_1.txt, f102_4.txt
        quarter = int(basename[5])
        isodate = date2iso(quarter2date(year, quarter))
    return isodate