国产毛片a精品毛-国产毛片黄片-国产毛片久久国产-国产毛片久久精品-青娱乐极品在线-青娱乐精品

嵌入式新手如何設(shè)定Linux的時間函數(shù)

發(fā)布時間:2014-6-5 16:47    發(fā)布者:edu118gct
關(guān)鍵詞: Linux , 時間
一、時間相關(guān)說明
格林威治時間表示0時區(qū)的標(biāo)準(zhǔn)時間。其他時區(qū)的時間和此標(biāo)準(zhǔn)時間均有時間差。UTC(UniversalTime Coordinated)是世界協(xié)調(diào)時間,是格林威治時間在互聯(lián)網(wǎng)中的表示方法
二、標(biāo)準(zhǔn)C語言時間函數(shù)
1、time(取得本地目前的時間秒數(shù))
#include
time_ttime(time_t *t);
函數(shù)說明  此函數(shù)會返回從公元1970年1月1日的UTC時間從0時0分0秒(Epoch,linux紀(jì)元)算起到現(xiàn)在所經(jīng)過的秒數(shù)。如果t并非空指針的話,此函數(shù)也會將返回值存到t指針?biāo)傅膬?nèi)存。
返回值  成功則返回秒數(shù),失敗則返回((time_t)-1)值,錯誤原因存于errno中。
time_t定義為longint
范例  #include
mian()
{
longint seconds= time((time_t*)NULL);
printf(“%d\n”,seconds);
}
執(zhí)行  9.73E+08
2、gmtime(根據(jù)本地時間取得目前的UTC時間)
#include
structtm*gmtime(const time_t*timep);
函數(shù)說明  gmtime()將參數(shù)timep 所指的time_t 結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界所使用的時間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm返回。
結(jié)構(gòu)tm的定義為
structtm
{
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
};
inttm_sec 代表目前秒數(shù),正常范圍為0-59,但允許至61秒
inttm_min 代表目前分?jǐn)?shù),范圍0-59
inttm_hour 從午夜算起的時數(shù),范圍為0-23
inttm_mday 目前月份的日數(shù),范圍01-31
inttm_mon 代表目前月份,從一月算起,范圍從0-11
inttm_year 從1900年算起至今的年數(shù)
inttm_wday 一星期的日數(shù),從星期一算起,范圍為0-6
inttm_yday 從今年1月1日算起至今的天數(shù),范圍為0-365
inttm_isdst 日光節(jié)約時間的旗標(biāo)
此函數(shù)返回的時間日期未經(jīng)時區(qū)轉(zhuǎn)換,而是UTC時間。
返回值  返回結(jié)構(gòu)tm代表目前UTC 時間
范例  #include
main(){
char*wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_ttimep;
structtm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d\n”,wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
執(zhí)行  2000/10/28 Sat 8:15:38
3、localtime(取得當(dāng)?shù)啬壳癠TC時間和日期)
#include
structtm *localtime(const time_t * timep);
函數(shù)說明  localtime()將參數(shù)timep所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界所使用的時間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm返回。結(jié)構(gòu)tm的定義請參考gmtime()。此函數(shù)返回的時間日期已經(jīng)轉(zhuǎn)換成當(dāng)?shù)貢r區(qū)。
返回值  返回結(jié)構(gòu)tm代表目前的當(dāng)?shù)貢r間。
范例  #include
main(){
char*wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_ttimep;
structtm *p;
time(&timep);
p=localtime(&timep);
printf(“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf(“%s%d:%d:%d\n”,wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
執(zhí)行  2000/10/28 Sat 11:12:22
4、ctime(將時間和日期以字符串格式表示)
#include
char*ctime(const time_t *timep);
函數(shù)說明  ctime()將參數(shù)timep所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界所使用的時間日期表示方法,然后將結(jié)果以字符串形態(tài)返回。此函數(shù)已經(jīng)由時區(qū)轉(zhuǎn)換成當(dāng)?shù)貢r間,字符串格式為“WedJun 30 21 :49 :08 1993\n”。若再調(diào)用相關(guān)的時間日期函數(shù),此字符串可能會被破壞。
返回值  返回一字符串表示目前當(dāng)?shù)氐臅r間日期。
范例  #include
main()
{
time_ttimep;
time(&timep);
printf(“%s”,ctime(&timep));
}
執(zhí)行  Sat Oct 28 10 : 12 : 05 2000
5、asctime(將時間和日期以字符串格式表示)
#include
char* asctime(const struct tm * timeptr);
函數(shù)說明  asctime()將參數(shù)timeptr所指的tm結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界所使用的時間日期表示方法,然后將結(jié)果以字符串形態(tài)返回。此函數(shù)已經(jīng)由時區(qū)轉(zhuǎn)換成當(dāng)?shù)貢r間,字符串格式為:“WedJun 30 21:49:08 1993\n”
返回值  若再調(diào)用相關(guān)的時間日期函數(shù),此字符串可能會被破壞。此函數(shù)與ctime不同處在于傳入的參數(shù)是不同的結(jié)構(gòu)。
附加說明  返回一字符串表示目前當(dāng)?shù)氐臅r間日期。
范例  #include
main()
{
time_ttimep;
time(&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
執(zhí)行  Sat Oct 28 02:10:06 2000
6、mktime(將時間結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成經(jīng)過的秒數(shù))
#include
time_tmktime(strcut tm * timeptr);
函數(shù)說明  mktime()用來將參數(shù)timeptr所指的tm結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經(jīng)過的秒數(shù)。
返回值  返回經(jīng)過的秒數(shù)。
范例
#include
main()
{
time_ttimep;
strcuttm *p;
time(&timep);
printf(“time(): %d \n”,timep);
p=localtime(&timep);
timep= mktime(p);
printf(“time()->localtime()->mktime():%d\n”,timep);
}
執(zhí)行  time():974943297
time()->localtime()->mktime():974943297
設(shè)置系統(tǒng)時間
標(biāo)準(zhǔn)C庫中只有獲取系統(tǒng)時間的API,好像還沒有設(shè)置系統(tǒng)時間的API,本文將談?wù)勅绾卧趌inux和windows平臺設(shè)置系統(tǒng)時間,最后給出一個與平臺無關(guān)的設(shè)置系統(tǒng)時間的封閉函數(shù)。
Linux下設(shè)置系統(tǒng)時間:
1.Linux下設(shè)置系統(tǒng)時間的函數(shù)有好幾個,先來看看最常用的stime()函數(shù),這個函數(shù)只能精確到秒。
#define _SVID_SOURCE
#include
int stime(time_t *t);
參數(shù)說明:
t是以秒為單位的時間值,從GMT1970年1月1日0時0分0秒開始計算。
返回值:
成功返回0,錯誤返回-1,errno錯誤碼,EFAULT表示傳遞的參數(shù)錯誤,如時間值是無效的值,EPERM表示權(quán)限不夠,注意只有root用戶才有修改系統(tǒng)時間的權(quán)限。如果要讓普通程序修改系統(tǒng)時間,可以先切換到root用戶操作,修改完成后,再切換到普通用戶,或者用命令chmod+s給執(zhí)行文件加上root用戶的權(quán)限。
2.linux是如何管理時間的?
在系統(tǒng)啟動時,Linux操作系統(tǒng)將時間從CMOS中讀到系統(tǒng)時間變量中,以后修改時間通過修改系統(tǒng)時間實現(xiàn)。為了保持系統(tǒng)時間與CMOS時間的一致性,Linux每隔11分鐘會將系統(tǒng)時間寫入CMOS,同步時間。從這可以看出,獲取系統(tǒng)時間有兩個途徑,一種是從CMOS中讀,一種是從系統(tǒng)中讀,但修改時間卻只有一種,即修改linux系統(tǒng)中的時間,而修改CMOS中的時間是無效的,因為CMOS中的時間會被定時重寫掉。另外還有一點要注意,修改了系統(tǒng)時間并不是馬上生效的,假如你修改了系統(tǒng)時間并馬上關(guān)機(jī),再開機(jī)的時候,時間還是原來的,因為修改的時間還沒有來得及寫入CMOS中。
3.通過settimeofday()函數(shù)來設(shè)置系統(tǒng)時間,這個函數(shù)設(shè)置的精度可以精確到微秒。
#include
intsettimeofday(const struct timeval *tv , const struct timezone *tz);
struct timeval {
    time_t      tv_sec;   
    suseconds_t tv_usec;   
};
struct timezone {
    inttz_minuteswest;   
    inttz_dsttime;        
};
tz參數(shù)為時區(qū),時區(qū)結(jié)構(gòu)中tz_dsttime在linux中不支持,應(yīng)該置為0,通常將參數(shù)tz設(shè)置為NULL,表示使用當(dāng)前系統(tǒng)的時區(qū)。該函數(shù)是glib中的,但在mingw中沒有實現(xiàn)。
該函數(shù)返回值與stime()一樣,同樣也需要root權(quán)限。
4.設(shè)置CMOS時間,其實它是通過RTC(Real-timeclock)設(shè)備驅(qū)動來完成的,你可以用ioctl()函數(shù)來設(shè)置時間,當(dāng)然也可以通過操作/dev/rtc設(shè)備文件,在此就不詳細(xì)說明了。深圳、廣州、鄭州專業(yè)嵌入式實訓(xùn),聯(lián)系郭老師QQ754634522
二、windows下設(shè)置系統(tǒng)時間
1.設(shè)置當(dāng)前時區(qū)的時間
#include
BOOL SetLocalTime(constSYSTEMTIME* lpSystemTime);
typedef struct _SYSTEMTIME{  // st
    WORD wYear;
    WORD wMonth; //月份從1開始
    WORD wDayOfWeek; //SetLocalTime()不使用這個參數(shù)
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME;
函數(shù)成功返回非零,失敗返回零。注意要求調(diào)用進(jìn)程必需有SE_SYSTEMTIME_NAME權(quán)限。
2.另外還有一個函數(shù)SetSystemTime(),它的參數(shù)與SetLocalTime一樣,只不過以UTC時區(qū)為基準(zhǔn)的。
BOOL SetSystemTime(constSYSTEMTIME* lpSystemTime);
二、 一個封裝的設(shè)置系統(tǒng)時間的函數(shù)
//設(shè)置成功返回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結(jié)構(gòu)中的年是從1970年開始的計數(shù)
       st.wMonth = t.tm_mon + 1; //注意structtm結(jié)構(gòu)中的月份是從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結(jié)構(gòu)時間轉(zhuǎn)換成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系統(tǒng)時間函數(shù)
1、gettimeofday(取得目前的時間)
#include
#include
intgettimeofday ( struct timeval * tv , struct timezone * tz )
函數(shù)說明  gettimeofday()會把目前的時間有tv所指的結(jié)構(gòu)返回,當(dāng)?shù)貢r區(qū)的信息則放到tz所指的結(jié)構(gòu)中。
timeval結(jié)構(gòu)定義為:
structtimeval{
longtv_sec;   
longtv_usec;
};
timezone結(jié)構(gòu)定義為:
structtimezone{
inttz_minuteswest;
inttz_dsttime;
};
上述兩個結(jié)構(gòu)都定義在/usr/include/sys/time.h。tz_dsttime所代表的狀態(tài)如下
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所指的內(nèi)存空間超出存取權(quán)限。
范例  #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);
}
執(zhí)行  tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0

2、settimeofday(設(shè)置目前時間)
#include
#include
intsettimeofday ( const struct timeval *tv,const struct timezone *tz);

函數(shù)說明  settimeofday()會把目前時間設(shè)成由tv所指的結(jié)構(gòu)信息,當(dāng)?shù)貢r區(qū)信息則設(shè)成tz所指的結(jié)構(gòu)。詳細(xì)的說明請參考gettimeofday()。注意,只有root權(quán)限才能使用此函數(shù)修改時間。

返回值  成功則返回0,失敗返回-1,錯誤代碼存于errno。
錯誤代碼  EPERM 并非由root權(quán)限調(diào)用settimeofday(),權(quán)限不夠。
EINVAL時區(qū)或某個數(shù)據(jù)是不正確的,無法正確設(shè)置時間。
3、clock_gettime(獲取指定時鐘的時間值)
#include
intclock_gettime( clockid_t clock_id,struct timespec * tp );
說明:clock_id指定要獲取時間的時鐘,根據(jù)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.
EPERM
buf.modeis non-zero and the caller does not have sufficient privilege.Under Linux theCAP_SYS_TIME capability is required.
CONFORMINGTO
adjtimex()is Linux specific and should not be used in programs intended to be portable.See adjtime(3) for a more portable, but less flexible, method of adjusting thesystem clock.更多嵌入式學(xué)習(xí)詳情聯(lián)系郭老師QQ754634522


本文地址:http://m.qingdxww.cn/thread-129873-1-1.html     【打印本頁】

本站部分文章為轉(zhuǎn)載或網(wǎng)友發(fā)布,目的在于傳遞和分享信息,并不代表本網(wǎng)贊同其觀點和對其真實性負(fù)責(zé);文章版權(quán)歸原作者及原出處所有,如涉及作品內(nèi)容、版權(quán)和其它問題,我們將根據(jù)著作權(quán)人的要求,第一時間更正或刪除。
您需要登錄后才可以發(fā)表評論 登錄 | 立即注冊

廠商推薦

  • Microchip視頻專區(qū)
  • Dev Tool Bits——使用MPLAB® Discover瀏覽資源
  • Dev Tool Bits——使用條件軟件斷點宏來節(jié)省時間和空間
  • Dev Tool Bits——使用DVRT協(xié)議查看項目中的數(shù)據(jù)
  • Dev Tool Bits——使用MPLAB® Data Visualizer進(jìn)行功率監(jiān)視
  • 貿(mào)澤電子(Mouser)專區(qū)

相關(guān)在線工具

相關(guān)視頻

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權(quán)所有   京ICP備16069177號 | 京公網(wǎng)安備11010502021702
快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产三级精品三级在专区 | 欧美精品无播放器在线观看 | 亚洲人成在线观看一区二区 | 成年一级片 | 日韩一区二区三区免费视频 | 亚洲高清在线播放 | 久久久精品免费国产四虎 | 99久久免费国产精精品 | 久久99国产精品二区不卡 | 日韩高清一级 | 99视频在线国产 | 91久久精品国产91性色tv | 四虎永久地址4hu紧急入口 | 20201精品极品国产色在线 | 久久精品国产亚洲婷婷 | 在线欧美日韩 | 欧美一区二区激情视频 | 污污视频网站免费在线观看 | 欧美色丁香| 国内一级一级毛片a免费 | 欧美成人全部视频 | 久久艹艹 | 精品日韩一区 | 牛牛影视成人午夜影视 | 欧美日韩高清一区二区三区 | 91精品免费在线观看 | 麻豆网站在线播放 | 国产精品免费看久久久香蕉 | 精品国产免费人成高清 | 2021国产麻豆剧果冻传媒入 | 亚洲国产一区在线观看 | 四虎在线最新永久免费播放 | 麻豆中文字幕 | 爱福利视频一区 | 九色视频在线观看免费 | 欧美在线看欧美高清视频免费 | 中文字幕 视频一区 | 国产日韩免费 | 999色综合 | 91极品女神嫩模在线播放 | 欧美三级视频在线播放 |