当前位置: C语言 -- 附录 -- ctime_s

ctime_s函数


概要:
#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
errno_t ctime_s(char *s, rsize_t maxsize, 
      const time_t *timer);

描述:

该函数将参数timer指向的日历时间转换为字符串形式的本地时间。

该函数等价于asctime_s(s, maxsize, localtime_s(timer, &(struct tm){0}))


运行约束:

参数s和参数timer不能是空指针。参数maxsize应不小于26,且不大于宏RSIZE_MAX

在存在运行约束冲突的情况下,如果参数s不是空指针,参数maxsize不等于0且不大于宏RSIZE_MAX,函数将s[0]设置为空字符。


参数:
char *s

指向char类型数组的指针。

rsize_t maxsize

参数s指向数组的最小数组长度。

const time_t *timer

指向time_t类型对象的指针。


返回值:

如果成功转换并存入参数s指向的数组,函数返回0;否则函数返回非0值。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
/*安全函数ctime_s范例*/

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <time.h>

#define LENGTH 26

int main(void)
{
    time_t currentTime;
    char str[LENGTH];

    time(&currentTime);
    if(!ctime_s(str, LENGTH, &currentTime))
        puts(str);

    return 0;
}


输出:

Wed Jun 22 07:48:35 2022

注:使用Visual Studio编译。


相关内容:
ctime time_t类型对象转换为字符串的函数。