首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求C++中的时间函数,该如何解决

2012-04-28 
求C++中的时间函数比如我给的是2009/01/01/12/52/51,给定的是年月日时分秒,我要是想加8个时间,在C++怎么存

求C++中的时间函数
比如我给的是2009/01/01/12/52/51,给定的是年月日时分秒,我要是想加8个时间,在C++怎么存储,运算最方便,我想过用struct tm结构体,但是要把2009/01/01/12/52/51赋值给一个结构体不好吧,结构体里面的信息太多了,有的设置不了。有没有方便的方法

[解决办法]

C/C++ code
typedef struct tagTime{int year;short month;short day;short hour;short minute;short second;}Time;Time time;char buffer[] = "20090101125251";memcpy( &time , buffer , strlen(buffer) );//直接写的,可能有错误
[解决办法]
COleDateTime
COleDateTimeSpan
C/C++ code
#include <afxdisp.h>#include <stdio.h>#include <stdlib.h>int main(int argc, TCHAR* argv[]) {    COleDateTime t;    COleDateTimeSpan ts;    CString s,fmt;    int nYear;    int nMonth;    int nDay;    int nHour;    int nMin;    int nSec;    int lDays;    int nHours;    int nMins;    int nSecs;    int i,N;    // initialize MFC and print and error on failure    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {        printf("Fatal Error: MFC initialization failed\n");        return 1;    }    if (argc<13) {        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);        return 2;    }    if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";    else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";    else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";    else {        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);        return 3;    }    nYear =atoi(argv[ 1]);    nMonth=atoi(argv[ 2]);    nDay  =atoi(argv[ 3]);    nHour =atoi(argv[ 4]);    nMin  =atoi(argv[ 5]);    nSec  =atoi(argv[ 6]);    lDays =atoi(argv[ 7]);    nHours=atoi(argv[ 8]);    nMins =atoi(argv[ 9]);    nSecs =atoi(argv[10]);    N     =atoi(argv[11]);    if (N<=0) {        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);        return 4;    }    t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);    ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );    for (i=1;i<=N;i++) {        s=t.Format(fmt);        printf("%08d %s\n",i,s);        t=t+ts;    }    return 0;} 

热点排行