time.h
返回
time.h strftime
#include
#include
int main(void)
{
time_t shijian;
struct tm *ltime;
char ss[80];
time(&shijian);
ltime = localtime(&shijian);
printf("%s", asctime(ltime));
strftime(ss, 80, "[%a]", ltime); puts(ss);
strftime(ss, 80, "[%A]", ltime); puts(ss);
strftime(ss, 80, "[%b]", ltime); puts(ss);
strftime(ss, 80, "[%B]", ltime); puts(ss);
strftime(ss, 80, "[%c]", ltime); puts(ss);
strftime(ss, 80, "[%d]", ltime); puts(ss);
strftime(ss, 80, "[%H]", ltime); puts(ss);
strftime(ss, 80, "[%I]", ltime); puts(ss);
strftime(ss, 80, "[%j]", ltime); puts(ss);
strftime(ss, 80, "[%m]", ltime); puts(ss);
strftime(ss, 80, "[%M]", ltime); puts(ss);
strftime(ss, 80, "[%p]", ltime); puts(ss);
strftime(ss, 80, "[%S]", ltime); puts(ss);
strftime(ss, 80, "[%U]", ltime); puts(ss);
strftime(ss, 80, "[%w]", ltime); puts(ss);
strftime(ss, 80, "[%W]", ltime); puts(ss);
strftime(ss, 80, "[%x]", ltime); puts(ss);
strftime(ss, 80, "[%X]", ltime); puts(ss);
strftime(ss, 80, "[%y]", ltime); puts(ss);
strftime(ss, 80, "[%Y]", ltime); puts(ss);
strftime(ss, 80, "[%Z]", ltime); puts(ss);
strftime(ss, 80, "[%%]", ltime); puts(ss);
return 0;
}
time.h clock_t
#include
#include
void mywait(int msec)
{
clock_t start = clock();
while ((clock() - start ) / (CLOCKS_PER_SEC/1000.0) < msec );
return;
}
int main(void)
{
int i;
char mybuf[BUFSIZ];
printf("BUFSIZ=%d\n", BUFSIZ);
for ( i='a'; i<='j'; i++) {
putchar(i);
mywait(200);
}
setvbuf(stdout, mybuf, _IOFBF, BUFSIZ);
for (i='A'; i<='J'; i++) {
putchar(i);
mywait(100);
}
fflush(stdout);
return 0;
}
/*
运行结果
BUFSIZ=512
abcdefghijABCDEFGHIJ
*/
time.h ltime->tm_sec
#include
#include
int main(void)
{
time_t shijian;
struct tm *ltime;
time(&shijian);
ltime = localtime(&shijian);
printf("Time 1: %s", asctime(ltime));
ltime->tm_sec += 361;
ltime->tm_mday += 3;
shijian = mktime(ltime);
ltime = localtime(&shijian);
printf("Time 2: %s", asctime(ltime));
return 0;
}
/*
运行结果
Time 1: Thu Jan 15 15:27:23 2008
Time 2: Sun Jan 18 15:33:24 2008
*/
time.h difftime
#include
#include
int main(void)
{
time_t t1,t2;
int i;
for (i=1; i<=5; i++) {
time(&t1);
while(1) {
time(&t2);
if (difftime(t2,t1) >= 1.0 ) break;
}
printf("\abeep.\n");
}
return 0;
}
/*
运行结果
beep.
beep.
beep.
beep.
beep.
*/
time.h ctime
#include
#include
int main(void)
{
time_t shijian;
time(&shijian);
printf("%s",ctime(&shijian));
printf("%s",asctime(localtime(&shijian)));
return 0;
}
/*
运行结果
Mon Sep 29 17:42:36 2008
Mon Sep 29 17:42:36 2008
*/
time.h clock
#include
#include
int main(void)
{
clock_t t1, t2;
int i;
for (i=1; i<=10; i++) {
t1 = clock();
while(1) {
t2 = clock();
if ((t2-t1) / CLOCKS_PER_SEC >= 1 ) break;
}
printf("\abeep!!\n");
}
return 0;
}
/*
运行结果(一秒响一次)
beep!!
beep!!
beep!!
beep!!
beep!!
beep!!
beep!!
beep!!
beep!!
beep!!
*/
#include
#include
int main(void)
{
time_t shijian;
struct tm *ltime;
time(&shijian);
ltime = localtime(&shijian);
printf("Year =%d\n", ltime->tm_year + 1900);
printf("Month =%d\n", ltime->tm_mon + 1);
printf("Day =%d\n", ltime->tm_mday + 1);
printf("Hour =%d\n", ltime->tm_hour + 1);
printf("Minute=%d\n", ltime->tm_min + 1);
printf("Second=%d\n", ltime->tm_sec + 1);
printf("asctime->%s\n", asctime(ltime));
return 0;
}
返回