- 论坛徽章:
- 0
|
#include stdlib.h>
#include stdio.h>
#include unistd.h>
#include string.h>
#include time.h>
#include sys/time.h>
#define err(msg) perror(msg)
int main(void)
{
struct timeval tv, tv2;
struct tm *tm;
time_t newtime;
if (gettimeofday(&tv, NULL) == -1) {
err("gettimeofday");
goto out;
}
if ((tm = localtime(&tv.tv_sec)) == NULL) {
err("localtime");
goto out;
}
printf("year = %d\n", tm->tm_year + 1900);
printf("month = %d\n", tm->tm_mon + 1);
printf("mday = %d\n", tm->tm_mday);
printf("hour = %d\n", tm->tm_hour);
printf("min = %d\n", tm->tm_min);
printf("sec = %d\n", tm->tm_sec);
tm->tm_hour++;
if ((newtime = mktime(tm)) == (time_t)-1) {
err("mktime");
goto out;
}
tv2.tv_sec = newtime;
tv2.tv_usec = 0;
if (settimeofday(&tv2, NULL) == -1) {
err("settimeofday");
goto out;
}
printf("time change success.\n");
return 0;
out:
return -1;
}
[root@localhost tmp]#
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/63604/showart_2114577.html |
|