linux常用的时间获取函数(time,gettimeofday,clock_gettime,_ftime,localtime,strftime )
time()提供了秒级的精确度 1、头文件 <time.h>
2、函数原型
time_t time(time_t * timer)
函数返回从TC1970-1-1 0:0:0开始到现在的秒数 用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。 #include <time.h>
#include <stdio.h>
int main(void)
{
time_t t;
t = time(NULL);
printf("The number of seconds since January 1, 1970 is %ld",t); return 0;
} #include <stdio.h>
#include <stddef.h>
#include <time.h>
int main(void)
{
time_t timer;//time_t就是long int 类型
struct tm *tblock;
timer = time(NULL);//这一句也可以改成time(&timer);
tblock = localtime(&timer);
printf("Local time is: %s/n",asctime(tblock)); return 0;
}
gettimeofday()提供了微秒级的精确度 1、头文件 <time.h>
2、函数原型
int gettimeofday(struct timeval *tv, struct timezone *tz); gettimeofday()会把目前的时间由tv所指的结构返回,当地时区的信息则放到tz所指的结构中(可用NULL)。
参数说明:
timeval结构定义为:
struct timeval
{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 结构定义为:
struct timezone
{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/ 返回值: 成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。 #include<stdio.h>
#include<time.h>
int main(void)
{
struct timeval tv;
struct timezone tz; gettimeofday (&tv , &tz); printf(“tv_sec; %d/n”, tv,.tv_sec) ;
printf(“tv_usec; %d/n”,tv.tv_usec); printf(“tz_minuteswest; %d/n”, tz.tz_minuteswest);
printf(“tz_dsttime, %d/n”,tz.tz_dsttime); return 0;
}
clock_gettime( ) 提供了纳秒级的精确度 1、头文件 <time.h>
2、编译&链接。在编译链接时需加上 -lrt ;因为在librt中实现了clock_gettime函数
3、函数原型
int clock_gettime(clockid_t clk_id, struct timespect *tp);
参数说明:
clockid_t clk_id 用于指定计时时钟的类型,有以下4种:
CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间
struct timespect *tp用来存储当前的时间,其结构如下:
struct timespec
{
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
返回值。0成功,-1失败 #include<stdio.h>
#include<time.h>
int main()
{
struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts);
printf("CLOCK_REALTIME: %d, %d", ts.tv_sec, ts.tv_nsec); clock_gettime(CLOCK_MONOTONIC, &ts);//打印出来的时间跟 cat /proc/uptime 第一个参数一样
printf("CLOCK_MONOTONIC: %d, %d", ts.tv_sec, ts.tv_nsec); clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec); clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
printf("CLOCK_THREAD_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec); printf("/n%d/n", time(NULL)); return 0;
}
/proc/uptime里面的两个数字分别表示:
the uptime of the system (seconds), and the amount of time spent in idle process (seconds).
把第一个数读出来,那就是从系统启动至今的时间,单位是秒
_ftime()提供毫秒级的精确度 1、头文件 <sys/types.h> and <sys/timeb.h>
2、函数原型
void _ftime(struct _timeb *timeptr);
参数说明:
struct _timeb
{
time_t time;
unsigned short millitm;
short timezone;
short dstflag;
}; #include <stdio.h>
#include <sys/timeb.h>
#include <time.h> void main( void )
{
struct _timeb timebuffer;
char *timeline; _ftime( &timebuffer );
timeline = ctime( & ( timebuffer.time ) ); printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );
}
头文件:#include <time.h> 定义函数:struct tm *localtime(const time_t * timep); 函数说明:localtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm 返回。结构tm 的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。 返回值:返回结构tm 代表目前的当地时间。 范例
#include <time.h>
main(){
char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t timep;
struct tm *p;
time(&timep);
p = localtime(&timep); //取得当地时间
printf ("%d%d%d ", (1900+p->tm_year), (l+p->tm_mon), p->tm_mday);
printf("%s%d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
} 执行结果:
2000/10/28 Sat 11:12:22
size_t strftime ( char * ptr, size_t maxsize, const char * format, const struct tm * timeptr );
Copies into ptr the content of format, expanding its format tags into the corresponding values as specified bytimeptr, with a limit of maxsize characters.
Parameters
- ptr
- Pointer to the destination array where the resulting C string is copied.
- maxsize
- Maximum number of characters to be copied to ptr.
- format
- C string containing any combination of regular characters and special format specifiers. These format specifiers are replaced by the function to the corresponding values to represent the time specified in timeptr. They all begin with a percentage (%) sign, and are:
specifier Replaced by Example %a Abbreviated weekday name * Thu %A Full weekday name * Thursday %b Abbreviated month name * Aug %B Full month name * August %c Date and time representation * Thu Aug 23 14:55:02 2001 %d Day of the month (01-31) 23 %H Hour in 24h format (00-23) 14 %I Hour in 12h format (01-12) 02 %j Day of the year (001-366) 235 %m Month as a decimal number (01-12) 08 %M Minute (00-59) 55 %p AM or PM designation PM %S Second (00-61) 02 %U Week number with the first Sunday as the first day of week one (00-53) 33 %w Weekday as a decimal number with Sunday as 0 (0-6) 4 %W Week number with the first Monday as the first day of week one (00-53) 34 %x Date representation * 08/23/01 %X Time representation * 14:55:02 %y Year, last two digits (00-99) 01 %Y Year 2001 %Z Timezone name or abbreviation CDT %% A % sign % * The specifiers whose description is marked with an asterisk (*) are locale-dependent.
- timeptr
- Pointer to a tm structure that contains a calendar time broken down into its components (see tm).
Return Value
If the resulting C string fits in less than maxsize characters including the terminating null-character, the total number of characters copied to ptr (not including the terminating null-character) is returned.
Otherwise, zero is returned and the contents of the array are indeterminate.
Portability
This description corresponds to the C++ version of this function (which is the same as in the ISO-C Standard of 1990). C compilers may support additional specifiers and modifiers for the format parameter of this function, which are not described here.
Example
/* strftime example */
#include <stdio.h>
#include <time.h> int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer []; time ( &rawtime );
timeinfo = localtime ( &rawtime ); strftime (buffer,,"Now it's %I:%M%p.",timeinfo);
puts (buffer);
return ;
}
Example output:
Now it's 03:21PM. |
linux常用的时间获取函数(time,gettimeofday,clock_gettime,_ftime,localtime,strftime )的更多相关文章
- Linux下精确控制时间的函数
Linux下精确控制时间的函数 在测试程序接口运行时间的时候,常用time,gettimeofday等函数,但是这些函数在程序执行的时候是耗费时间的,如果仅仅测试时间还行,但是如果程序中用到时间控制类 ...
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
time()提供了秒级的精确度 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在的秒 ...
- MYSQL常用的时间日期函数
#时间日期函数 #获取当前日期XXXX-XX-XXSELECT CURRENT_DATE(); SELECT CURDATE();#效果与上一条相同 #获取当前日期与时间XXXX-XX-XX XX:X ...
- mysql 常用的时间日期函数小结
本文主要是总结一些常用的在实际运用中常用的一些mysql时间日期以及转换的函数 1.now() :返回当前日期和时间 select now(); //2018-04-21 09:19:21 2.cu ...
- 银弹谷零代码开发V百科|使用技巧:OMG!这些时间日期函数太好用了吧,盘它
银弹谷零代码开发V百科|使用技巧:OMG!这些时间日期函数太好用了吧,盘它 Hello~everybody!小V又来咯!这次小V给大家带来的是零代码开发V平台常用的时间日期函数.小V知道我们平时常常会 ...
- [转]Android时间获取与使用
编写Android网络程序时难免会遇到手机时间不准确的问题,本文总结了一些常用的时间获取与校正方法: 转载请注明:http://blog.csdn.net/xzy2046 1.获取本机当前时间: Ti ...
- linux几种时间函数总结
一.linux时间函数总结 最近的工作中用到的时间函数比较频繁,今天抽时间总结一下,在linux下,常用的获取时间的函数有如下几个: asctime, ctime, gmtime, localti ...
- (笔记)Linux延时及时间函数总结
一. 基础知识1.时间类型.Linux下常用的时间类型有4个:time_t,struct timeval,struct timespec,struct tm.(1)time_t是一个长整型,一般用来表 ...
- Linux常用系统函数
Linux常用系统函数 一.进程控制 fork 创建一个新进程clone 按指定条件创建子进程execve 运行可执行文件exit 中止进程_exit 立即中止当前进程getdtablesize 进程 ...
随机推荐
- 浅谈我为什么选择用Retrofit作为我的网络请求框架
比较AsyncTask.Volley.Retrofit三者的请求时间 使用 单次请求 7个请求 25个请求 AsyncTask 941ms 4539ms 13957ms Volley 560ms 22 ...
- 苹果新的编程语言 Swift 语言进阶(十六)--泛型
泛型允许你定义一个宽松.可重用的函数或者类型,使用泛型能够避免代码的重复,也能以更清楚和抽象的方式来表达程序的意图. 泛型是Swift语言提供的强大功能之一,Swift提供的许多标准库都使用了泛型来创 ...
- C语言之将无符号字符型转化为ascii码值
这个宏是在linux内核中获取的,主要的功能是能够将一个无符号字符型的参数转化为ASCII码值. ASCII : ASCII 编码里包括了128个字符.用 十进制 0 到 127 来表示 .那就对了 ...
- mahout系列之---谱聚类
1.构造亲和矩阵W 2.构造度矩阵D 3.拉普拉斯矩阵L 4.计算L矩阵的第二小特征值(谱)对应的特征向量Fiedler 向量 5.以Fiedler向量作为kmean聚类的初始中心,用kmeans聚类 ...
- HBase中创建索引
hbasene(https://github.com/akkumar/hbasene)是开源项目,在hbase存储上封装使用Lucene来创建索引,代码API非常简单,熟悉lucene的朋友可以很方便 ...
- vimgrep 搜索总结
vimgrep /匹配模式/[g][j] 要搜索的文件/范围 g:表示是否把每一行的多个匹配结果都加入 j:表示是否搜索完后定位到第一个匹配位置 vimgrep /pattern/ % ...
- 你真的知道.NET Framework中的阻塞队列BlockingCollection的妙用吗?
BlockingCollection集合是一个拥有阻塞功能的集合,它就是完成了经典生产者消费者的算法功能.一般情况下,我们可以基于 生产者 - 消费者模式来实现并发.BlockingCollectio ...
- R实战 第七篇:绘图文本表
文本表是显示数据的重要图形,一个文本表按照区域划分为:列标题,行标题,数据区,美学特征有:前景样式.背景央视.字体.网格线等. 一,使用ggtexttable绘图文本表 载入ggpubr包,可以使用g ...
- 实例解析Collections源码,Iterator和ListIterator
比如一个视频或文章有多个页面标签设置,我们在看一篇文章或一个视频时,底部有为你推荐栏目. 如何根据这个文章或视频的标签,来实现这个推荐栏目呢. public List<VideoInfoVo&g ...
- 菜鸟级Git GitHub创建仓库
菜鸟标准:知道pwd ,rm 命令是什么. 一.Git 是什么. git 是目前世界上最先进的分布式版本控制系统 二.SVN与Git 1.版本控制系统 SVN 是集中式版本控制系统,版本库是集中放在中 ...