asctime

王朝百科·作者佚名  2010-01-15
窄屏简体版  字體: |||超大  

函数名: 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

", str);

return 0;

}

英文详解:

asctimeSynopsis

#include <time.h>

char *asctime(const struct tm *timeptr);

Description

Theasctimefunction converts the broken-down time in the structure pointed to bytimeptrinto a string in the form

Sun Sep 16 01:03:52 1973

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

",

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

Theasctimefunction 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 (&ltime);

/* convert it to the structure tm */

newtime = localtime(&ltime);

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航