文章

C Time函数

C Time函数

时间定义

  • 毫秒

    符号ms,英语millisecond

    1 s = 1000 ms

  • 微妙

    符号μs,英语microsecond

    1 ms = 1000 us

    1 s = 1000 000 us

  • 纳秒

    符号ns,英语nanosecond

    1 us = 1000 ns

    1 ms = 1000 000 000 ns

    1 s = 1000 000 000 000 ns

  • 皮秒

    符号ps,英语picosecond

    1 ns = 1000 ps

    1 us = 1000 000 ps

    1 ms = 1000 000 000 ps

    1 s = 1000 000 000 000 000 ps

结构体及使用

timeval

在Linux系统中,有一个结构体timeval用于时间的定义

1
2
3
4
5
6
7
#include "sys/time.h"

struct timeval
{
__time_t tv_sec;            /* Seconds */
__suseconds_t tv_usec;      /* Microseconds */
};

tv_sec 表示Epoch到创建struct timeval时的秒数 tv_usec 表示微秒数 (一般最大为999999)

gettimeofday()

  • 函数原型
1
2
#include <sys/time.h>
int gettimeofday(struct timeval* tv, struct timezone* tz);

该函数功能是得到当前时间和分区,并且分别写入到tv和tz中

成功返回0,失败返回-1

tz目前已被弃置,应始终置为NULL

C语言time标准库

库变量

  • size_t: 无符号整数类型,是sizeof关键字的结果
  • clock_t: 适合存储处理器时间的类型
  • time_t: 适合存储日历时间的类型
  • struct tm: 用来保存时间和日期的结构

tm结构的定义如下:

1
2
3
4
5
6
7
8
9
10
11
struct tm {
   int tm_sec;         /* 秒,范围从 0 到 59       */
   int tm_min;         /* 分,范围从 0 到 59      */
   int tm_hour;        /* 小时,范围从 0 到 23     */
   int tm_mday;        /* 一月中的第几天,范围从 1 到 31    */
   int tm_mon;         /* 月,范围从 0 到 11      */
   int tm_year;        /* 自 1900 年起的年数      */
   int tm_wday;        /* 一周中的第几天,范围从 0 到 6 */
   int tm_yday;        /* 一年中的第几天,范围从 0 到 365   */
   int tm_isdst;       /* 夏令时               */
};

库宏

  • NULL: 空指针常量
  • CLOCK_PER_SEC: 表示每秒的处理器时钟个数

库函数

asctime()

C 库函数 char *asctime(const struct tm *timeptr) 返回一个指向字符串的指针,它代表了结构 struct timeptr 的日期和时间

1
char *asctime(const struct tm *timeptr)

参数

timeptr是指向tm结构的指针,结构如下

1
2
3
4
5
6
7
8
9
10
11
struct tm {
   int tm_sec;         /* 秒,范围从 0 到 59				*/
   int tm_min;         /* 分,范围从 0 到 59				*/
   int tm_hour;        /* 小时,范围从 0 到 23				*/
   int tm_mday;        /* 一月中的第几天,范围从 1 到 31	                */
   int tm_mon;         /* 月份,范围从 0 到 11				*/
   int tm_year;        /* 自 1900 起的年数				*/
   int tm_wday;        /* 一周中的第几天,范围从 0 到 6		        */
   int tm_yday;        /* 一年中的第几天,范围从 0 到 365	                */
   int tm_isdst;       /* 夏令时						*/	
};

返回值

该函数返回一个C字符串,包含了可读格式的日期和时间信息Www Mmm dd hh:mm:ss yyyy

其中,Www 表示星期几,Mmm 是以字母表示的月份,dd 表示一月中的第几天,hh:mm:ss 表示时间,yyyy 表示年份

clock()

C 库函数 clock_t clock(void) 返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。为了获取 CPU 所使用的秒数,您需要除以 CLOCKS_PER_SEC

在 32 位系统中,CLOCKS_PER_SEC 等于 1000000,该函数大约每 72 分钟会返回相同的值。

1
clock_t clock(void)

返回值

该函数返回自程序启动起,处理器时钟所使用的时间,如失败,则返回-1

ctime()

C 库函数 char *ctime(const time_t *timer) 返回一个表示当地时间的字符串,当地时间是基于参数 timer

返回的字符串格式如下: Www Mmm dd hh:mm:ss yyyy 其中,Www 表示星期几,Mmm 是以字母表示的月份,dd 表示一月中的第几天,hh:mm:ss 表示时间,yyyy 表示年份

1
char *ctime(const time_t *timer)

参数

  • timer: 指向time_t对象的指针,该对象包含了一个日历时间

返回值

该函数返回一个C字符串,该字符串包含了可读格式的日期和时间信息

用法示例

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <time.h>

int main ()
{
   time_t curtime;

   time(&curtime);

   printf("当前时间 = %s", ctime(&curtime));

   return(0);
}

difftime()

C 库函数 double difftime(time_t time1, time_t time2) 返回 time1 之间相差的秒数 (time1 - time2)。这两个时间是在日历时间中指定的,表示了自纪元 Epoch(协调世界时 UTC:1970-01-01 00:00:00)起经过的时间。

1
double difftime(time_t time1, time_t time2)

参数

  • time1: 表示结束时间的time_t对象
  • time2: 表示开始时间的time_t对象

返回值

返回以双精度浮点型double值表示的两个时间之间相差的秒数(time2-time1)

gmtime()

C 库函数 struct tm *gmtime(const time_t *timer) 使用 timer 的值来填充 tm 结构,并用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。

1
struct tm *gmtime(const time_t *timer)

参数

  • timer: 指向日历时间的time_t值的指针

返回值

返回指向tm结构的指针

1
2
3
4
5
6
7
8
9
10
11
struct tm {
   int tm_sec;         /* 秒,范围从 0 到 59				*/
   int tm_min;         /* 分,范围从 0 到 59				*/
   int tm_hour;        /* 小时,范围从 0 到 23				*/
   int tm_mday;        /* 一月中的第几天,范围从 1 到 31	                */
   int tm_mon;         /* 月份,范围从 0 到 11				*/
   int tm_year;        /* 自 1900 起的年数				*/
   int tm_wday;        /* 一周中的第几天,范围从 0 到 6		        */
   int tm_yday;        /* 一年中的第几天,范围从 0 到 365	                */
   int tm_isdst;       /* 夏令时						*/	
};

localtime()

C 库函数 struct tm *localtime(const time_t *timer) 使用 timer 的值来填充 tm 结构。timer 的值被分解为 tm 结构,并用本地时区表示

1
struct tm *localtime(const time_t *timer)

参数

  • timer: 指向表示日历时间的time_t值的指针

返回值

返回指向tm结构的指针

1
2
3
4
5
6
7
8
9
10
11
struct tm {
   int tm_sec;         /* 秒,范围从 0 到 59				*/
   int tm_min;         /* 分,范围从 0 到 59				*/
   int tm_hour;        /* 小时,范围从 0 到 23				*/
   int tm_mday;        /* 一月中的第几天,范围从 1 到 31	                */
   int tm_mon;         /* 月份,范围从 0 到 11				*/
   int tm_year;        /* 自 1900 起的年数				*/
   int tm_wday;        /* 一周中的第几天,范围从 0 到 6		        */
   int tm_yday;        /* 一年中的第几天,范围从 0 到 365	                */
   int tm_isdst;       /* 夏令时						*/	
};

mktime()

C 库函数 time_t mktime(struct tm *timeptr)timeptr 所指向的结构转换为一个依据本地时区的 time_t 值。

1
time_t mktime(struct tm *timeptr)

参数

  • timeptr: 指向表示日历时间的time_t值的指针

返回值

返回一个time_t值,该值对应于以参数传递的日历时间,如果发生错误则返回-1

strftime()

C 库函数 size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) 根据 format 中定义的格式化规则,格式化结构 timeptr 表示的时间,并把它存储在 str 中。

1
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)

参数

  • str: 指向目标数组的指针,用于复制产生的C字符串
  • maxsize: 被复制到str的最大字符数
  • format: 这是C字符串,包含了普通字符和特殊格式说明符的任何组合。这些格式说明符由函数替换为表示tm中所指定时间的相对应值
说明符替换为实例
%a缩写的星期几名称Sun
%A完整的星期几名称Sunday
%b缩写的月份名称Mar
%B完整的月份名称March
%c日期和时间表示法Sun Aug 19 02:56:02 2012
%d一月中的第几天(01-3119
%H24 小时格式的小时(00-23)14
%I12 小时格式的小时(01-12)05
%j一年中的第几天(001-366)231
%m十进制数表示的月份(01-12)08
%M分(00-59)55
%pAM 或 PM 名称PM
%S秒(00-61)02
%U一年中的第几周,以第一个星期日作为
第一周的第一天(00-53)
33
%w十进制数表示的星期几,星期日表示为 0(0-6)4
%W一年中的第几周,以第一个星期一作为
第一周的第一天(00-53)
34
%x日期表示法08/19/12
%X时间表示法02:50:06
%y年份,最后两个数字(00-99)01
%Y年份2012
%Z时区的名称或缩写CDT
%%一个 % 符号%
  • timeptr: 指向tm结构的指针
1
2
3
4
5
6
7
8
9
10
11
struct tm {
   int tm_sec;         /* 秒,范围从 0 到 59				*/
   int tm_min;         /* 分,范围从 0 到 59				*/
   int tm_hour;        /* 小时,范围从 0 到 23				*/
   int tm_mday;        /* 一月中的第几天,范围从 1 到 31	                */
   int tm_mon;         /* 月份,范围从 0 到 11				*/
   int tm_year;        /* 自 1900 起的年数				*/
   int tm_wday;        /* 一周中的第几天,范围从 0 到 6		        */
   int tm_yday;        /* 一年中的第几天,范围从 0 到 365	                */
   int tm_isdst;       /* 夏令时						*/	
};

返回值

如果产生的 C 字符串小于 size 个字符(包括空结束字符),则会返回复制到 str 中的字符总数(不包括空结束字符),否则返回零。

运行实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <time.h>

int main ()
{
   time_t rawtime;
   struct tm *info;
   char buffer[80];

   time( &rawtime );

   info = localtime( &rawtime );

   strftime(buffer,80,"%x - %I:%M%p", info);
   printf("格式化的日期 & 时间 : |%s|\n", buffer );
  
   return(0);
}

运行结果:

1
格式化的日期 & 时间 : |08/23/12 - 12:40AM|

time()

C 库函数 time_t time(time_t *seconds) 返回自纪元 Epoch(1970-01-01 00:00:00 UTC)起经过的时间,以秒为单位。如果 seconds 不为空,则返回值也存储在变量 seconds 中。

1
time_t time(time_t *t)

参数

  • seconds: 指向类型为time_t的对象的指针,用于存储seconds的值

返回值

以time_t对象返回当前日历时间

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t seconds;

  seconds = time(NULL);
  printf("自 1970-01-01 起的小时数 = %ld\n", seconds/3600);
  
  return(0);
}
本文由作者按照 CC BY 4.0 进行授权