关于时间的几个函数

本文详细介绍了多种时间处理的数据结构,如struct tm、struct timeval、struct timezone等,并深入解析了time()、localtime()、gmtime()、mktime()、gettimeofday()、clock_gettime()、ftime()等常用时间函数的功能及使用示例。

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

关于时间的处理


1,常用的结构体
(1),struct tm
struct tm {
                int tm_sec;         /* seconds */
                int tm_min;         /* minutes */
                int tm_hour;        /* hours */
                int tm_mday;        /* day of the month */
                int tm_mon;         /* month */
                int tm_year;        /* year */
                int tm_wday;        /* day of the week */
                int tm_yday;        /* day in the year */
                int tm_isdst;       /* daylight saving time */
            };
 
 //int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
 //int tm_min 代表目前分数,范围0-59
 //int tm_hour 从午夜算起的时数,范围为0-23
 //int tm_mday 目前月份的日数,范围01-31
 //int tm_mon 代表目前月份,从一月算起,范围从0-11
 //int tm_year 从1900 年算起至今的年数
 //int tm_wday 一星期的日数,从星期一算起,范围为0-6
 //int tm_yday 从今年1月1日算起至今的天数,范围为0-365
 //int tm_isdst 日光节约时间的旗标
 
(2),struct timeval,struct timezone;
struct timeval {
                time_t      tv_sec;     /* seconds (秒)*/
                suseconds_t tv_usec;    /* microseconds(微秒) */
            };
 struct timezone {
                int tz_minuteswest;     /* minutes west of Greenwich */
                int tz_dsttime;         /* type of DST correction */
            };
 int tz_minuteswest;     /* 格林威治时间往西方的时差 */
 int tz_dsttime;         /*  时间的修正方式*/    
 
(3),struct timespec
struct timespec {
time_t tv_sec; // seconds (秒)
long tv_nsec; // and nanoseconds(纳秒)
};

(4), struct timeb
struct timeb {
               time_t         time;
               unsigned short millitm;//毫秒
               short          timezone;
               short          dstflag;
           };
   
2,时间函数介绍
(1),time()函数获取当前时间
 #include <time.h>
 
 time_t time(time_t *t);
//此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。

eg:
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
 
 int main()
 {
     time_t seconds;
 
     seconds = time((time_t *)NULL);
     printf("%d\n", seconds);
 
     return 0;
 }

(2),localtime_r() localtime()取得当地目前时间和日期
#include <time.h>
 
     struct tm *localtime(const time_t *timep);
     struct tm *localtime_r(const time_t *timep, struct tm *result);
 
 /*该函数将有time函数获取的值timep转换真实世界所使用的时间日期表示方法,然后将结果由结构tm返回*/
 
 /**需要注意的是localtime函数可以将时间转换本地时间,但是localtime函数不是线程安全的。
 多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的**/
 
eg:
#include <stdio.h>
 #include <string.h>
 #include <time.h>
 
 int main()
 {
     time_t timep;
     struct tm *p; 
 
     time(&timep);
     p = localtime(&timep);
    printf("本地时间:%d-%d-%d-%d-%d-%d\n",ptr->tm_year + 1900,ptr->tm_mon + 1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);
     return 0;
 }
 
(3),asctime()  asctime_r() 将时间和日期以字符串格式返回‘
 #include <time.h>
 
     struct tm *gmtime(const time_t *timep);
     struct tm *gmtime_r(const time_t *timep, struct tm *result);
 
     char *asctime(const struct tm *tm);
     char *asctime_r(const struct tm *tm, char *buf);
 
 /**gmtime是把日期和时间转换为格林威治(GMT)时间的函数。将参数time 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回**/
 
 /**asctime 将时间以换为字符串字符串格式返回 **/

 eg:
 #include <stdio.h>
 #include <string.h>
 #include <time.h> 
 
 int main()
 {
     time_t timep;
     time(&timep);
 
     printf("%s\n", asctime(gmtime(&timep)));   
 
     ;
 } 

(4),ctime(),ctime_r() 将时间和日期以字符串格式表示
#include <time.h>
 
        char *ctime(const time_t *timep);
        char *ctime_r(const time_t *timep, char *buf);
 
 /**ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回**/
 
eg:
#include <stdio.h>
 #include <string.h>
 #include <time.h>   
 
 int main(void)
 {
     time_t timep;
 
     time(&timep);
     printf("%s\n", ctime(&timep));
 
     ;
 }

(5),mktime() 将时间结构体struct tm的值转化为经过的秒数

#include <time.h>
 
     time_t mktime(struct tm *tm);
 
 /**将时间结构体struct tm的值转化为经过的秒数**/
 
eg:
#include <stdio.h>
 #include <string.h>
 #include <time.h>  
 
 int main()
 {
     time_t timep;
     struct tm *p;   
 
     time(&timep);
     p = localtime(&timep);
     timep = mktime(p);
 
     printf("%d\n", timep);   
 
     ;
 }
 
(6),gettimeofday() 获取当前时间
#include <sys/time.h>
 
     int gettimeofday(struct timeval *tv, struct timezone *tz);
 
 struct timeval {
                time_t      tv_sec;     /* seconds (秒)*/
                suseconds_t tv_usec;    /* microseconds(微秒) */
            };
 struct timezone {
                int tz_minuteswest;     /* minutes west of Greenwich */和格林威治时间差多少分钟
                int tz_dsttime;         /* type of DST correction */日光节约时间的状态
            };
 //gettimeofday函数获取当前时间存于tv结构体中,相应的时区信息则存于tz结构体中
 //需要注意的是tz是依赖于系统,不同的系统可能存在获取不到的可能,因此通常设置为NULL  
eg:
#include <stdio.h>
 #include <string.h>
 #include <sys/time.h>
 
 int main()
 {
     struct timeval tv; 
 
     gettimeofday(&tv, NULL);
 
     printf("tv_sec: %d\n", tv.tv_sec);
     printf("tv_usec: %d\n", tv.tv_usec);
 
     return 0;
 }
 
(7),clock_gettime
int clock_gettime(clockid_t clk_id, struct timespec* tp);

clk_id : 检索和设置的clk_id指定的时钟时间。
CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,
                中间时刻如果系统时间被用户改成其他,则对应的时间相应改变
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间

eg:
Link with -lrt.
int main()
 {
     struct timespec t;
     
     clock_gettime(CLOCK_REALTIME,&t);
     printf("tv_sec:%d\n",t.tv_sec);
     printf("tv_nsec:%d\n",t.tv_nsec);
     
     return 0;
 }

(8),ftime
  #include <sys/timeb.h>

       int ftime(struct timeb *tp);
    精确到毫秒
eg:
int main()
{
      struct   timeb   tp;

      ftime(&tp);
      printf("time:%d\n", tp.time);
      printf("millitm:%d\n",tp.millitm);
      printf("timezone:%d\n",tp.timezone);
      printf("dstflag:%d\n",tp.dstflag);
      return 0;
}

函数            精确级别                数据类型
time            秒级(s)                    time_t
ftime            毫秒级(ms)                struct   timeb
gettimeofday    微妙级(us)                struct timeval
clock_gettime    纳秒级(ns)                struct timespec


ctime和asctime区别:都是时间字符串 参数不一样,前者是time_t 后者是struct tm

mktime和gmtime,localtime是一个逆过程。
mktime 输入是struct tm 输出是time_t
gmtime,localtime 输入是time_t 输出是struct tm

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值