69,336
社区成员
发帖
与我相关
我的任务
分享
/*-
* Copyright (C), 1988-2012, mymtom
*
* vi:set ts=4 sw=4:
*/
#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */
/**
* @file tm.c
* @brief
*/
#include <stdio.h>
#include <time.h>
#include <string.h>
#define HAVE_STRPTIME 1
int main(int argc, char *argv[])
{
time_t tick;
struct tm tm;
char buf[20];
memset(&tm, 0, sizeof(tm));
#if HAVE_STRPTIME
strptime("2011/11/30", "%Y/%m/%d", &tm);
#else
tm.tm_year = 2011 - 1900;
tm.tm_mon = 11 - 1;
tm.tm_mday = 30;
#endif
tm.tm_mday += 3;
tm.tm_isdst = -1;
tick = mktime(&tm);
tm = *localtime(&tick);
strftime(buf, sizeof(buf), "%Y/%m/%d", &tm);
printf("%s\n", buf);
return 0;
}
tm t1;
time_t t2;
time(&t2);
localtime_s(&t1,&t2);
t.tm_sec+=20;
t2=mktime(t1);
localtime_s(&t1,&t2);
//取20秒后的时间,其他类似
#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;
}