linux下获取当前时间的相关函数

 更新时间:2023年09月07日 09:01:59   作者:yun6853992  
这篇文章主要介绍了linux下获取当前时间的相关函数,具有很好的参考价值,希望对大家有所帮助,

1.可以获取当前时间的s数

结构体: time_t

函数

time_t time(time_t * timer)

2.可以获取到当前时间的微秒数

结构体:struct timeval

 struct timeval {
     time_t tv_sec; // seconds
     long tv_usec; // microseconds
 };

函数:

int gettimeofday(struct timeval *tv, struct timezone *tz);

3.可以获取到当前时间的纳秒数

结构体:struct timespec

 struct timespec {
     time_t tv_sec; // seconds
     long tv_nsec; // and nanoseconds
 };

函数:

int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间:

4.对获取到的时间做特定的格式化

结构体:

 struct tm {
     int tm_sec;      // Seconds (0-60) 
     int tm_min;      // Minutes (0-59) 
     int tm_hour;     // Hours (0-23) 
     int tm_mday;     // Day of the month (1-31) 
     int tm_mon;      // Month (0-11) 
     int tm_year;     // Year - 1900;从1900年算起,至今的年份 
     int tm_wday;     // Day of the week (0-6, Sunday = 0) 
     int tm_yday;     // Day in the year (0-365, 1 Jan = 0) 
     int tm_isdst;    // Daylight saving time;干啥用的??? 
 };

转换函数:

struct tm* localtime_r(const time_t* timer, struct tm* result );//线程安全

格式化函数:

size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );

5.相关测试代码

 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h> //time()
 #include <string.h>
 #include <unistd.h>
 #include <stdint.h> //uint64_t
 ​
 #include <sys/time.h> //gettimeofday
 void get_time();
 void get_time1();
 static uint64_t getNowTime();
 int main()
 {
 ​
     // time_t time(time_t * timer)  返回TC1970-1-1 0:0:0开始到现在的秒数
     // 与相关函数:localtime、gmtime、asctime、ctime,结合可以获得当前系统时间或是标准时间。
     //  struct tm* gmtime(const time_t *timep);    转换为没有经过时区转换的UTC时间,是一个struct tm结构指针
     //  stuct tm* localtime(const time_t *timep);  gmtime类似,但是它是经过时区转换的时间。
     //  char *asctime(const struct tm* timeptr);   转换为真实世界的时间,以字符串的形式显示
     //  char *ctime(const time_t *timep);          转换为真是世界的时间,以字符串显示
 ​
     //  int gettimeofday(struct timeval *tv, struct timezone *tz);
     //          返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用
     //  time_t mktime(struct tm* timeptr);         转换为从1970年至今的秒数
 ​
     //size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );
     //          可以通过该函数,对时间进行特定格式的字符串构造,进行时间格式的设置
 //所以,获取时间一般有两个函数time() 和gettimeofday()
 ​
     //time()函数的测试 两种使用方法
     time_t time0, time1;
     time0 = time(NULL);
     time(&time1); //获取当前时间,秒数
     printf("time() test is %ld = %ld \n", time0, time1);
     printf("ctime() :%s", ctime(&time1)); //直接字符串打印 带换行的
 ​
     //先用gmtime,没有经过时区转换的时间 只有这里才不会有时区的转换  差8小时
     struct tm* g_time = gmtime(&time0);
     printf("asctime(gmtime()) :%s", asctime(g_time)); //转换为字符串进行打印
     //用localtime 转换为经过时区转换的时间
     struct tm* l_time = localtime(&time0);
     printf("asctime(localtime()) :%s", asctime(l_time)); //转换为字符串进行打印
 ​
     printf("mktime(g_time) to s :[%ld] \n", mktime(g_time)); //这里默认带了时区的转换
     printf("mktime(l_time) to s :[%ld] \n", mktime(l_time));
 ​
     char tmpbuf[128];
     struct tm* newtime=localtime(&time1);
     //测试相关的格式打印
     strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
     printf("%s \n", tmpbuf);
 ​
     //其他两个结构体
     get_time();
     get_time1();
     printf("get ms time: %lu \n", getNowTime());
     return 0;
 }
 ​
 /******************
 秒和纳秒
 struct timespec {
     time_t tv_sec; // seconds
     long tv_nsec; // and nanoseconds
 };
 int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间:
     CLOCK_REALTIME              统当前时间,从1970年1.1日算起
     CLOCK_MONOTONIC             系统的启动时间,不能被设置
     CLOCK_PROCESS_CPUTIME_ID    本进程运行时间
     CLOCK_THREAD_CPUTIME_ID     本线程运行时间
 相关函数
     //把获得的时间秒数转换为struct tm 类型
     struct tm *localtime(const time_t *clock);  //线程不安全
     struct tm* localtime_r(const time_t* timer, struct tm* result );//线程安全
     //格式化时间字符串 
     size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );
 ​
 秒和微妙
 struct timeval {
     time_t tv_sec; // seconds
     long tv_usec; // microseconds
 };
 int gettimeofday(struct timeval *tv, struct timezone *tz)获取系统的时间
 ******************/
 //1:clock_gettime 操作timespec结构,返回秒+纳秒结构的时间,可以定义时钟类型
 void get_time()
 {
     struct timespec ts;
     clock_gettime(CLOCK_REALTIME, &ts);
     printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n",ts.tv_sec, ts.tv_nsec);
     struct tm t;
     char date_time[64];
     strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&ts.tv_sec, &t));//只到秒上
     printf("clock_gettime :date_time=%s, tv_nsec=%ld\n", date_time, ts.tv_nsec);
 }
 // 2:gettimeofday 获取系统时间 这里微妙和纳秒可以对结构体操作进行转换
 //  struct timeval now;
 //  struct timespec outtime;
 //  gettimeofday(&now, NULL);
 //  outtime.tv_sec = now.tv_sec; //s
 //  outtime.tv_nsec = now.tv_usec * 1000; //微妙转为纳秒
 void get_time1()
 {
     struct timeval us;
     gettimeofday(&us,NULL);
     printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec);
     struct tm t;
     char date_time[64];
     strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&us.tv_sec, &t));
     printf("gettimeofday: date_time=%s, tv_usec=%ld\n", date_time, us.tv_usec);
 }
 //获取当前时间  ms单位
 static uint64_t getNowTime()
 {
     struct timeval tval;
     uint64_t nowTime;
 ​
     gettimeofday(&tval, NULL);
 ​
     nowTime = tval.tv_sec * 1000L + tval.tv_usec / 1000L;
     return nowTime;
 }
 /******************************************
     struct tm {
         int tm_sec;      // Seconds (0-60) 
         int tm_min;      // Minutes (0-59) 
         int tm_hour;     // Hours (0-23) 
         int tm_mday;     // Day of the month (1-31) 
         int tm_mon;      // Month (0-11) 
         int tm_year;     // Year - 1900;从1900年算起,至今的年份 
         int tm_wday;     // Day of the week (0-6, Sunday = 0) 
         int tm_yday;     // Day in the year (0-365, 1 Jan = 0) 
         int tm_isdst;    // Daylight saving time;干啥用的??? 
     };
 ​
     %a: 英文单词中星期几的缩写版。如:星期三,表示为"Wed"。
 •   %A: 英文单词中星期几的完整版。如:星期三,表示为"Wednesday"。
 •   %b: 英文单词中月份的缩写版。如:11月,表示为"Nov"。
 •   %B: 英文单词中月份的缩写版。如:11月,表示为"November"。
 •   %c: 格式化的时间字符串。如:2018-11-28 10:13:40,表示为"Wed Nov 28 10:13:40 2018"。
 •   %F: 日期格式为yyyy-mm-dd,与%Y:%m:%d作用相同。如:2018-11-28。
 •   %X: 时间格式为hh:mm:ss。如:10:13:40。
 •   %j: 一年中的第几天,范围:001-366.
 •   %W: 一年中的第几周,范围:00-53.
 •   %Y: 日期中的年,如:2018。
 •   %m: 日期中的月,范围:00-12。
 •   %d: 日期中的天,范围:01-31。
 •   %H: 小时,范围:00-24。
 •   %M: 分钟,范围:00-59。
 •   %S: 秒,范围:00-60。
 ******************************************/

执行结果:

time() test is 1623892563 = 1623892563 
ctime() :Thu Jun 17 09:16:03 2021
asctime(gmtime()) :Thu Jun 17 01:16:03 2021
asctime(localtime()) :Thu Jun 17 09:16:03 2021
mktime(g_time) to s :[1623892563] 
mktime(l_time) to s :[1623892563] 
Today is Thursday, day 17 of June in the year 2021.
 
clock_gettime : tv_sec=1623892563, tv_nsec=702520168
clock_gettime :date_time=2021-06-17 09:16:03, tv_nsec=702520168
gettimeofday: tv_sec=1623892563, tv_usec=702558
gettimeofday: date_time=2021-06-17 09:16:03, tv_usec=702558
get ms time: 1623892563702 

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Linux堆内存修改及清理命令方式

    Linux堆内存修改及清理命令方式

    这篇文章主要介绍了Linux堆内存修改及清理命令方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • linux下统计appche站点IP访问量的shell脚本

    linux下统计appche站点IP访问量的shell脚本

    这篇文章主要介绍了linux下统计appche站点IP访问量的几种shell脚本以及执行结果
    2014-06-06
  • 如何在Linux下设置录音笔时间

    如何在Linux下设置录音笔时间

    这篇文章主要介绍了如何在Linux下设置录音笔时间的方法,有这方面需要的朋友跟着学习下吧。
    2017-12-12
  • apache tomcat 一个网站多域名的实现方法

    apache tomcat 一个网站多域名的实现方法

    因此处是进行多域名设置,所以 Apache 与 tomcat的结合没有详述,此处只是设置多域名的方法
    2009-02-02
  • Linux下apache如何限制并发连接和下载速度

    Linux下apache如何限制并发连接和下载速度

    在Linux下限值Apache的并发连接数和下载速度需要用到一款Apache的扩展模块mod_limitipconn,下面我们就来讨论mod_limitipconn的安装使用方法
    2014-11-11
  • 11个有用的Linux命令

    11个有用的Linux命令

    Linux命令行吸引了大多数Linux爱好者。一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务。今天为你解释下面几个命令:sudo、python、mtr、Ctrl+x+e、nl、shuf、shuf、last。
    2013-10-10
  • apache启用gzip压缩的实现方法

    apache启用gzip压缩的实现方法

    对于部署在Linux服务器上的PHP程序,在服务器支持的情况下,我们建议你开启使用Gzip Web压缩,以前脚本之家介绍了iis中的开启方法,这篇文章主要介绍了linux中apache的开启方法
    2013-06-06
  • centos源码编译php5 mcrypt模块步骤详解

    centos源码编译php5 mcrypt模块步骤详解

    服务器安装的是php5.3版本,源中没有包含mcrypt扩展,只有自己从源码中编译mcrypt模块了,看下面的详细步骤
    2013-12-12
  • 配置Linux服务器SSH 安全访问的四个小技巧

    配置Linux服务器SSH 安全访问的四个小技巧

    越来越多的站长,开始使用独立主机(Dedicated Host)和 VPS。而为了节省成本或提高性能,不少人的独机和 VPS,都是基于 unmanaged 的裸机,一切都要自己 DIY。这时候,安全策略的实施,就犹为重要。
    2010-12-12
  • CentOS MySQL 5.7编译安装步骤详细说明

    CentOS MySQL 5.7编译安装步骤详细说明

    这篇文章主要介绍了CentOS MySQL 5.7编译安装详细介绍的相关资料,这里对安装步骤进行了详细介绍,需要的朋友可以参考下
    2016-12-12

最新评论