C로 현재 시간 가져 오기
내 시스템의 현재 시간을 알고 싶습니다. 이를 위해 C에서 다음 코드를 사용하고 있습니다.
time_t now;
struct tm *mytime = localtime(&now);
if ( strftime(buffer, sizeof buffer, "%X", mytime) )
{
printf("time1 = \"%s\"\n", buffer);
}
문제는이 코드가 임의의 시간을 제공한다는 것입니다. 또한 임의의 시간은 매번 다릅니다. 내 시스템의 현재 시간을 원합니다.
여기 에서 복사 붙여 넣기 :
/* localtime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
(C에서 작동하려면 main () 인수 목록에 "void"를 추가하십시오.)
now
변수를 초기화하십시오 .
time_t now = time(0); // Get the system time
localtime
함수는 전달에 시간 값을 변환하는 데 사용됩니다 time_t
A를을 struct tm
, 실제로 시스템 시간을 검색하지 않습니다.
쉬운 방법:
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t mytime = time(NULL);
char * time_str = ctime(&mytime);
time_str[strlen(time_str)-1] = '\0';
printf("Current Time : %s\n", time_str);
return 0;
}
위의 @mingos에서 답을 확장하기 위해 내 시간을 특정 형식 ([dd mm yyyy hh : mm : ss])으로 형식화하는 아래 함수를 작성했습니다.
// Store the formatted string of time in the output
void format_time(char *output){
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
sprintf(output, "[%d %d %d %d:%d:%d]",timeinfo->tm_mday, timeinfo->tm_mon + 1, timeinfo->tm_year + 1900, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
}
자세한 내용은 여기에서struct tm
확인할 수 있습니다 .
#include<stdio.h>
#include<time.h>
void main()
{
time_t t;
time(&t);
printf("\n current time is : %s",ctime(&t));
}
guys you can use this function to get current local time. if you want gmtime then use gmtime function instead of localtime. cheers
time_t my_time;
struct tm * timeinfo;
time (&my_time);
timeinfo = localtime (&my_time);
CCLog("year->%d",timeinfo->tm_year+1900);
CCLog("month->%d",timeinfo->tm_mon+1);
CCLog("date->%d",timeinfo->tm_mday);
CCLog("hour->%d",timeinfo->tm_hour);
CCLog("minutes->%d",timeinfo->tm_min);
CCLog("seconds->%d",timeinfo->tm_sec);
If you just need the time without the date.
time_t rawtime;
struct tm * timeinfo;
time( &rawtime );
timeinfo = localtime( &rawtime );
printf("%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min,
timeinfo->tm_sec);
#include <stdio.h>
#include <time.h>
void main()
{
time_t t;
time(&t);
clrscr();
printf("Today's date and time : %s",ctime(&t));
getch();
}
guys i got a new way get system time. though its lengthy and is full of silly works but in this way you can get system time in integer format.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char hc1,hc2,mc1,mc2;
int hi1,hi2,mi1,mi2,hour,minute;
system("echo %time% >time.txt");
fp=fopen("time.txt","r");
if(fp==NULL)
exit(1) ;
hc1=fgetc(fp);
hc2=fgetc(fp);
fgetc(fp);
mc1=fgetc(fp);
mc2=fgetc(fp);
fclose(fp);
remove("time.txt");
hi1=hc1;
hi2=hc2;
mi1=mc1;
mi2=mc2;
hi1-=48;
hi2-=48;
mi1-=48;
mi2-=48;
hour=hi1*10+hi2;
minute=mi1*10+mi2;
printf("Current time is %d:%d\n",hour,minute);
return 0;
}
참고URL : https://stackoverflow.com/questions/5141960/get-the-current-time-in-c
'IT story' 카테고리의 다른 글
GUI를 통해 TFS에서 다른 사용자의 체크 아웃을 실행 취소하는 방법은 무엇입니까? (0) | 2020.09.06 |
---|---|
jQuery는 POST 매개 변수로 문자열을 보냅니다. (0) | 2020.09.06 |
CoffeeScript 정의되지 않음 (0) | 2020.09.06 |
모든 Rails 도우미를 모든 뷰에서 항상 사용할 수있는 이유는 무엇입니까? (0) | 2020.09.06 |
Apache Kafka 대 Apache Storm (0) | 2020.09.06 |