您的位置:首页 > 编程语言 > C语言/C++

C++如何使用时间函数(1)

2012-11-24 14:31 453 查看
#include <stdio.h>

#include <time.h>

int main(void)

{

const char *Day[7] = {

"Sunday" , "Monday", "Tuesday", "Wednesday",

"Thursday", "Friday", "Saturday"

};

const char *Month[12] = {

"January", "February", "March", "April",

"May", "June", "July", "August",

"September", "October", "November", "December"

};

const char *Suffix[4] = { "st", "nd", "rd", "th" };

enum sufindex { st, nd, rd, th } sufsel = th; /* Suffix selector */

struct tm *OurT = NULL; /* Pointer for the time structure */

time_t Tval = 0; /* Calendar time */

Tval = time(NULL); /* Get calendar time */

OurT = localtime(&Tval); /* Generate time structure */

switch(OurT->tm_mday)

{

case 1: case 21: case 31:

sufsel= st;

break;

case 2: case 22:

sufsel= nd;

break;

case 3: case 23:

sufsel= rd;

break;

default:

sufsel= th;

break;

}

printf("Today is %s the %d%s %s %d", Day[OurT->tm_wday],

OurT->tm_mday, Suffix[sufsel], Month[OurT->tm_mon], 1900 + OurT->tm_year);

printf("\nThe time is %d : %d : %d",

OurT->tm_hour, OurT->tm_min, OurT->tm_sec );

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: