#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <conio.h>
void sleep( clock_t wait )
int show_time()
{
char tmpbuf[128], ampm[] = "AM"
__time64_t ltime
struct __timeb64 tstruct
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 }
char ch
_tzset()
while(1)
{
if(_kbhit())
{
ch=getchar()
if(ch=='q') return 1
}
_strtime( tmpbuf )
printf( "time:\t\t%s\r", tmpbuf )
sleep( (clock_t)1 * CLOCKS_PER_SEC )
}
return 0
}
void sleep( clock_t wait )
{
clock_t goal
goal = wait + clock()
while( goal >clock() )
}
int main(int argc,char * argv[])
{
show_time()
return 0
}
程序执行时,按q结束。
共勉,我也在学习阶段。
#include <time.h>
#include <stdio.h>
int main(void) {
//nano time
struct timespec ts1,ts2,ts3,ts4,ts5
int err
err = clock_gettime(CLOCK_REALTIME,&ts1)
if(err < 0) {
return 1
}
printf("ts1 nano:%di\n",ts1.tv_nsec)
err = clock_gettime(CLOCK_REALTIME,&ts2)
if(err < 0) {
return 1
}
printf("ts2 nano:%d\n",ts2.tv_nsec)
printf("nano:ts2-ts1=%d\n",ts2.tv_nsec-ts1.tv_nsec)
err = clock_gettime(CLOCK_MONOTONIC,&ts3)
if(err < 0) {
return 1
}
printf("system started time nano:%d\n",ts3.tv_nsec)
err = clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&ts4)
if(err < 0) {
return 1
}
printf("process cpu time nano:%d\n",ts4.tv_nsec)
err = clock_gettime(CLOCK_THREAD_CPUTIME_ID,&ts5)
if(err < 0) {
return 1
}
printf("thread cup time nano:%d\n",ts5.tv_nsec)
return 0
}
#include<stdio.h>#include<windows.h>
int main()
{
int hour = 0, min = 0, sec = 0
while (1){
Sleep(1000)//暂停1s
system("cls")//清屏
sec++
if (sec == 60){
min++
sec = 0
}
if (min == 60){
hour++
min = 0
}
if (hour == 24){
hour = 0
}
printf("%02d:%02d:%02d\n", hour, min, sec)
//%02d输出长度为2,不足2前面补0
}
return 0
}
运行截图