# Path: kquant_data/utils/xdatetime.py
# def yyyyMMddHHmm_2_datetime(dt):
#     """
#     输入一个长整型yyyyMMddhmm，返回对应的时间
#     :param dt:
#     :return:
#     """
#     dt = int(dt)  # FIXME：在python2下会有问题吗？
#     (yyyyMMdd, hh) = divmod(dt, 10000)
#     (yyyy, MMdd) = divmod(yyyyMMdd, 10000)
#     (MM, dd) = divmod(MMdd, 100)
#     (hh, mm) = divmod(hh, 100)
# 
#     return datetime(yyyy, MM, dd, hh, mm)
# 
# def yyyyMMdd_2_datetime(dt):
#     yyyyMMdd = int(dt)
#     (yyyy, MMdd) = divmod(yyyyMMdd, 10000)
#     (MM, dd) = divmod(MMdd, 100)
#     if yyyy == 0:
#         return None
# 
#     return datetime(yyyy, MM, dd, 0, 0)
# 
# Path: kquant_data/config.py
# __CONFIG_H5_FUT_SECTOR_DIR__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'sectorconstituent')
# 
# Path: kquant_data/future/symbol.py
# def wind_code_2_InstrumentID(df, field):
#     sym_ex = df[field].str.split('.')
#     sym_ex = list(sym_ex)
#     sym_ex_df = pd.DataFrame(sym_ex, index=df.index)
#     sym_ex_df.columns = ['InstrumentID', 'exchange']
#     df = pd.concat([df, sym_ex_df], axis=1)
#     df['lower'] = (df['exchange'] == 'SHF') | (df['exchange'] == 'DCE') | (df['exchange'] == 'INE')
#     df['InstrumentID'][df['lower']] = df['InstrumentID'].str.lower()
#     return df
# 
# Path: kquant_data/xio/csv.py
# def read_datetime_dataframe(path, sep=','):
#     """
#     读取季报的公告日，即里面全是日期
#     注意：有些股票多个季度一起发，一般是公司出问题了，特别是600878，四个报告同一天发布
#     年报与一季报很有可能一起发
# 
#     读取ipo_date
#     :param path:
#     :param sep:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, index_col=0, parse_dates=True, encoding='utf-8-sig', sep=sep)
#     except (FileNotFoundError, OSError):
#         return None
# 
#     df = df.stack(dropna=False)
#     df = pd.to_datetime(df)
#     df = df.unstack()
# 
#     return df
# 
# Path: kquant_data/utils/xdatetime.py
# def tic():
#     """
#     对应MATLAB中的tic
#     :return:
#     """
#     globals()['tt'] = time.clock()
# 
# def toc():
#     """
#     对应MATLAB中的toc
#     :return:
#     """
#     t = time.clock() - globals()['tt']
#     print('\nElapsed time: %.8f seconds\n' % t)
#     return t


import os
import sys
import pandas as pd
from datetime import datetime
from kquant_data.utils.xdatetime import yyyyMMddHHmm_2_datetime, yyyyMMdd_2_datetime
from kquant_data.config import __CONFIG_H5_FUT_SECTOR_DIR__
from kquant_data.future.symbol import wind_code_2_InstrumentID
from kquant_data.xio.csv import read_datetime_dataframe
from kquant_data.utils.xdatetime import tic, toc
    df_csv = pd.read_csv(path_ipo_last_trade, encoding='utf-8-sig')

    df = wind_code_2_InstrumentID(df_csv, 'wind_code')
    df = df.apply(CZCE_3to4, axis=1)
    df = df.set_index(['InstrumentID'])
    return df


if __name__ == '__main__':
    ipo_last_trade = load_ipo_last_trade_trading()
    ipo_last_trade['ipo_date'] = ipo_last_trade['ipo_date'].apply(lambda x: yyyyMMdd_2_datetime(x))
    ipo_last_trade['lasttrade_date'] = ipo_last_trade['lasttrade_date'].apply(lambda x: yyyyMMdd_2_datetime(x))
    ipo_last_trade['ipo_date'] = pd.to_datetime(ipo_last_trade['ipo_date'])
    ipo_last_trade['lasttrade_date'] = pd.to_datetime(ipo_last_trade['lasttrade_date'])

    root_path = r'D:\DATA_FUT_HDF5\Data_Wind\60'
    csv_path = os.path.join(root_path, 'first_last.csv')
    first_last = read_datetime_dataframe(csv_path)
    if first_last is None:
        first_last = pd.DataFrame(columns=['first', 'last'])

    date_table = ipo_last_trade.merge(first_last, how='inner', left_index=True, right_index=True)

    tic()
    df = get_first_last_min(root_path, date_table, first_last)
    toc()

    # 如何实现
    root_path = r'D:\DATA_FUT_HDF5\Data_Wind\86400_Wind'
    csv_path = os.path.join(root_path, 'first_last.csv')

# Path: kquant_data/utils/xdatetime.py
# def yyyyMMddHHmm_2_datetime(dt):
#     """
#     输入一个长整型yyyyMMddhmm，返回对应的时间
#     :param dt:
#     :return:
#     """
#     dt = int(dt)  # FIXME：在python2下会有问题吗？
#     (yyyyMMdd, hh) = divmod(dt, 10000)
#     (yyyy, MMdd) = divmod(yyyyMMdd, 10000)
#     (MM, dd) = divmod(MMdd, 100)
#     (hh, mm) = divmod(hh, 100)
# 
#     return datetime(yyyy, MM, dd, hh, mm)
# 
# def yyyyMMdd_2_datetime(dt):
#     yyyyMMdd = int(dt)
#     (yyyy, MMdd) = divmod(yyyyMMdd, 10000)
#     (MM, dd) = divmod(MMdd, 100)
#     if yyyy == 0:
#         return None
# 
#     return datetime(yyyy, MM, dd, 0, 0)
# 
# Path: kquant_data/config.py
# __CONFIG_H5_FUT_SECTOR_DIR__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'sectorconstituent')
# 
# Path: kquant_data/future/symbol.py
# def wind_code_2_InstrumentID(df, field):
#     sym_ex = df[field].str.split('.')
#     sym_ex = list(sym_ex)
#     sym_ex_df = pd.DataFrame(sym_ex, index=df.index)
#     sym_ex_df.columns = ['InstrumentID', 'exchange']
#     df = pd.concat([df, sym_ex_df], axis=1)
#     df['lower'] = (df['exchange'] == 'SHF') | (df['exchange'] == 'DCE') | (df['exchange'] == 'INE')
#     df['InstrumentID'][df['lower']] = df['InstrumentID'].str.lower()
#     return df
# 
# Path: kquant_data/xio/csv.py
# def read_datetime_dataframe(path, sep=','):
#     """
#     读取季报的公告日，即里面全是日期
#     注意：有些股票多个季度一起发，一般是公司出问题了，特别是600878，四个报告同一天发布
#     年报与一季报很有可能一起发
# 
#     读取ipo_date
#     :param path:
#     :param sep:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, index_col=0, parse_dates=True, encoding='utf-8-sig', sep=sep)
#     except (FileNotFoundError, OSError):
#         return None
# 
#     df = df.stack(dropna=False)
#     df = pd.to_datetime(df)
#     df = df.unstack()
# 
#     return df
# 
# Path: kquant_data/utils/xdatetime.py
# def tic():
#     """
#     对应MATLAB中的tic
#     :return:
#     """
#     globals()['tt'] = time.clock()
# 
# def toc():
#     """
#     对应MATLAB中的toc
#     :return:
#     """
#     t = time.clock() - globals()['tt']
#     print('\nElapsed time: %.8f seconds\n' % t)
#     return t


# Path: demo_future/E00_update_first_last_h5.py
import os
import sys
import pandas as pd
from datetime import datetime
from kquant_data.utils.xdatetime import yyyyMMddHHmm_2_datetime, yyyyMMdd_2_datetime
from kquant_data.config import __CONFIG_H5_FUT_SECTOR_DIR__
from kquant_data.future.symbol import wind_code_2_InstrumentID
from kquant_data.xio.csv import read_datetime_dataframe
from kquant_data.utils.xdatetime import tic, toc

    df_csv = pd.read_csv(path_ipo_last_trade, encoding='utf-8-sig')

    df = wind_code_2_InstrumentID(df_csv, 'wind_code')
    df = df.apply(CZCE_3to4, axis=1)
    df = df.set_index(['InstrumentID'])
    return df


if __name__ == '__main__':
    ipo_last_trade = load_ipo_last_trade_trading()
    ipo_last_trade['ipo_date'] = ipo_last_trade['ipo_date'].apply(lambda x: yyyyMMdd_2_datetime(x))
    ipo_last_trade['lasttrade_date'] = ipo_last_trade['lasttrade_date'].apply(lambda x: yyyyMMdd_2_datetime(x))
    ipo_last_trade['ipo_date'] = pd.to_datetime(ipo_last_trade['ipo_date'])
    ipo_last_trade['lasttrade_date'] = pd.to_datetime(ipo_last_trade['lasttrade_date'])

    root_path = r'D:\DATA_FUT_HDF5\Data_Wind\60'
    csv_path = os.path.join(root_path, 'first_last.csv')
    first_last = read_datetime_dataframe(csv_path)
    if first_last is None:
        first_last = pd.DataFrame(columns=['first', 'last'])

    date_table = ipo_last_trade.merge(first_last, how='inner', left_index=True, right_index=True)

    tic()
    df = get_first_last_min(root_path, date_table, first_last)
    toc()

    # 如何实现
    root_path = r'D:\DATA_FUT_HDF5\Data_Wind\86400_Wind'
    csv_path = os.path.join(root_path, 'first_last.csv')

    first_last = read_datetime_dataframe(csv_path)