linux时间(二 设置系统时间)

/*******************

设置系统时间

********************/

 

#include<time.h>

int stime(long * time)

这个函数可以用于设置系统时间,但是从参数可见,要让保存的时间是一个long类型的指针。

所以需要将年月日的时间换算成从1900年到现在的秒数。下面这个函数做这个工作。

 

time_t mktime(struct tm * time)

将时间结构数据转换成经过的秒数

例如下面这个例子

//重置系统时间
extern int set_write_time_bgstage( int set_time[])
{
   
   if (set_time == NULL)
   {
      printf("error in write_time_bgstage : time == NULL/n");
   return -1;
   }
 
   time_t t;
   struct tm  current_time;
  
   current_time.tm_year  = set_time[0] - 1900;
   current_time.tm_mon   = set_time[1] - 1;
   current_time.tm_mday  = set_time[2];
   current_time.tm_hour  = set_time[3];
   current_time.tm_min   = set_time[4];
   current_time.tm_sec   = 0;
   current_time.tm_isdst = -1;
  
   t = mktime(&current_time);
   stime(&t);
  
   system("hwclock -w");
   return 0;
}

注意:使用stime不能真正的修改系统时间,当重新启动计算机之后,可以观察到修改无效。因为stime没有将

时间写入CMOS.要写入bois需要调用 hwclock -w 程序

ps:cmos 是电脑主板上的一块可读写的RAM芯片。因为可读写的特性,所以在电脑主板上用来保存BIOS设置完电脑硬件参数后的数据,这个芯片仅仅是用来存放数据的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值