读书人

怎样在C语言中显示时间,该如何解决

发布时间: 2012-02-27 10:00:22 作者: rapoo

怎样在C语言中显示时间
怎样在C语言中显示时间

[解决办法]
#include <time.h>

time_t sec = time(NULL);
struct tm t = *localtime(&sec);

printf( "%02d:%02d:%02d: \n ", t.tm_hour, t.tm_min, t.tm_sec);



[解决办法]
使用time就可以了饿


#include <stdio.h> ;
#include <stdlib.h> ;
#include <time.h> ;
#include <string.h> ;

#define DEBUG
#define CONVERT_MOUNTH 1


void arrange_str(char *str_time, char *realtime, int length){
int i = 0;
int j = 0;
int off = 0;
int add_count[6] = { 20, 4, 8, 11, 14, 17 };
int operation[6] = { 4, 3, 2, 2, 2, 2 };
char *mouth_ch[12] = { "Jun ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Agu ", "Sep ", "Oct ", "Nov ", "Dec "};
char *mount_digit[12] = { "01 ", "02 ", "03 ", "04 ", "05 ", "06 ", "07 ", "08 ", "09 ", "10 ", "11 ", "12 " };

for( ; i < 6; i++ ){
if( CONVERT_MOUNTH == 1 && i == 1 ){
for( ; j < 12; j++ )
if( strncmp( mouth_ch[j], str_time + add_count[i], operation[i] ) == 0 )
strcpy( realtime+off, mount_digit[j]);
off += 2;
}else{
strncpy( realtime+off, str_time + add_count[i], operation[i]);
off += operation[i];
}
}
*(realtime+14) = '\0 ';
}

main(){
int i = 0;
time_t tmp;
struct tm *ptr;
char realtime[16];
char *str_time;

tmp = time(NULL);
ptr = localtime(&tmp);
str_time = asctime(ptr);

#ifdef DEBUG
printf( "begin\n ");
for( i = 0; i < (int)strlen(asctime(ptr)); i++ )
printf( "%c\n ", *(str_time+i));
printf( "end\n ");
printf(str_time);
printf( "%d\n ", strlen(asctime(ptr)));
#endif
arrange_str( str_time, realtime, strlen(asctime(ptr)) );
printf( "%s\n ",realtime);
}
[解决办法]
#include <time.h>
#include <stdio.h>

main()
{
struct tm *tm_ptr;
time_t the_time;

(void) time(&the_time);
tm_ptr=localtime(&the_time);

printf( "Raw time is %ldn ",the_time);
printf( "Mytime show:\n ");
printf( "Date:%02d/%02d/%02d\n ",
tm_ptr-> tm_year,tm_ptr-> tm_mon+1,tm_ptr-> tm_mday);
printf( "Time:%02d/%02d/%02d\n ",
tm_ptr-> tm_hour,tm_ptr-> tm_min,tm_ptr-> tm_sec);
exit(0);
}
[解决办法]
#include "stdio.h "
#include "stdlib.h "
#include "time.h "
int main()
{
time_t lt; /*define a longint time varible*/
lt=time(NULL);/*system time and date*/
printf(ctime(&lt)); /*english format output*/
printf(asctime(localtime(&lt)));/*tranfer to tm*/
printf(asctime(gmtime(&lt))); /*tranfer to Greenwich time*/
system( "pause ");
}


[解决办法]
#include <time.h>
#include <stdio.h>

void main( void )
{
struct tm *newtime;
long ltime;

time( &ltime );

/* Obtain coordinated universal time: */
newtime = gmtime( &ltime );
printf( "Coordinated universal time is %s\n ",
asctime( newtime ) );
}

读书人网 >C语言

热点推荐