linux c获得时间和设置时间

  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结构体的原形如下:

 

  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是一个结构体,定义如下

  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. }   

 

 

求当前时间的示例

 

  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;

}

posted on 2013-08-12 09:24  carekee  阅读(31671)  评论(0编辑  收藏  举报