VC++(Ctime日期函数)应用

11 篇文章 0 订阅
// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <string.h>
#include <iostream>
#include <io.h>
#include<atltime.h>

using namespace std;

#define BUFSIZE 1024
#define PLANFILE "planTast.ini";
#define MANUALFILE "manualRun.ini";

CTime   String2Time(CString s)   
{   
int   iY,iMon,iD,iH,iM,iSS;   
int   len;   
len=s.GetLength();   
if(len==8)   
{   
   CTime   t1;   
   t1=CTime::GetCurrentTime();   
   iY=t1.GetYear();   
   iMon=t1.GetMonth();   
   iD=t1.GetDay();   
   CString   h,m,ss;   
   h=s.Mid(0,2);   
   iH=atoi(h);   
   m=s.Mid(3,2);   
   iM=atoi(m);   
   ss=s.Mid(6,2);   
   iSS=atoi(ss);   
}   
else if(len==19)   
{   
   CString   y,mon,d,h,m,ss;   
   y=s.Mid(0,4);   
   iY=atoi(y);   
   mon=s.Mid(5,2);   
   iMon=atoi(mon);   
   d=s.Mid(8,2);   
   iD=atoi(d);   
   h=s.Mid(11,2);   
   iH=atoi(h);   
   m=s.Mid(14,2);   
   iM=atoi(m);   
   ss=s.Mid(17,2);   
   iSS=atoi(ss);   
}   
else   
{   
   CTime   t2;   
   t2=CTime::GetCurrentTime();   
   iY=t2.GetYear();   
   iMon=t2.GetMonth();   
   iD=t2.GetDay();   
   iH=t2.GetHour();   
   iM=t2.GetMinute();   
   iSS=t2.GetSecond();   
}   
CTime t(iY,iMon,iD,iH,iM,iSS);   
return t;   
}  

int main()
{
/*
FILE* fd;
char buffer[BUFSIZE];
if(_access("1.txt",00) == -1) return -1;

if (( fd = fopen("1.txt","rt")) == NULL) return -1;

while(fgets(buffer,BUFSIZE,fd))
{
   cout <<buffer <<endl;
}
fclose(fd);
unlink("1.txt");
*/
//CString strTime("2007-05-23");
CString strTime("2007-05-23 02:23:12");
CTime cTime = String2Time(strTime);
CTime time = CTime::GetCurrentTime();   //获取当前时间   
CTime time1(time.GetYear(),time.GetMonth()-3,time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());   
cout <<time.GetYear()<<time.GetDay() << time.GetHour() <<endl;

cout <<cTime.GetMonth()<<cTime.GetDay()<<endl;
CTimeSpan tmr3 = cTime - time;
LONGLONG longsum = tmr3.GetTotalSeconds();
if (longsum < 10 && longsum>-10)
{
   cout << "OK" << endl;
}
return 0;
}   

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值