首页 > 其他分享 >ctime(ctime头文件的作用)

ctime(ctime头文件的作用)

时间:2022-10-24 20:34:15浏览次数:44  
标签:头文件 ctime asctime tm time timeptr include 作用


C语言 ctime()

没有 #include 的写法,只有 #include ,time.h 是C语言里时间的库函数。

ctime在C语言里,只是一个把日期和时间转换为字符串的函数。具体函数原型为:

char *ctime( const time_t *timer )

用法实例:

#include

#include

int main( void )

time_t ltime;

time(

printf( "The time is %s\n", ctime(

return 0;

假如当前的系统时间是2011年1月19日,15时16分20秒,星期三,那么经过这段程序运行后,在显示终端上出现:The time is Wed Jan 19 15:16:20 2011asctime是把时间换成ascii码。

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

具体介绍如下:

函数名: asctime

功 能: 转换日期和时间为ASCII码

用 法: char *asctime(const struct tm *tblock);

程序例:

#include

#include

#include

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

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

#include

void main(void)

struct tm *newtime;

time_t ltime;

/* Get the time in seconds */

time (

/* convert it to the structure tm */

newtime = localtime(

/* 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;

求采纳为满意回答。函数: ctime

功 能: 把日期和时间转换为字符串

用 法: char *ctime(const time_t *time);

#include

#include

intmain(void)

time_tt;

t=time(&t);

printf("today'sdateandtime:%s\n",ctime(&t));

return0;

注:若在linux下使用本函数,需要include 头文件就是显示当前的时间。 怎么显示忘了 好像能赋值给一个变量把,

ctime是标准c++的类么

CTime是mfc的类。要使用的话生成控制台的时候选支持mfc就可以直接用了不用包含文件。

没选的话包含alttime.h

或者,你也可以用SYSTEMTIME类型,不需要mfc支持。

SYSTEMTIMECurTime;

GetLocalTime(&CurTime);//获得系统当前时间

///

例子上面不是给了么?最后两行。

你的出错问题是没有 这个文件,因为CTime的定义是在alttime.h里的,要包含的话包含这个文件。ctime::getcurrenttime()不是标准c++中的,ctime只是mfc(微软基础类库)中的一个时间类而已。好像是

出错估计是用发问题,或者头文件丢失

标签:头文件,ctime,asctime,tm,time,timeptr,include,作用
From: https://blog.51cto.com/yetaotao/5791216

相关文章