struct timeval 结构体使用浅析

时间结构体struct timeval的使用

#include <sys/time.h>

该头文件的位置在:/usr/include/x86_64-linux-gnu/sys/time.h。事实上,该文件只是引入了#include <bits/types/struct_timeval.h>,并未定义结构体。

(1)struct timeval结构体定义
/usr/include/x86_64-linux-gnu/bits/types/struct_timeval.h

#include <bits/types.h>

/* A time value that is accurate to the nearest
   microsecond but also has a range of years.  */
struct timeval
{
  __time_t tv_sec;              /* Seconds.  */
  __suseconds_t tv_usec;        /* Microseconds.  */
};

(2)__time_t__suseconds_t宏定义
/usr/include/x86_64-linux-gnu/bits/types.h

#define __SLONGWORD_TYPE        long int
#define __ULONGWORD_TYPE        unsigned long int
/* quad_t is also 64 bits.  */
#if __WORDSIZE == 64
typedef long int __quad_t;
typedef unsigned long int __u_quad_t;
#else
__extension__ typedef long long int __quad_t;
__extension__ typedef unsigned long long int __u_quad_t;
#endif

#if __WORDSIZE == 32
# define __SQUAD_TYPE           __quad_t
# define __UQUAD_TYPE           __u_quad_t
/* We want __extension__ before typedef's that use nonstandard base types
   such as `long long' in C89 mode.  */
# define __STD_TYPE             __extension__ typedef
#elif __WORDSIZE == 64
# define __SQUAD_TYPE           long int
# define __UQUAD_TYPE           unsigned long int
/* No need to mark the typedef with __extension__.   */
# define __STD_TYPE             typedef
#else
# error
#endif
#include <bits/typesizes.h>     /* Defines __*_T_TYPE macros.  */

__STD_TYPE __TIME_T_TYPE __time_t;      /* Seconds since the Epoch.  */
__STD_TYPE __SUSECONDS_T_TYPE __suseconds_t; /* Signed count of microseconds.  */

gcc标准C语言进行了扩展,但用到这些扩展功能时,编译器会提出警告,使用__extension__关键字会告诉gcc不要提出警告。

(3)__TIME_T_TYPE__SUSECONDS_T_TYPE宏定义
/usr/include/x86_64-linux-gnu/bits/typesizes.h

#ifndef _BITS_TYPES_H
# error "Never include <bits/typesizes.h> directly; use <sys/types.h> instead."
#endif

/* See <bits/types.h> for the meaning of these macros.  This file exists so
   that <bits/types.h> need not vary across different GNU platforms.  */

/* X32 kernel interface is 64-bit.  */
#if defined __x86_64__ && defined __ILP32__
# define __SYSCALL_SLONG_TYPE   __SQUAD_TYPE
# define __SYSCALL_ULONG_TYPE   __UQUAD_TYPE
#else
# define __SYSCALL_SLONG_TYPE   __SLONGWORD_TYPE
# define __SYSCALL_ULONG_TYPE   __ULONGWORD_TYPE
#endif

#define __TIME_T_TYPE           __SYSCALL_SLONG_TYPE
#define __SUSECONDS_T_TYPE      __SYSCALL_SLONG_TYPE

#error命令是C/C++语言的预处理命令之一,当预处理器预处理到#error命令时将停止编译并输出用户自定义的错误消息。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值