Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7569851
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2012-11-01 17:14:35

 

/**

 * 函数功能:获取系统的时间

 * char  *cDateTime:14字节时间串,YYYYMMDDHHMMSS,20120227161359

 */

void ST_GetDateTime(char *cDateTime)

{

       int ret;

       time_t t;

 

       ret = time(&t); // 返回从公元197011000秒到现在的秒数

       if(ret < 0)

       {

              perror("ST_GetDateTime");

              return;

       }

 

       struct tm *tm = localtime(&t); //取得当地目前时间和日期,时间日期已经转换成当地时区

//     char *format = " %Y/%m/%d %H:%M:%S";

       char *format = " %Y%m%d%H%M%S";

       strftime(cDateTime, 21, format, tm); // 函数按照参数fmt所设定格式将time类型的参数格式化为日期时间信息然后存储在字符串str

}

 

/**

 * 函数功能:设置系统的时间

 * char  *cDateTime:YYYYMMDDHHMMSS,20120227161359

 * 返回值: 0x00-->设置成功 : 0x8B-->参数错误 : 0x01-->设置失败

 */

int ST_SetDateTime(char *cDateTime)

{

       char cmd[256];

 

       struct

       {

              char year[5];

              char month[3];

              char day[3];

              char hour[3];

              char min[3];

              char sec[3];

       } date;

 

       memset(&date, 0, sizeof(date));

       memset(&cmd, 0, sizeof(cmd));

 

       memcpy(&date.year, cDateTime, 4);

       memcpy(&date.month, cDateTime + 4, 2);

       memcpy(&date.day, cDateTime + 6, 2);

       memcpy(&date.hour, cDateTime + 8, 2);

       memcpy(&date.min, cDateTime + 10, 2);

       memcpy(&date.sec, cDateTime + 12, 2);

       sprintf(cmd, "date -s %s.%s.%s-%s:%s:%s", date.year, date.month, date.day, date.hour, date.min, date.sec);

        printf("%s\n",cmd);

       if(system(cmd) < 0)            // 时间设置

       {

              perror("ST_SetDateTime");

              return 1;

       }

       if(system("hwclock -w") < 0)      // 时间设置生效

       {

              perror("ST_SetDateTime");

              return 1;

       }

 

       return 0;

}

 

 

阅读(2919) | 评论(0) | 转发(2) |
0

上一篇:Linux_C 串口基本操作

下一篇:Linux_C计时器

给主人留下些什么吧!~~