#include "stdafx.h" /// 判断文件格式 const unsigned int GetFileFormat(TCHAR* sFileName) { unsigned int iRet = E_FileERR; /// 0、判断文件名是否有效 //if (sFileName.IsEmpty() || 4 > sFileName.GetLength()) // return iRet; int iLenth = WideCharToMultiByte(CP_ACP, 0, sFileName, -1, NULL, 0, NULL, NULL); if (iLenth == 0 || 4 > iLenth) return iRet; string strFileName = C22<255>(sFileName); size_t iLen = strFileName.length(); int iPos = strFileName.find_last_of(‘.‘); if (iPos >= 0 && iPos < iLen) { string strSuff(strFileName.substr(iPos, iLen - iPos)); /// 转变为小写,方便比较扩展名 transform(strSuff.begin(), strSuff.end(), strSuff.begin(), tolower); if (!(strSuff.compare(".csv"))) { iRet = E_FileCSV; } else if (!(strSuff.compare(".txt"))) { iRet = E_FileTXT; } else if (!(strSuff.compare(".xls"))) { iRet = E_FileXLS; } else if (!(strSuff.compare(".xlsx"))) { iRet = E_FileXLSX; } else if (!(strSuff.compare(".dbf"))) { iRet = E_FileDBF; } } return iRet; } const SYSTEMTIME Time_tToSystemTime(time_t t) { tm temptm; localtime_s(&temptm, &t); SYSTEMTIME st = { 1900 + temptm.tm_year, 1 + temptm.tm_mon, temptm.tm_wday, temptm.tm_mday, temptm.tm_hour, temptm.tm_min, temptm.tm_sec, 0 }; return st; } const time_t SystemTimeToTime_t(const SYSTEMTIME& st) { struct tm gm = { st.wSecond, st.wMinute, st.wHour, st.wDay, st.wMonth - 1, st.wYear - 1900, st.wDayOfWeek, 0, 0 }; return mktime(&gm); } const SYSTEMTIME GetTimeFromString(std::string szTime, std::string szGap, bool bOneDayTime) { SYSTEMTIME targetTime = { 0 }; int gapSize = szTime.find(szGap); int pos0 = szTime.find_first_of(szGap); int pos1 = szTime.find_last_of(szGap); if (pos0 < 0 || pos1 < 0 || pos0 == pos1) { return targetTime; } string szHead(szTime.c_str(), pos0); string szMiddle(szTime.c_str() + pos0 + 1, pos1 - pos0 - 1); string szTail(szTime.c_str() + pos1 + 1, szTime.length() - pos1 - 1); if (bOneDayTime) //HH:mm:ss { targetTime = { 0, 0, 0, 0, atoi(szHead.c_str()), atoi(szMiddle.c_str()), atoi(szTail.c_str()), 0 }; } else //YYYY:MM:DD { targetTime = { atoi(szHead.c_str()), atoi(szMiddle.c_str()), 0, atoi(szTail.c_str()), 0, 0, 0, 0 }; } return targetTime; } //获取指定月份天数(//改为获取当月最后一个工作日) const unsigned int GetDaysInMonth(const unsigned int year, const unsigned int month) { unsigned int tmpDay; const int day[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month == 2) { tmpDay = ((0 == year % 4) && (0 != year % 100) || (0 == year % 400)) ? 29 : 28; } else if (month > 12) { tmpDay = 30; } else { tmpDay = day[month - 1]; } return tmpDay; } //获取当前时间点当月最后一个工作日 const SYSTEMTIME GetCurMonthLastWorkDay(SYSTEMTIME curTime) { unsigned int lastDay = GetDaysInMonth(curTime.wYear, curTime.wMonth); time_t lastDaySecond = SystemTimeToTime_t(curTime) + (lastDay - curTime.wDay) * (24 * 3600); SYSTEMTIME lastDayTime = Time_tToSystemTime(lastDaySecond); return GetLastWorkDay(lastDayTime); } //获取当前时间点最后一个工作日的日期 const SYSTEMTIME GetLastWorkDay(const SYSTEMTIME& st) { int endSeconds = 0; if (0 == st.wDayOfWeek % 6) { time_t selTime_t = SystemTimeToTime_t(st); //当前时间点为周末 if (st.wDayOfWeek == 6) { endSeconds = (24 * 3600) * 1; } else { endSeconds = (24 * 3600) * 2; } return Time_tToSystemTime(selTime_t - endSeconds); } else { //当前时间点为工作日 return st; } } //获取当前时间点包含N个工作日的起始日期(默认是5个工作日) const SYSTEMTIME GetFirstWorkDay(const SYSTEMTIME& st, const unsigned int nDaysBefor) { int startSecond = 0; int endSeconds = 0; time_t selTime_t = SystemTimeToTime_t(st); //获取日期前推 if (st.wDayOfWeek % 6 == 0)//当日为周末 { if (st.wDayOfWeek == 6) { endSeconds = (24 * 3600) * 1; } else { endSeconds = (24 * 3600) * 2; } int nAddDay = nDaysBefor % 5 == 0 ? -1 : 0; startSecond = (24 * 3600) * (nDaysBefor - 1) + (48 * 3600) * (nDaysBefor / 5 + nAddDay) + endSeconds; } else //当日即为工作日 { if (st.wDayOfWeek > nDaysBefor) { startSecond = (24 * 3600) * (nDaysBefor - 1); } else { int nAddDay = (nDaysBefor - st.wDayOfWeek) % 5 == 0 ? 0 : 1; startSecond = (24 * 3600) * (nDaysBefor - 1) + (48 * 3600) * ((nDaysBefor - st.wDayOfWeek) / 5 + nAddDay); } } return Time_tToSystemTime(selTime_t - startSecond); //第一个工作日 } static int IsLeapYear(int year) { if ((year % 4 == 0) && (year % 100 != 0) || year % 400 == 0) return 366; else return 365; } const SYSTEMTIME GetFirstWorkDayOfOneYear(const WORD nYear) { int nTotalDays = 0; for (int i = 1; i < nYear; i++) nTotalDays += IsLeapYear(i); int nDayOfWeek = (nTotalDays + 1) % 7; int nStartDay = 1; int nSetDayOfWeek = nDayOfWeek; if (nDayOfWeek == 6) { nSetDayOfWeek = 1; nStartDay += 2; } else if (nDayOfWeek == 0) { nSetDayOfWeek = 1; nStartDay += 1; } SYSTEMTIME firstWorkDay = { nYear, 1, nSetDayOfWeek, nStartDay, 0, 0, 0, 0 }; return firstWorkDay; } const string GetFutureCode(SYSTEMTIME sysTm, E_FUTURECODE eType, E_FUTURETIME eMonth) { string szStr; if (eType == TYPE_IFCODE) { szStr = "IF"; } else if (eType == TYPE_IHCODE) { szStr = "IH"; } else { szStr = "IC"; } char szTime[5]; memset(szTime, 0, sizeof(szTime)); //CURMONTH int year = sysTm.wYear % 100; int month = sysTm.wMonth; bool bNext = false; //判断当月第1天是一周的第几天 int gapDay; time_t selTime = SystemTimeToTime_t(sysTm); SYSTEMTIME time_fir = Time_tToSystemTime(selTime - (sysTm.wDay - 1) * 24 * 3600); //如果第1天不是第一周日期 if (time_fir.wDayOfWeek % 6 == 0) //周末 { gapDay = 1 + (5 + time_fir.wDayOfWeek / 6) + 14; } else //第一天为周五 { gapDay = 1 + (5 - time_fir.wDayOfWeek) + 14; } if (sysTm.wDay > gapDay) { if (month == 12) { month = 1; year += 1; } else { month += 1; } } if (eMonth == TIME_NEXTMONTH || bNext) { if (month == 12) { month = 1; year += 1; } else { month += 1; } } else if (eMonth == TIME_ENDOFSEASON) { month += 3 - month % 3; if (month > 12) { year += 1; month %= 12; } } else if (eMonth == TIME_NEXTENDOFSEASON) { if (month % 3 == 0) { month += 6; } if (month > 12) { year += 1; month %= 12; } } sprintf_s(szTime, "%02d%02d", year, month); szStr.append(szTime); return szStr; }
原文地址:https://www.cnblogs.com/depend-wind/p/9638546.html
时间: 2024-10-10 18:49:33