linux c获得时间和设置时间

本文介绍如何使用C语言处理时间,包括获取当前时间、格式化输出时间及设置系统时间的方法,并详细解释了time_t和tm结构体的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#include<time.h> //C语言的头文件 #include<stdio.h> //C语言的I/O void main() { time_t now; //实例化time_t结构 struct tm *timenow; //实例化tm结构指针 time(&now); //time函数读取现在的时间(国际标准时间非北京时间),然后传值给now timenow = localtime(&now); //localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区) printf("Local time is %s/n",asctime(timenow)); //上句中asctime函数把时间转换成字符,通过printf()函数输出 }

 


注释: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. };  

struct tm { int tm_sec;//seconds 0-61 int tm_min;//minutes 1-59 int tm_hour;//hours 0-23 int tm_mday;//day of the month 1-31 int tm_mon;//months since jan 0-11 int tm_year;//years from 1900 int tm_wday;//days since Sunday, 0-6 int tm_yday;//days since Jan 1, 0-365 int tm_isdst;//Daylight Saving time indicator };

 


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

struct tm { int tm_sec; //当前秒 int tm_min; //当前分钟 int tm_hour; //当前小时 int tm_mday; //当前在本月中的天,如11月1日,则为1 int tm_mon; //当前月,范围是0~11 int tm_year; //当前年和1900的差值,如2006年则为36 int tm_wday; //当前在本星期中的天,范围0~6 int tm_yday; //当前在本年中的天,范围0~365 int tm_isdst; //这个我也不清楚 }

 

 

求当前时间的示例

 

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

int getSystemTime() { time_t timer; struct tm* t_tm; time(&timer); t_tm = localtime(&timer); printf("today is %4d%02d%02d%02d%02d%02d/n", t_tm.tm_year+1900, t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec); return 0; } /************************************************ 设置操作系统时间 参数:*dt数据格式为"2006-4-20 20:30:30" 调用方法: char *pt="2006-4-20 20:30:30"; SetSystemTime(pt); **************************************************/ int SetSystemTime(char *dt) { struct rtc_time tm; struct tm _tm; struct timeval tv; time_t timep; sscanf(dt, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,&tm.tm_hour, &tm.tm_min, &tm.tm_sec); _tm.tm_sec = tm.tm_sec; _tm.tm_min = tm.tm_min; _tm.tm_hour = tm.tm_hour; _tm.tm_mday = tm.tm_mday; _tm.tm_mon = tm.tm_mon - 1; _tm.tm_year = tm.tm_year - 1900; timep = mktime(&_tm); tv.tv_sec = timep; tv.tv_usec = 0; if(settimeofday (&tv, (struct timezone *) 0) < 0) { printf("Set system datatime error!/n"); return -1; } return 0; }

 

 

其他时间的函数和结构还有:


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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值