Linux-C语言中gettimeofday()函数的使用方法(转载)
1.简介:
在C语言中可以使用函数gettimeofday()函数来得到时间。它的精度可以达到微妙
2.函数原型:
#include<sys/time.h>
int gettimeofday(struct timeval*tv,struct timezone *tz )
3.说明:
gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
4.结构体:
1>timeval
struct timeval{
long tv_sec;/*秒*/
long tv_usec;/*微妙*/
};
2>timezone 结构定义为:
struct timezone{
int tz_minuteswest;/*和greenwich 时间差了多少分钟*/
int tz_dsttime;/*type of DST correction*/
}
3>在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。
4>函数执行成功后返回0,失败后返回-1,错误代码存于errno中。
5.程序实例:
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
int main()
{
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);
}
说明:在使用gettimeofday()函数时,第二个参数一般都为空,因为我们一般都只是为了获得当前时间,而不用获得timezone的数值
gettimeofday()
Get the current time
Synopsis:
#include <sys/time.h> int gettimeofday( struct timeval * when,
void * not_used );
Arguments:
- when
- A pointer to a timeval structure where the function can store the time. The struct timeval contains the following members:
- time_t tv_sec — the number of seconds since the start of the Unix Epoch.
- suseconds_t tv_usec — the number of microseconds.
- not_used
- This pointer must be NULL or the behavior of gettimeofday() is unspecified. This argument is provided only for backwards compatibility.
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
The gettimeofday() function returns the current time in when in seconds and microseconds, since the Unix Epoch, 00:00:00 January 1, 1970 Coordinated Universal Time (UTC) (formerly known as Greenwich Mean Time (GMT)).
Returns:
0 for success, or -1 if an error occurs (errno is set).
Errors:
- EFAULT
- An error occurred while accessing the when buffer.
Linux-C语言中gettimeofday()函数的使用方法(转载)的更多相关文章
- C语言中malloc函数的使用方法
C语言中malloc是动态内存分配函数.函数原型:void *malloc(unsigned int num_bytes);参数:num_bytes 是无符号整型,用于表示分配的字节数.返回值:如果分 ...
- C语言中system()函数的用法总结(转)
system()函数功能强大,很多人用却对它的原理知之甚少先看linux版system函数的源码: #include <sys/types.h> #include <sys/wait ...
- C语言中qsort函数用法
C语言中qsort函数用法-示例分析 本文实例汇总介绍了C语言中qsort函数用法,包括针对各种数据类型参数的排序,非常具有实用价值非常具有实用价值. 分享给大家供大家参考.C语言中的qsort ...
- 【转】Linux时间函数之gettimeofday()函数之使用方法
原文网址:http://blog.csdn.net/tigerjibo/article/details/7039434 一.gettimeofday()函数的使用方法: 1.简介: 在C语言中可以使用 ...
- 使用C语言中qsort()函数对浮点型数组无法成功排序的问题
一 写在开头 1.1 本节内容 本节主要内容是有关C语言中qsort()函数的探讨. 二 问题和相应解决方法 qsort()是C标准库中的一个通用的排序函数.它既能对整型数据进行排序也能对浮点型数据进 ...
- C语言中malloc函数返回值是否需要类型强制转换问题
1. 在C语言中, 如果调用的函数没有函数原型, 则其返回值将默认为 int 型. 考虑调用malloc函数时忘记了 #include <stdlib.h>的情况 此时malloc函数返回 ...
- R语言中apply函数
前言 刚开始接触R语言时,会听到各种的R语言使用技巧,其中最重要的一条就是不要用循环,效率特别低,要用向量计算代替循环计算. 那么,这是为什么呢?原因在于R的循环操作for和while,都是基于R语言 ...
- (转)C语言中Exit函数的使用
C语言中Exit函数的使用 exit() 结束当前进程/当前程序/,在整个程序中,只要调用 exit ,就结束return() 是当前函数返回,当然如果是在主函数main, 自然也就结束当前进程了,如 ...
- C语言中malloc函数的理解
在C语言中malloc函数主要是用在堆内存的申请上,使用malloc函数时,函数会返回一个void *类型的值,这个值就是你申请的堆内存的首地址:为什么返回的地址是一个void *类型的地址呢?首先我 ...
随机推荐
- 【集训笔记】【大数模板】特殊的数 【Catalan数】【HDOJ1133【HDOJ1134【HDOJ1130
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3324 http://blog.csdn.net/xymscau/artic ...
- nginx sendfile tcp_nopush tcp_nodelay参数解释
sendfile 现在流行的web 服务器里面都提供 sendfile 选项用来提高服务器性能,那到底 sendfile是什么,怎么影响性能的呢?sendfile实际上是 Linux2.0+以后的推出 ...
- Shell之sed命令
sed用于一次性处理所有的编辑任务,尤为高效,为用户节省了大量的时间,sed适用于以下三种场合: 1.编辑相对交互文本编辑器而言太大的文件: 2.编辑命令太复杂,在交互式文本编辑器中难以输入的情况: ...
- html+css实现图片的层布局
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 基于visual Studio2013解决C语言竞赛题之1006填空
题目 解决代码及点评 /************************************************************************/ ...
- 基于Andoird 4.2.2的Account Manager源代码分析学习:AccountManagerService系统服务的添加
从启动说起 Android系统加载时,首先启动init进程,该进程会启动Zygote进程.Zygote进程执行/system/bin/app_process程序.app_process程序在执行中,通 ...
- aix网络管理
lsdev -Cc adapter | grep ent 列出网卡 lsdev -Cc adapter 或者lscfg | grep -i adpter 显示已经安装的网卡 lsdev -Cc i ...
- 圣魔大战3(Castle Fantisia)艾伦希亚战记完美攻略
作为城堡幻想曲系列续作,艾伦希亚战记继承了前作的战棋+养成模式进行游戏. (城堡幻想曲3,纠正大家个错误哦,不是圣魔大战3,圣魔大战是城堡幻想曲2,圣魔大战不是个系列,艾伦西亚战记==艾伦希亚战记,一 ...
- MongoDB系列之二(主动复制)
目前我正在进行MongoDB的双机热备方面相关的工作.根据我目前看到的MongoDB方面的材料,MongoDB的实际部署有三种方式,分别是“主动复制”,“副本集”以及“分片副本集”. 首先我们从最简单 ...
- Mnesia基本用法
查看表结构 查看mnesia表的结构: mnesia:info(). 查看此表的基本信息: mnesia:table_info(<tableName>, all). Mnesia初使化 m ...