Linuxシステムプログラミング30のシステムデータファイルと情報-時間関数の紹介

タイムスタンプ:

 用户喜欢用字符串格式: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-
時間を秒単位で取得カーネルタイムスタンプを秒単位で取得

概要
#include <time.h>

   time_t time(time_t *tloc);

説明
time()は、エポック、1970-01-01 00:00:00 +0000(UTC)からの秒数として時間を返します。
tlocがNULL以外の場合、戻り値はtlocが指すメモリにも格納されます。

戻り
値成功すると、エポックが返されてからの秒単位の時間の値。エラーの場合、((time_t)-1)が返され、errnoが適切に設定されます。

使用法:
ここに画像の説明を挿入


名前
gmtimelocaltimemktime-日付と時刻を内訳時刻またはASCIIに変換します

概要
#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構造形式の時刻情報を通常のユーザーが見たい文字列形式に変換します。

概要
#include <time.h>

/ *
構造体conststruct tmから目的のフィールド形式を抽出し、それをサイズmaxのバッファー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).

...
...

使用法:

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/LinuxArmbiggod/article/details/106039908