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”+“\0\n”,共26个字符。

2 参数

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

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

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

ctime()库描述如下:

1. A C-string containing the date and time information in a human-readable format.
2. 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);
   // 将时间转换为本地时间结构
   current_time_tm = localtime(&current_time);
   // 将time_t时间转换为字符串
   time_string = ctime(&current_time);
   // 输出结果   
   printf("ctime()  函数返回值 = %p\n", time_string);
   // 将struct tm时间转换为字符串
   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_t时间转换为字符串
   time_string = ctime(&current_time);
   // 输出结果
   printf("当前时间 : %s", time_string);
   // 将struct tm时间转换为字符串
   time_string = asctime(current_time_tm);
   // 输出结果
   printf("当前时间 : %s", time_string);
   //
   return 0;
}

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

在这里插入图片描述

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);
   // 将struct tm时间转换为字符串
   time_string = asctime(current_time_tm);
   // 拷贝字符串
   strcpy(saved_time_string, time_string);
   // 输出结果
   printf("存储在静态字符串中的当前时间 : %s", time_string);
   printf("存储在字符数组中的当前时间   : %s", saved_time_string);
   //
   return 0;
}

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

在这里插入图片描述

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中提到,python的time库中有多个函数,包括time.asctime()函数。这个函数接受一个时间元组作为参数,可以是通过函数gmtime()或localtime()返回的时间值。它将返回一个可读的形式为'Sun Jun 20 23:21:05 1993'的字符串。如果未提供时间元组参数,则函数将使用当前时间作为默认值。需要注意的是,asctime()函数不会添加尾随的换行符。中也提到了time.ctime()函数,它将以秒表示的时间转换为表示本地时间的字符串。如果未提供参数或者参数为None,它将默认使用time.time()作为参数,相当于asctime(localtime(secs))的效果。因此,python asctime函数的参数可以是时间元组或者秒数。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [Python中常用的Python time模块常用函数](https://download.csdn.net/download/weixin_38551938/13741745)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Python标准库函数time的使用](https://blog.csdn.net/weixin_44690846/article/details/88987989)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值