Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1090953
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: LINUX

2009-12-08 22:14:38

#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]#


阅读(822) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~