交易日期处理

import time
import datetime
import calendar

time_format = '%Y%m%d%H%M%S'
date_format = '%Y%m%d'

'''格式化日期 - yyyyMMdd'''
def getFormatDate(date=None):
    return date.strftime(date_format)

'''格式化时间 - yyyyMMddHHmmss'''
def getFormatTime(time=None):
    return time.strftime(time_format)

'''string to time'''
def getTimeByStr(cur_date=None):
    cur_time = datetime.datetime.strptime(cur_date, date_format)
    return cur_time

'''获取当前时间 - yyyyMMddHHmmss'''
def getCurTime():
    cur_time = time.strftime(time_format)
    return cur_time

'''获取当前日期 - yyyyMMdd'''
def getCurDate():
    cur_date = time.strftime(date_format)
    return cur_date

'''获取后面第N天 - yyyyMMdd'''
def getNextDate(cur_date=None, days=0):
    cur_time = getTimeByStr(cur_date)
    next_time = cur_time + datetime.timedelta(days)
    next_date = next_time.strftime(date_format)
    return next_date

'''获取前面第N天 - yyyyMMdd'''
def getPrevDate(cur_date=None, days=0):
    cur_time = getTimeByStr(cur_date)
    prev_time = cur_time - datetime.timedelta(days)
    prev_date = prev_time.strftime(date_format)
    return prev_date

'''校验是否交易日 - 1/0'''
def isTrdDate(conn=None, cur_date=None):
    sql_sel = 'select hs_flag from trade_day where t_date=%s'
#    print('sql_sel',sql_sel)
    cur = conn.cursor()
    cur.execute(sql_sel, (cur_date,))
    hs_flag = cur.fetchone()[0]
    cur.close()
    return hs_flag

'''获取最近交易日 - yyyyMMdd'''
def getRecentTrdDate(conn=None, cur_date=None):
    while isTrdDate(conn, cur_date)!='1':
        cur_date = getPrevDate(cur_date, 1)
        
    return cur_date

'''获取上一个交易日 - yyyyMMdd'''
def getPrevTrdDate(conn=None, cur_date=None):
    recent_date = getRecentTrdDate(conn, cur_date)
    prev_date = getPrevDate(recent_date, 1)
    recent_trddate = getRecentTrdDate(conn, prev_date)
    return recent_trddate

'''获取下一个交易日 - yyyyMMdd'''
def getNextTrdDate(conn=None, cur_date=None):
    recent_date = getRecentTrdDate(conn, cur_date)
    next_date = getNextDate(recent_date, 1)
    while isTrdDate(conn, next_date)!='1':
        next_date = getNextDate(next_date, 1)
    
    return next_date

'''获取下N个交易日 - yyyyMMdd'''
def getNextTrdDateN(conn=None, cur_date=None, days=1):
    while days>0:
        cur_date = getNextTrdDate(conn, cur_date)
        days = days - 1
    
    return cur_date

'''获取该周第一天 - yyyyMMdd'''
def getFristDateByWeek(cur_date=None):
    cur_time = getTimeByStr(cur_date)
    monday = cur_time - datetime.timedelta(cur_time.weekday())
    return getFormatDate(monday)

'''获取该周最后一天 - yyyyMMdd'''
def getLastDateByWeek(cur_date=None):
    cur_time = getTimeByStr(cur_date)
    sunday = cur_time + datetime.timedelta(6 - cur_time.weekday())
    return getFormatDate(sunday)

'''获取该周首尾交易日 - yyyyMMdd'''
def getBothTrdDateByWeek(conn=None, cur_date=None):
    monday = getFristDateByWeek(cur_date)
    sunday = getLastDateByWeek(cur_date)
    
    prev_date = getPrevDate(monday, 1)
    frist_trd_date = getNextTrdDate(conn, prev_date)
    diff = int(sunday) - int(frist_trd_date)
    if diff<0:
        frist_trd_date = ' '
    
    last_trd_date = getRecentTrdDate(conn, sunday)
    diff = int(monday) - int(last_trd_date)
    if diff>0:
        last_trd_date = ' '
    
    return frist_trd_date, last_trd_date
    
'''获取该月第一天 - yyyyMMdd'''
def getFristDateByMonth(cur_date=None):
    year = int(cur_date[0:4])
    month = int(cur_date[4:6])
    first_date = datetime.date(year=year, month=month, day=1)
    return getFormatDate(first_date)

'''获取该月第一个交易日 - yyyyMMdd'''
def getFristTrdDateByMonth(conn=None, cur_date=None):
    frist_date = getFristDateByMonth(cur_date)
    prev_date = getPrevDate(frist_date, 1)
    trd_date = getNextTrdDate(conn, prev_date)
    return trd_date

'''获取该月最后一天 - yyyyMMdd'''
def getLastDateByMonth(cur_date=None):
    year = int(cur_date[0:4])
    month = int(cur_date[4:6])
    month_range = calendar.monthrange(year, month)[1]
    last_date = datetime.date(year=year, month=month, day=month_range)
    return getFormatDate(last_date)

'''获取该月最后一个交易日 - yyyyMMdd'''
def getLastTrdDateByMonth(conn=None, cur_date=None):
    last_date = getLastDateByMonth(cur_date)
    trd_date = getRecentTrdDate(conn, last_date)
    return trd_date

'''获取该季度首尾日期 - yyyyMMdd'''
def getBothDateByQuarter(cur_date=None):
    s_year = cur_date[0:4]
    month = int(cur_date[4:6])
    if (month>12) or (month<1):
        err = 'date error = %s' % cur_date
        raise Exception(err)
    
    month = month - 1
    quarter = int(month/3) + 1;
    frist_date = '%s%s'
    last_date = '%s%s'
    
    if quarter==1:
        frist_date = frist_date % (s_year, '0101')
        last_date = last_date % (s_year, '0331')
    elif quarter==2:
        frist_date = frist_date % (s_year, '0401')
        last_date = last_date % (s_year, '0630')
    elif quarter==3:
        frist_date = frist_date % (s_year, '0701')
        last_date = last_date % (s_year, '0930')
    elif quarter==4:
        frist_date = frist_date % (s_year, '1001')
        last_date = last_date % (s_year, '1231')
    else:
        frist_date = None
        last_date = None
    
    return frist_date, last_date


'''获取该季度首尾交易日 - yyyyMMdd'''
def getBothTrdDateByQuarter(conn=None, cur_date=None):
    frist_date, last_date = getBothDateByQuarter(cur_date)
    
    prev_date = getPrevDate(frist_date, 1)
    frist_trd_date = getNextTrdDate(conn, prev_date)
    last_trd_date = getRecentTrdDate(conn, last_date)
    return frist_trd_date, last_trd_date
    
'''下一个自然日是否为自然周第一个交易日 - yyyyMMdd'''
def isFristTrdDateByWeekForNextDate(conn=None, cur_date=None):
    result = False
    
    next_date = getNextDate(cur_date, 1)
    monday = getFristDateByWeek(next_date)
    prev_date = getPrevDate(monday, 1)
    frist_trd_date = getNextTrdDate(conn, prev_date)
    
    if next_date==frist_trd_date:
        result = True
    else:
        result = False
    
    return result
    
    
'''获取下个月第一个自然日'''
def getFristDateByNextMonth(cur_date=None):
    year = int(cur_date[0:4])
    month = int(cur_date[4:6])
    day = int(cur_date[6:8])
    
    monthrange = calendar.monthrange(year, month)[1]
    interval = monthrange - day + 1
    frist_day = getNextDate(cur_date, interval)
    
    return frist_day


'''获取下个月首尾交易日 - yyyyMMdd'''
def getBothTrdDateByNextMonth(conn=None, cur_date=None):
    frist_day = getFristDateByNextMonth(cur_date)
    frist_trd_date = getFristTrdDateByMonth(conn, frist_day)
    last_trd_date = getLastTrdDateByMonth(conn, frist_day)
    return frist_trd_date, last_trd_date


'''获取下一季首尾交易日 - yyyyMMdd'''
def getBothTrdDateByNextQuarter(conn=None, cur_date=None):
    cur_last_date = getBothDateByQuarter(cur_date)[1]
    next_frist_date = getNextDate(cur_last_date, 1)
    return getBothTrdDateByQuarter(conn, next_frist_date)


'''获取上N个交易日 - yyyyMMdd'''


def getPreTrdDateN(conn=None, cur_date=None, days=1):
    while days > 0:
        cur_date = getPrevTrdDate(conn, cur_date)
        days = days - 1

    return cur_date

猜你喜欢

转载自blog.csdn.net/changyan_123/article/details/84573657