2014年3月25日09:53:30
判断文件夹名是否是合法YYYYMM格式
1、使用VS2008创建一个带预编译头的控制台项目。
2、源文件内容:
#include "stdafx.h"
#define LWW_CONSOLE
#ifdef LWW_CONSOLE
#define CosPrintf printf
#else
#define CosPrintf //
#endif
#include <windows.h>
#include <time.h>
#include<iostream>
using namespace std;
bool isValidYYYYMM(TCHAR * tcs,int nBeginYYYYMM,int nNowYYYYMM)
{
int nSize = _tcsclen(tcs);
if(nSize != 6)
return false;
int nYYYYMM = _tstoi(tcs);
if((nYYYYMM>=nBeginYYYYMM)&&(nYYYYMM<=nNowYYYYMM))
{
return true;
}
else
{
return false;
}
}
void getYYYYMM_months(tm * pNowTm,int nSaveMonth,int * pBeginYYYYMM,int *pNowYYYYMM)
{
int nSaveYear = nSaveMonth/12;
int nBeginYear = (pNowTm->tm_year+1900) - nSaveYear;
int nSaveMonth12 = nSaveMonth%12;
int nBeginMonth = 0;
int nNowMonth = pNowTm->tm_mon+1;
if(nSaveMonth12 <= nNowMonth)
{
nBeginMonth = nNowMonth - nSaveMonth12 + 1;
}
else
{
nBeginYear--;
nSaveMonth12 -= nNowMonth;
nBeginMonth = 12 - nSaveMonth12 + 1;
}
*pBeginYYYYMM = nBeginYear * 100 + nBeginMonth;
*pNowYYYYMM = (pNowTm->tm_year+1900) * 100 + nNowMonth;
return;
}
bool getLocalTime(tm * pNowTm)
{
tm * pTm;
time_t nowTime;
nowTime = time(NULL);
pTm = localtime(&nowTime);
memcpy(pNowTm,pTm,sizeof(tm));
return true;
}
int main( void )
{
char ch;
int nSaveDay = 60;
int nSaveMonth = nSaveDay/30;
if(nSaveMonth<=0)
nSaveMonth = 1;
tm aNowTm;
getLocalTime(&aNowTm);
int nBeginYYYYMM = 0;
int nNowYYYYMM = 0;
getYYYYMM_months(&aNowTm,nSaveMonth,&nBeginYYYYMM,&nNowYYYYMM);
TCHAR tcsFileName[MAX_PATH] = {0};
_tcscpy(tcsFileName,TEXT("201403"));
if(isValidYYYYMM(tcsFileName,nBeginYYYYMM,nNowYYYYMM))
{
CosPrintf("isValidYYYYMM\n");
}
printf("程序结束\n");
cin>>ch;
return 0;
}