linux c獲得時間和設置時間


[c-sharp] view plain copy print ?
  1. #include<time.h> //C語言的頭文件   
  2. #include<stdio.h> //C語言的I/O    
  3.   
  4. void main()   
  5. {   
  6. time_t now; //實例化time_t結構    
  7. struct tm *timenow; //實例化tm結構指針    
  8. time(&now);   
  9. //time函數讀取現在的時間(國際標准時間非北京時間),然后傳值給now    
  10.   
  11. timenow = localtime(&now);   
  12. //localtime函數把從time取得的時間now換算成你電腦中的時間(就是你設置的地區)    
  13.   
  14. printf("Local time is %s/n",asctime(timenow));   
  15. //上句中asctime函數把時間轉換成字符,通過printf()函數輸出    
  16. }   

 


注釋:time_t是一個在time.h中定義好的結構體。而tm結構體的原形如下:

 

[c-sharp] view plain copy print ?
  1. struct tm   
  2. {   
  3. int tm_sec;//seconds 0-61    
  4. int tm_min;//minutes 1-59    
  5. int tm_hour;//hours 0-23    
  6. int tm_mday;//day of the month 1-31    
  7. int tm_mon;//months since jan 0-11    
  8. int tm_year;//years from 1900    
  9. int tm_wday;//days since Sunday, 0-6    
  10. int tm_yday;//days since Jan 1, 0-365    
  11. int tm_isdst;//Daylight Saving time indicator    
  12. };  

 


#include "time.h"

time_t time(time_t *timer);
調用后將當前系統時間與1900年1月1日相差的秒數存入到timer中,timer可看成是一個長整型數
struct tm* localtime(const time_t *timer)
將time()函數調用的結果做為參數傳入到localtime()函數中就能得到當前時間和日期,注意得到的年是和1970的差值,月份是和1月的差值


struct tm是一個結構體,定義如下

[c-sharp] view plain copy print ?
  1. struct tm   
  2. {   
  3. int tm_sec; //當前秒    
  4. int tm_min; //當前分鍾    
  5. int tm_hour; //當前小時    
  6. int tm_mday; //當前在本月中的天,如11月1日,則為1    
  7. int tm_mon; //當前月,范圍是0~11    
  8. int tm_year; //當前年和1900的差值,如2006年則為36    
  9. int tm_wday; //當前在本星期中的天,范圍0~6    
  10. int tm_yday; //當前在本年中的天,范圍0~365    
  11. int tm_isdst; //這個我也不清楚    
  12. }   

 

 

求當前時間的示例

 

[c-sharp] view plain copy print ?
  1. int getSystemTime()   
  2. {   
  3. time_t timer;   
  4. struct tm* t_tm;   
  5. time(&timer);   
  6. t_tm = localtime(&timer);   
  7. printf("today is %4d%02d%02d%02d%02d%02d/n", t_tm.tm_year+1900,   
  8. t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec);   
  9. return 0;   
  10. }   
  11.   
  12. /************************************************ 
  13. 設置操作系統時間 
  14. 參數:*dt數據格式為"2006-4-20 20:30:30" 
  15. 調用方法: 
  16.     char *pt="2006-4-20 20:30:30"; 
  17.     SetSystemTime(pt); 
  18. **************************************************/  
  19. int SetSystemTime(char *dt)  
  20. {  
  21.     struct rtc_time tm;  
  22.     struct tm _tm;  
  23.     struct timeval tv;  
  24.     time_t timep;  
  25.     sscanf(dt, "%d-%d-%d %d:%d:%d", &tm.tm_year,  
  26.         &tm.tm_mon, &tm.tm_mday,&tm.tm_hour,  
  27.         &tm.tm_min, &tm.tm_sec);  
  28.     _tm.tm_sec = tm.tm_sec;  
  29.     _tm.tm_min = tm.tm_min;  
  30.     _tm.tm_hour = tm.tm_hour;  
  31.     _tm.tm_mday = tm.tm_mday;  
  32.     _tm.tm_mon = tm.tm_mon - 1;  
  33.     _tm.tm_year = tm.tm_year - 1900;  
  34.   
  35.     timep = mktime(&_tm);  
  36.     tv.tv_sec = timep;  
  37.     tv.tv_usec = 0;  
  38.     if(settimeofday (&tv, (struct timezone *) 0) < 0)  
  39.     {  
  40.     printf("Set system datatime error!/n");  
  41.     return -1;  
  42.     }  
  43.     return 0;  
  44. }  

 

 

其他時間的函數和結構還有:


timeval結構


#include <include/linux/time.h>

struct timeval
{
time_t tv_sec;
susecond_t tv_usec; //當前妙內的微妙數
};

 

tms結構
保存着一個進程及其子進程使用的cpu時間

 

struct tms
{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}

 

timer_struct結構


#include <include/linux/timer.h>

struct timer_struct
{
unsigned long expires; //定時器被激活的時刻
void (*fn)(void); //定時器激活后的處理函數
}

 

utime函數
更改文件的存取和修改時間

int utime(const char pathname, const struct utimbuf *times) // return value 0 or -1

times 為空指針,存取和修改時間設置為當前時間

 

struct utimbuf
{
time_t actime;
time_t modtime;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM