40 - asctime()函数

1 函数原型

asctime():时间类型转换,函数原型如下:

char* asctime (const struct tm * timeptr);

ctime()库描述如下:

Convert tm structure to string
1. Interprets the contents of the tm structure pointed by timeptr as a calendar time and converts it to a C-string containing a human-readable version of the corresponding date and time.
2. The returned string has the following format:
    Www Mmm dd hh:mm:ss yyyy
3. Where Www is the weekday, Mmm the month (in letters), dd the day of the month, hh:mm:ss the time, and yyyy the year.
4. The string is followed by a new-line character ('\n') and terminated with a null-character.
5. For an alternative with custom date formatting, see strftime.
  1. asctime()函数:
    (1)用于将struct tm类型的时间结构转换为可读的字符串;
    (2)字符串包含表示时间的日期和时间信息,格式是固定的:“Www Mmm dd hh:mm:ss yyyy”+“\n\0”,共26个字符,各部分含义见 39 - ctime()函数

2 参数

ctime()函数只有一个参数timeptr:

  1. 参数timeptr是一个指向struct tm类型时间结构的指针,类型为struct tm*;timeptr指向的时间结构包含要转换的日期和时间信息。

ctime库描述如下:

timeptr
1. Pointer to a tm structure that contains a calendar time broken down into its components (see struct tm).

3 返回值

asctime()函数的返回值类型为char*型:

  1. 转换成功,返回一个指向静态字符串的指针,该字符串包含表示时间的日期和时间信息。

特别注意

  1. 当asctime()函数以localtime()函数返回值为参数时,字符串包含表示本地时间的日期和时间信息;
  2. 当asctime()函数以gmtime()函数返回值为参数时,字符串包含表示UTC时间的日期和时间信息。

特别说明

  1. 静态字符串是指字符串的存储空间在程序开始时分配,并在程序结束时释放,在程序运行期间其生命周期是静态的;
  2. 同一程序中所有的ctime()函数和asctime()函数共享同一个静态字符串,每次调用这两个函数时,都会覆盖这个静态字符串中的内容;
  3. 如果要保存静态字符串中的内容,必须在程序中单独声明一个字符数组,在每次调用ctime()函数或asctime()函数后,将静态字符串中的内容拷贝至新的字符数组中。

ctime()库描述如下:

7. A C-string containing the date and time information in a human-readable format.
8. The returned value points to an internal array whose validity or value may be altered by any subsequent call to asctime or ctime.

4 示例

4.1 示例1

示例代码如下所示:

int main() {
   //
   time_t current_time = 0;
   char* time_string = NULL;
   struct tm* current_time_tm = NULL;
   // 获取当前时间
   current_time = time(NULL);
   // 将时间值转换为字符串
   time_string = ctime(&current_time);
   //  
   printf("ctime()  函数返回值 = %p\n", time_string);
   // 将时间值转换为时间结构
   current_time_tm = localtime(&current_time);
   // 将时间结构转换为字符串
   time_string = asctime(current_time_tm);
   //
   printf("asctime()函数返回值 = %p\n", time_string);
   //
   return 0;
}

代码运行结果如下图所示:

在这里插入图片描述

代码及运行结果分析如下:

  1. ctime()函数和asctime()函数的返回值是指针,两指针值相同说明指向的是同一片内存空间,即两函数共享同一个静态字符串。

4.2 示例2

示例代码如下所示:

int main() {
   //
   time_t current_time = 0;
   char* time_string = NULL;
   struct tm* current_time_tm = NULL;
   // 获取当前时间
   current_time = time(NULL);
   // 将时间值转换为时间结构
   current_time_tm = localtime(&current_time);
   // 将时间结构转换为字符串
   time_string = asctime(current_time_tm);
   // 打印当前时间
   printf("当前本地时间 : %s", time_string);
   // 将时间值转换为时间结构
   current_time_tm = gmtime(&current_time);
   // 将时间结构转换为字符串
   time_string = asctime(current_time_tm);
   // 打印当前时间
   printf("当前UTC 时间 : %s", time_string);
   //
   return 0;
}

代码运行结果如下图所示:

在这里插入图片描述

代码及运行结果分析如下:

  1. asctime()函数参数不同,字符串包含的时间信息不同。

4.3 示例3

示例代码如下所示:

int main() {
   //
   time_t current_time = 0;
   char* time_string = NULL;
   char saved_time_string[26] = { 0 };
   struct tm* current_time_tm = NULL;
   // 获取当前时间
   current_time = time(NULL);
   // 将时间值转换为时间结构
   current_time_tm = localtime(&current_time);
   // 将时间结构转换为字符串
   time_string = asctime(current_time_tm);
   // 拷贝字符串
   strcpy(saved_time_string, time_string);
   // 打印当前时间
   printf("存储在静态字符串中的当前时间 : %s", time_string);
   printf("存储在字符数组  中的当前时间 : %s", saved_time_string);
   //
   return 0;
}

代码运行结果如下图所示:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值