首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

嵌入式新手如何设定Linux的时间函数(2)

嵌入式新手如何设定Linux的时间函数(2)

4.设置CMOS时间,其实它是通过RTC(Real-timeclock)设备驱动来完成的,你可以用ioctl()函数来设置时间,当然也可以通过操作/dev/rtc设备文件,在此就不详细说明了。
二、windows下设置系统时间
1.设置当前时区的时间
#include
BOOL SetLocalTime(constSYSTEMTIME* lpSystemTime);
typedef struct _SYSTEMTIME{  // st
    WORD wYear;
    WORD wMonth; //月份从1开始
    WORD wDayOfWeek; //SetLocalTime()不使用这个参数
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME;
函数成功返回非零,失败返回零。注意要求调用进程必需有SE_SYSTEMTIME_NAME权限。
2.另外还有一个函数SetSystemTime(),它的参数与SetLocalTime一样,只不过以UTC时区为基准的。
BOOL SetSystemTime(constSYSTEMTIME* lpSystemTime);
二、 一个封装的设置系统时间的函数
//设置成功返回true,否则返回false
       bool set_local_time(struct tm& t)
{
#ifdef_WIN32
       SYSTEMTIME st;
       memset(&st, 0,sizeof(SYSTEMTIME));
       st.wYear = t.tm_year + 1970; //注意structtm结构中的年是从1970年开始的计数
       st.wMonth = t.tm_mon + 1; //注意structtm结构中的月份是从0开始的
       st.wDay = t.tm_mday;
       st.wHour = t.tm_hour;
       st.wMinute = t.tm_min;
       st.wSecond = t.tm_sec;
       if(!SetLocalTime(&st))
              return true;
              else
                     return false;
       #else
              //将structtm结构时间转换成GMT时间time_t
              struct time_t st;
              st = mktime(&t);
              if(st==-1)
                     return false;
              if(!stime(st))
                     return true;
              else
                     return false;
#endif
}
三、linux系统时间函数
1、gettimeofday(取得目前的时间)
#include
#include
intgettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明  gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
structtimeval{
longtv_sec;   
longtv_usec;
};
timezone结构定义为:
structtimezone{
inttz_minuteswest;
inttz_dsttime;
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime所代表的状态如下
DST_NONE
DST_USA
DST_AUST
DST_WET
DST_MET
DST_EET
DST_CAN
DST_GB
DST_RUM
DST_TUR
DST_AUSTALT
返回值  成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
范例  #include
#include
main(){
structtimeval tv;
structtimezone tz;
gettimeofday(&tv , &tz);
printf(“tv_sec;%d\n”, tv,.tv_sec) ;
printf(“tv_usec;%d\n”,tv.tv_usec);
printf(“tz_minuteswest;%d\n”, tz.tz_minuteswest);
printf(“tz_dsttime,%d\n”,tz.tz_dsttime);
}
执行  tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
2、settimeofday(设置目前时间)
#include
#include
intsettimeofday ( const struct timeval *tv,const struct timezone *tz);
函数说明  settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
返回值  成功则返回0,失败返回-1,错误代码存于errno。
错误代码  EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL时区或某个数据是不正确的,无法正确设置时间。
3、clock_gettime(获取指定时钟的时间值)
#include
intclock_gettime( clockid_t clock_id,struct timespec * tp );
说明:clock_id指定要获取时间的时钟,根据Posix的指定可以是以下值:
CLOCK_REALTIME
Systemwiderealtime clock.
CLOCK_MONOTONIC
Representsmonotonic time. Cannot be set.
CLOCK_PROCESS_CPUTIME_ID
Highresolution per-process timer.
CLOCK_THREAD_CPUTIME_ID
Thread-specifictimer.
CLOCK_REALTIME_HR
Highresolution version of CLOCK_REALTIME.
CLOCK_MONOTONIC_HR
Highresolution version of CLOCK_MONOTONIC.
structtimespec {
time_ttv_sec;      
long  tv_nsec;      
};
4、adjtimex(tunekernel clock)
#include
intadjtimex(struct timex *buf);
说明:
Linux  uses  David L. Mills' clock adjustmentalgorithm (see RFC 1305).The system call adjtimex() reads and optionally setsadjustment parame-ters  for  this  algorithm.   It  takes a pointer to a timexstructure,updates kernel parameters from  field  values,  and  returns  the  same structure  with  current kernelvalues.  This structure isdeclared as follows:
structtimex {
intmodes;         
longoffset;        
longfreq;         
longmaxerror;      
longesterror;      
intstatus;         
longconstant;      
longprecision;     
longtolerance;     
structtimeval time;
longtick;         
};
Themodes field determines which parameters, if any, to  set.   It  may contain a bitwise-or combinationof zero or more of the following bits:
#defineADJ_OFFSET            0x0001
#defineADJ_FREQUENCY         0x0002
#defineADJ_MAXERROR          0x0004
#defineADJ_ESTERROR          0x0008
#defineADJ_STATUS            0x0010
#defineADJ_TIMECONST         0x0020
#defineADJ_TICK              0x4000
#defineADJ_OFFSET_SINGLESHOT 0x8001  
Ordinaryusers are restricted to a zero value for mode.  Only the supe-ruser may set anyparameters.
RETURNVALUE
Onsuccess, adjtimex() returns the clock state:
#defineTIME_OK   0
#defineTIME_INS  1
#defineTIME_DEL  2
#defineTIME_OOP  3
#defineTIME_WAIT 4
#defineTIME_BAD  5  
Onfailure, adjtimex() returns -1 and sets errno.
ERRORS
EFAULT
bufdoes not point to writable memory.
EINVAL
Anattempt is made to set buf.offset to a value outside the range -131071 to +131071,or to set buf.status to a value other than those listed above, or to setbuf.tick to a value outside the range 900000/HZ to 1100000/HZ, where HZ is thesystem  timer interruptfrequency.
继承事业,薪火相传
返回列表