asctime与ctime的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asctime与ctime的区别相关的知识,希望对你有一定的参考价值。

时间函数,谢谢了

asctime是把时间换成ascii码。
ctime是把时间转换成字符串。

具体介绍如下:

函数名: asctime
功 能: 转换日期和时间为ASCII码
用 法: char *asctime(const struct tm *tblock);
程序例:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(void)

struct tm t;
char str[80];
/* sample loading of tm structure */
t.tm_sec = 1; /* Seconds */
t.tm_min = 30; /* Minutes */
t.tm_hour = 9; /* Hour */
t.tm_mday = 22; /* Day of the Month */
t.tm_mon = 11; /* Month */
t.tm_year = 56; /* Year - does not include century */
t.tm_wday = 4; /* Day of the week */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */
/* converts structure to null terminated
string */
strcpy(str, asctime(&t));
printf("%s\n", str);
return 0;

英文详解:
[编辑本段]asctime

Synopsis
#include <time.h>
char *asctime(const struct tm *timeptr);
Description
The asctime function converts the broken-down time in the structure pointed to by timeptr into a string in the form
Sun Sep 16 01:03:52 1973\n\0
using the equivalent of the following algorithm.
char *asctime(const struct tm *timeptr)

static const char wday_name[7][3] =
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
;
static const char mon_name[12][3] =
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
;
static char result[26];
sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
wday_name[timeptr->tm_wday],
mon_name[timeptr->tm_mon],
timeptr->tm_mday, timeptr->tm_hour,
timeptr->tm_min, timeptr->tm_sec,
1900 + timeptr->tm_year);
return result;

Returns
The asctime function returns a pointer to the string.
Example :
#include <stdio.h>
#include <time.h>
void main(void)

struct tm *newtime;
time_t ltime;
/* Get the time in seconds */
time (<ime);
/* convert it to the structure tm */
newtime = localtime(<ime);
/* print the local time as a string */
printf("The current time and dat are %s", asctime(newtime));

The current time and dat are Mon Dec 28 12:33:50 1998

ctime
函数名: ctime
功 能: 把日期和时间转换为字符串
用 法: char *ctime(const time_t *time);
程序例:
#include
#include
int main(void)

time_t t;
time(&t);
printf("Today's date and time: %s\n", ctime(&t));
return 0;
参考技术A 两者的功能都是将时间转换为本地时间的字符串,区别在于两个函数的参数格式不同。 参考技术B 需要传递的参数不同:
asctime([tuple]) --> string
ctime(seconds) -> string

以上是关于asctime与ctime的区别的主要内容,如果未能解决你的问题,请参考以下文章

mtime/ctime/atime三个时间解析与区别总结文档

C语言如何返回格式化日期时间?(将日期和时间以字符串格式输出)ctime()asctime()localtime()

ctime time.h 区别

ctime和mtime和atime的区别

简单的ctime,atime和mtime区别说明

Linux中ctime mtime atime文件时间的区别