time_t通常是指示自纪元以来秒数的整数类型,因此您应该能够添加335(5分35秒).
请记住,ISO C99标准规定:
The range and precision of times representable in clock_t and time_t are implementation-defined.
因此,虽然这通常会起作用(并且在我曾经使用的每个系统上都这样做),但可能会出现一些边缘情况并非如此.
请参阅对您的程序的以下修改,增加五分钟(300秒):
#include
#include
void print_time(time_t tt) {
char buf[80];
struct tm* st = localtime(&tt);
strftime(buf, 80, "%c", st);
printf("%s\n", buf);
}
int main() {
time_t t = time(NULL);
print_time(t);
t += 300;
print_time(t);
return 0;
}
输出是:
Sat Nov 6 10:10:34 2010
Sat Nov 6 10:15:34 2010