Unix下的日历时间类型与函数

UNIX下的日历时间
如上图所示,Unix下的日历时间类型主要分为以下几种:

  • time_t类型,其本质上是一个长整数,表示从1970-01-01 00:00:00到目前计时时间的秒数,其值通过time_t time(time_t *tloc)获取。
  • struct timeval 和struct timespec,结构体成员除了含有一个time_t类型用于表示秒以外,还分别有一个数用来表示微秒或纳秒,其值通过int gettimeofday(struct timeval *tv, struct timezone *tz)int clock_gettime(clockid_t clk_id, struct timespec *tp)得到。
  • struct tm,结构体成员包括年月日分时秒所对应的int数值,其通常通过struct tm *localtime(const time_t *timep)转换后得到。
  • 简单年月日字符串,可以通过char *ctime(const time_t *timep)char *asctime(const struct tm *tm)转换得到。
  • 自定义格式字符串,可以通过size_t strftime(char *s, size_t max, const char *format, const struct tm *tm)转换得到。

简单代码示例:

struct timeval time;
gettimeofday(&time, NULL);
char buf[16];
struct tm *local = localtime(&ev->time.tv_sec);
buf[strftime(buf, sizeof(buf), "%H:%M:%S", local)] = '\0';//参数local表示本地时间
printf(ev->fp, "[%s.%d]", buf, time.tv_usec);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值