# Path: kquant_data/wind/utils.py
# def asDateTime(v, asDate=False):
#     """
#     万得中读出来的时间总多5ms，覆写这部分
#     w.asDateTime = asDateTime
#     w.start()
#     :param v:
#     :param asDate:
#     :return:
#     """
#     # return datetime(1899, 12, 30, 0, 0, 0, 0) + timedelta(v + 0.005 / 3600 / 24)
#     return datetime(1899, 12, 30, 0, 0, 0, 0) + timedelta(v)
# 
# Path: kquant_data/utils/xdatetime.py
# def datetime_2_yyyy(dt):
#     """
#     将时间转换成float类型
#     :param dt:
#     :return:
#     """
#     t = dt.timetuple()
#     return t.tm_year
# 
# Path: kquant_data/utils/xdatetime.py
# def datetime_2_yyyyMMdd(dt):
#     """
#     将时间转换成float类型
#     :param dt:
#     :return:
#     """
#     t = dt.timetuple()
#     return int((t.tm_year * 10000.0 + t.tm_mon * 100 + t.tm_mday))


from WindPy import w
from .utils import asDateTime
from ..utils.xdatetime import datetime_2_yyyy
from ..utils.xdatetime import datetime_2_yyyyMMdd
import os
import pandas as pd
    :param w:
    :param wind_codes:
    :param year:
    :return:
    """
    w.asDateTime = asDateTime
    w_wss_data = w.wss(wind_codes, "fellow_listeddate", "year=%s" % year)
    df = pd.DataFrame(w_wss_data.Data)
    df = df.T
    df.columns = ['fellow_listeddate']
    df.index = w_wss_data.Codes
    return df


def download_ipo_last_trade_trading(w, wind_codes):
    # 黄金从20130625开始将最小变动价位从0.01调整成了0.05，但从万得上查出来还是完全一样，所以没有必要记录mfprice
    # 郑商所在修改合约交易单位时都改了合约代码，所以没有必要记录contractmultiplier
    w.asDateTime = asDateTime
    w_wss_data = w.wss(wind_codes, "sec_name,ipo_date,lasttrade_date,lasttradingdate", "")
    grid = w_wss_data.Data

    # T1803一类的会被当成时间，需要提前转置
    new_grid = [[row[i] for row in grid] for i in range(len(grid[0]))]

    df = pd.DataFrame(new_grid)
    df.columns = ['sec_name', 'ipo_date', 'lasttrade_date', 'lasttradingdate']
    df.index = w_wss_data.Codes
    df.index.name = 'wind_code'

    df['ipo_date'] = df['ipo_date'].apply(datetime_2_yyyyMMdd)

# Path: kquant_data/wind/utils.py
# def asDateTime(v, asDate=False):
#     """
#     万得中读出来的时间总多5ms，覆写这部分
#     w.asDateTime = asDateTime
#     w.start()
#     :param v:
#     :param asDate:
#     :return:
#     """
#     # return datetime(1899, 12, 30, 0, 0, 0, 0) + timedelta(v + 0.005 / 3600 / 24)
#     return datetime(1899, 12, 30, 0, 0, 0, 0) + timedelta(v)
# 
# Path: kquant_data/utils/xdatetime.py
# def datetime_2_yyyy(dt):
#     """
#     将时间转换成float类型
#     :param dt:
#     :return:
#     """
#     t = dt.timetuple()
#     return t.tm_year
# 
# Path: kquant_data/utils/xdatetime.py
# def datetime_2_yyyyMMdd(dt):
#     """
#     将时间转换成float类型
#     :param dt:
#     :return:
#     """
#     t = dt.timetuple()
#     return int((t.tm_year * 10000.0 + t.tm_mon * 100 + t.tm_mday))


# Path: kquant_data/wind/wss.py
from WindPy import w
from .utils import asDateTime
from ..utils.xdatetime import datetime_2_yyyy
from ..utils.xdatetime import datetime_2_yyyyMMdd
import os
import pandas as pd

    :param w:
    :param wind_codes:
    :param year:
    :return:
    """
    w.asDateTime = asDateTime
    w_wss_data = w.wss(wind_codes, "fellow_listeddate", "year=%s" % year)
    df = pd.DataFrame(w_wss_data.Data)
    df = df.T
    df.columns = ['fellow_listeddate']
    df.index = w_wss_data.Codes
    return df


def download_ipo_last_trade_trading(w, wind_codes):
    # 黄金从20130625开始将最小变动价位从0.01调整成了0.05，但从万得上查出来还是完全一样，所以没有必要记录mfprice
    # 郑商所在修改合约交易单位时都改了合约代码，所以没有必要记录contractmultiplier
    w.asDateTime = asDateTime
    w_wss_data = w.wss(wind_codes, "sec_name,ipo_date,lasttrade_date,lasttradingdate", "")
    grid = w_wss_data.Data

    # T1803一类的会被当成时间，需要提前转置
    new_grid = [[row[i] for row in grid] for i in range(len(grid[0]))]

    df = pd.DataFrame(new_grid)
    df.columns = ['sec_name', 'ipo_date', 'lasttrade_date', 'lasttradingdate']
    df.index = w_wss_data.Codes
    df.index.name = 'wind_code'

    df['ipo_date'] = df['ipo_date'].apply(datetime_2_yyyyMMdd)

    df['lasttrade_date'] = df['lasttrade_date'].apply(datetime_2_yyyyMMdd)