# 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
"""
更新数据范围列表，用于下载数据时减少重复下载

日线数据路径是 IF/IF1804.h5
分钟数据路径是 IF1804/IF1804_20180226.h5

已经退市的合约，只要做过一次，后面就没有必要再做了
但数据还是有问题，不活跃的合约，最后几天完全没有行情了
"""


# 解决Python 3.6的pandas不支持中文路径的问题
print(sys.getfilesystemencoding())  # 查看修改前的
try:
    sys._enablelegacywindowsfsencoding()  # 修改
    print(sys.getfilesystemencoding())  # 查看修改后的
except:
    pass

path_ipo_last_trade = os.path.join(__CONFIG_H5_FUT_SECTOR_DIR__, 'ipo_last_trade_trading.csv')


def get_in_file_day(file_path, wind_code, df):
    print(file_path)
    # 需要
    df_h5 = pd.read_hdf(file_path)
    df_h5.dropna(axis=0, how='all', thresh=3, inplace=True)
    if df_h5.empty:
        return df
    first = yyyyMMddHHmm_2_datetime(df_h5['DateTime'].iloc[0])

# 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

"""
更新数据范围列表，用于下载数据时减少重复下载

日线数据路径是 IF/IF1804.h5
分钟数据路径是 IF1804/IF1804_20180226.h5

已经退市的合约，只要做过一次，后面就没有必要再做了
但数据还是有问题，不活跃的合约，最后几天完全没有行情了
"""


# 解决Python 3.6的pandas不支持中文路径的问题
print(sys.getfilesystemencoding())  # 查看修改前的
try:
    sys._enablelegacywindowsfsencoding()  # 修改
    print(sys.getfilesystemencoding())  # 查看修改后的
except:
    pass

path_ipo_last_trade = os.path.join(__CONFIG_H5_FUT_SECTOR_DIR__, 'ipo_last_trade_trading.csv')


def get_in_file_day(file_path, wind_code, df):
    print(file_path)
    # 需要
    df_h5 = pd.read_hdf(file_path)
    df_h5.dropna(axis=0, how='all', thresh=3, inplace=True)
    if df_h5.empty:
        return df
    first = yyyyMMddHHmm_2_datetime(df_h5['DateTime'].iloc[0])

    last = yyyyMMddHHmm_2_datetime(df_h5['DateTime'].iloc[-1])