随记一个C的时间加减

时间:2023-03-08 15:55:57
随记一个C的时间加减

//Centos6 x86_64

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
time_t tmp_time;
struct tm *ptime;
struct tm *tmp;
tmp_time = time(NULL);

if(argv[1]==0)
{
printf("Input Value Null!\n");
printf("Example:\n");
printf("zz -10 // before 10 minutes\n");
return -1;
}
else
printf("%d\n",atoi(argv[1]));

//ptime = localtime(&tmp_time);

//printf("%d-%d-%d %d:%d:%d\n",(1900+ptime->tm_year),(1+ptime->tm_mon),ptime->tm_mday,ptime->tm_hour,ptime->tm_min,ptime->tm_sec)
;

tmp_time += atoi(argv[1])*60;
tmp = localtime(&tmp_time);
printf("%04d-%02d-%02d %02d:%02d:%02d\n",(1900+tmp->tm_year),(1+tmp->tm_mon),tmp->tm_mday,tmp->tm_hour,tmp->tm_min,tmp->tm_sec);

}