The Linux Programming Interface 10 Time 时间

本文介绍了Linux编程接口中关于时间的概念,如实际时间与进程时间,并详细讲解了gettimeofday()、time()、ctime()、gmtime()、localtime()、mktime()等时间函数的用法。通过示例程序展示了如何获取和处理时间,包括时间戳、本地化和进程时间。最后,讨论了时区和进程时间信息的获取。
摘要由CSDN通过智能技术生成

The Linux Programming Interface

Time

(01) 实际时间和进程时间概念

Real time: This is the time as measured either from some standard point (calendar time) or from some fixed point (typically the start) in the life of a process (elapsed or wall clock time).

Process time: This is the amount of CPU time used by a process. Measuring process time is useful for checking or optimizing the performance of a program or algorithm.

(02)gettimeofday()函数

The gettimeofday() system call returns the calendar time in the buffer pointed to by tv.

#include <sys/time.h>

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

struct timeval {

    time_t tv_sec;                 /* Seconds since 00:00:00, 1 Jan 1970 UTC */

    suseconds tv_usec;      /* Additional microseconds (long int) */

}

tz argument is now obsolete and should always be specified as NULL.

(03) time函数

The time() system call returns the number of seconds since the Epoch

#include <time.h>

time_t time(time_t *timep);

(04)时间转换函数

(05) ctime() 函数

The ctime() function provides a simple method of converting a time_t value into printable form.

char *ctime(const time_t *timep);

(06)写一个简单的程序打印当前的时间

  1 #include <stdio.h>
  2 #include <sys/time.h>
  3 #include <time.h>
  4 
  5 int main() {
  6      time_t timep;
  7      struct tm *p;
  8      time(&timep);
  9      p = localtime(&timep);
 10      printf("%d-%d-%d %d:%d:%d\n",
 11         (1900 + p->tm_year), (1 + p->tm_mon),
 12         p->tm_mday, (p->tm_hour + 12), p->tm_min, p->tm_sec);
 13     return 0;
 14 }

输出:

wang@wang:~/srccode/yunos$ ./timeprint
2017-3-9 25:48:22

(07) gmtime() 和localtime()函数

The gmtime() and localtime() functions co

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值