# 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
                    dirpath_filename = os.path.join(_dirpath, filename)
                    shotname, extension = os.path.splitext(filename)
                    if extension != '.h5':
                        continue
                    try:
                        row = date_table.loc[shotname]
                        # 最后一天如何判断数据已经完成？
                        if row['lasttrade_date'] == row['last']:
                            print(row['last'])
                            continue
                    except:
                        pass
                    df = get_in_file_day(dirpath_filename, shotname, df)

    path = os.path.join(root_path, 'first_last.csv')
    df.index.name = 'product'
    df.to_csv(path)
    return df


def CZCE_3to4(x):
    if x['exchange'] == 'CZC':
        x['InstrumentID'] = '%s%s%s' % (x['InstrumentID'][0:2], str(x['lasttrade_date'])[2], x['InstrumentID'][-3:])

    return x


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


# 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

                    dirpath_filename = os.path.join(_dirpath, filename)
                    shotname, extension = os.path.splitext(filename)
                    if extension != '.h5':
                        continue
                    try:
                        row = date_table.loc[shotname]
                        # 最后一天如何判断数据已经完成？
                        if row['lasttrade_date'] == row['last']:
                            print(row['last'])
                            continue
                    except:
                        pass
                    df = get_in_file_day(dirpath_filename, shotname, df)

    path = os.path.join(root_path, 'first_last.csv')
    df.index.name = 'product'
    df.to_csv(path)
    return df


def CZCE_3to4(x):
    if x['exchange'] == 'CZC':
        x['InstrumentID'] = '%s%s%s' % (x['InstrumentID'][0:2], str(x['lasttrade_date'])[2], x['InstrumentID'][-3:])

    return x


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


    df = wind_code_2_InstrumentID(df_csv, 'wind_code')