Linux系统编程30 系统数据文件和信息 - 时间函数精讲

本文深入讲解了时间在计算机科学中的表示方法,包括时间戳、结构体时间格式、内核时间格式之间的转换,以及如何利用关键函数如time(), gmtime(), localtime(), mktime(), strftime()等进行时间管理与格式化。

时间戳:

 用户喜欢用字符串格式:char *
 我们,程序员喜欢用结构体表示时间信息中的各个段:struct tm
 内核,以秒为单位: time_t

关键函数

  time_t time(time_t *tloc);
  struct tm *gmtime(const time_t *timep);
  struct tm *localtime(const time_t *timep);
  time_t mktime(struct tm *tm); 注意没有const 修饰,说明 内核可能会改变 struct tm ,即对其进行修正,进位

相互转换关系如下图:

在这里插入图片描述


NAME
time - get time in seconds 获取内核时间戳,以秒为单位

SYNOPSIS
#include <time.h>

   time_t time(time_t *tloc);

DESCRIPTION
time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
If tloc is non-NULL, the return value is also stored in the memory pointed to by tloc.

RETURN VALUE
On success, the value of time in seconds since the Epoch is returned. On error, ((time_t) -1) is returned, and errno is set appropriately.

用法:
在这里插入图片描述


NAME
gmtime localtime mktime- transform date and time to broken-down time or ASCII

SYNOPSIS
#include <time.h>

	time_t * ---> struct tm *
	内核时间戳 time_t格式 转换为 程序员使用的时间格式 结构体格式 tm  
   struct tm *gmtime(const time_t *timep);
   struct tm *localtime(const time_t *timep);
   
   将 结构体格式的时间信息 转化 为内核格式时间,即以秒为单位,注意 mktime 会自动调整 tm 中的时间信息,如 当tm 中的天信息 为150 ,看着天数是溢出的,那么,mktime()会对他进行修正,即当前月天数后 进位到下一个月。其他也一样。
   time_t mktime(struct tm *tm); 注意没有const 修饰,说明 内核可能会改变 struct tm 


       struct tm {
           int tm_sec;    /* Seconds (0-60) */
           int tm_min;    /* Minutes (0-59) */
           int tm_hour;   /* Hours (0-23) */
           int tm_mday;   /* Day of the month (1-31) */
           int tm_mon;    /* Month (0-11) */
           int tm_year;   /* Year - 1900 */
           int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
           int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
           int tm_isdst;  /* Daylight saving time */
       };
   tm_sec    The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds.

   tm_min    The number of minutes after the hour, in the range 0 to 59.

   tm_hour   The number of hours past midnight, in the range 0 to 23.

   tm_mday   The day of the month, in the range 1 to 31.

   tm_mon    The number of months since January, in the range 0 to 11.

   tm_year   The number of years since 1900.

NAME
strftime - format date and time,将程序员使用的时间格式,即tm 结构体格式的时间信息转换为普通用户喜欢看到的字符串格式

SYNOPSIS
#include <time.h>

/*
在结构体 const struct tm 中提取出想要的字段format ,存放在 大小为 max 的 buffer s中
*/
size_t strftime(char *s, size_t max, const char *format,
const struct tm *tm);

const char *format:

 %a     The abbreviated name of the day of the week according to the current locale.

   %A     The full name of the day of the week according to the current locale.

   %b     The abbreviated month name according to the current locale.

   %B     The full month name according to the current locale.

   %c     The preferred date and time representation for the current locale.

   %C     The century number (year/100) as a 2-digit integer. (SU)

   %d     The day of the month as a decimal number (range 01 to 31).

...
...

用法:

在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ma浩然

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值