Desc :

 

 


Source Code :

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

void main()
{				// 0123456789
	char date[] = "2021-02-09";
	time_t now;
	struct tm t = { 0, };

	t.tm_mday = atoi(&date[8]);	// 정수로 변환하여 대입
	t.tm_mon = atoi(&date[5]) - 1;
	t.tm_year = atoi(&date[0])-1900;

	now = mktime(&t);
	printf("2021-02-09을 time_t로 변환하면 %d입니다.\n", now);
}

 


Result :

Desc :

 

 


Source Code :

#include <stdio.h>
#include <time.h>

void main()
{
	time_t now;
	struct tm t;
	char buff[100],AMPM[10];

	now = time(NULL);
	t = *localtime(&now);
	strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", &t);
	
	strftime(AMPM, sizeof(AMPM), "%p", &t);
	
	if (strcmp(AMPM, "AM"))	strcpy(AMPM, " 오후");	// 일치하면 0 --> 실행 X
	else strcpy(AMPM, " 오전");

	strcat(buff, AMPM);
	puts(buff);
}

 


Result :

Desc :

"%A" - 영문 요일 전체 표시

"%a" - 영문 요일 축약 표시

tm_wday - 요일이 0(일)~6(토)으로 저장


Source Code :

#include <stdio.h>
#include <time.h>

void main()
{
	time_t now;
	struct tm t;
	char buff[100];

	now = time(NULL);
	t = *localtime(&now);
	strftime(buff, sizeof(buff), "요일: %A", &t);
	puts(buff);
	strftime(buff, sizeof(buff), "요일: %a", &t);
	puts(buff);
	printf("%d \n", t.tm_wday);
}

 


Result :

Desc :

 

size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );

 

format에서 

%U - 일요일을 기준으로 주의 수 계산

%W - 월요일을 기준으로 주의 수 계산


Source Code :

#include <stdio.h>
#include <time.h>

void main()
{
	time_t now;
	struct tm t;
	char buff[100];

	now = time(NULL);
	t = *localtime(&now);
	strftime(buff, sizeof(buff), "올해의 경과된 주: %U주", &t);

	puts(buff);
}

 


Result :

Desc :

 

tm_yday : 1월 1일부터 며칠이 지났는지 저장됨


Source Code :

#include <stdio.h>
#include <time.h>

void main()
{
	time_t now;
	struct tm t;

	now = time(NULL);
	t = *localtime(&now);

	printf("올해의 경과일수: %d \n", t.tm_yday);
}

 


Result :

Desc :

 

 


Source Code :

#include <stdio.h>
#include <time.h>

void main()
{
	struct tm t1, t2, t3;
	int n1, n2;
	time_t n3;

	t1.tm_year = 2021 - 1900; // 1900기준
	t1.tm_mon = 2 - 1;
	t1.tm_mday = 9;
	t1.tm_hour = 1;
	t1.tm_min = 12;
	t1.tm_sec = 50;

	t2.tm_year = 2021 - 1900;
	t2.tm_mon = 2 - 1;
	t2.tm_mday = 9;
	t2.tm_hour = 3;
	t2.tm_min = 35;
	t2.tm_sec = 22;

	n1 = mktime(&t1);
	n2 = mktime(&t2);

	n3 = n2 - n1;
	t3 = *gmtime(&n3);
	t3.tm_year -= 70; // 1970기준이므로 - 70

	printf("날짜1: %4d - %02d - %02d %02d:%02d:%02d \n"
		, t1.tm_year + 1900, t1.tm_mon + 1, t1.tm_mday, t1.tm_hour, t1.tm_min, t1.tm_sec);

	printf("날짜2: %4d - %02d - %02d %02d:%02d:%02d \n"
		, t2.tm_year + 1900, t2.tm_mon + 1, t2.tm_mday, t2.tm_hour, t2.tm_min, t2.tm_sec);

	printf("날짜 차이: %4d - %02d - %02d %02d:%02d:%02d \n"
		, t3.tm_year, t3.tm_mon , t3.tm_mday, t3.tm_hour, t3.tm_min, t3.tm_sec);
}

 


Result :

 

책대로 타이핑하면 실행이 안되고, 자료형을 바꿔서 하면 결과가 이상하게,,, 나오네요,,

Desc :

시간 : 초의수 / 3600

분 : (초의 수 - 시간*3600) / 60

초 : 초의 수 % 60 


Source Code :

#include <stdio.h>
#include <time.h>

void main()
{
	struct tm t1, t2;
	int n1, n2, n3;

	t1.tm_year = 2021 - 1900;
	t1.tm_mon = 2 - 1;
	t1.tm_mday = 9;
	t1.tm_hour = 1;
	t1.tm_min = 12;
	t1.tm_sec = 50;

	t2.tm_year = 2021 - 1900;
	t2.tm_mon = 2 - 1;
	t2.tm_mday = 9;
	t2.tm_hour = 3;
	t2.tm_min = 35;
	t2.tm_sec = 22;

	n1 = mktime(&t1);
	n2 = mktime(&t2);

	n3 = n2 - n1;

	printf("시간1: %4d - %02d - %02d %02d:%02d:%02d \n"
		, t1.tm_year + 1900, t1.tm_mon + 1, t1.tm_mday, t1.tm_hour, t1.tm_min, t1.tm_sec);

	printf("시간2: %4d - %02d - %02d %02d:%02d:%02d \n"
		, t2.tm_year + 1900, t2.tm_mon + 1, t2.tm_mday, t2.tm_hour, t2.tm_min, t2.tm_sec);

	printf("시간차이: %d:%d:%d \n"
		, n3/3600,(n3-((n3/3600)*3600))/60, n3%60);
}

 


Result :

Desc :

 

clock() - 프로그램이 시작하고 나서 부터 프로세서가 소모한 시간을 0.001초 단위로 반환

 


Source Code :

#include <stdio.h>
#include <time.h>

void sleep(int sec);

void main()
{
	time_t n1, n2;

	time(&n1);
	sleep(5);
	time(&n2);

	printf("%g초가 지연되었습니다.\n", difftime(n2, n1));
}

void sleep(int sec)
{
	clock_t ct;
	ct = clock();
	while(ct + CLK_TCK * sec > clock()); 
}

 


Result :

Desc :

 

 


Source Code :

#include <stdio.h>
#include <time.h>

void main()
{
	time_t now, n1, n2;
	struct tm t1, t2;

	time(&now);	// 현재 시간
	t1 = *localtime(&now);	// 국내 시간
	t2 = *gmtime(&now); // 세계 표준 시간

	n1 = mktime(&t1); // 시간을 초로 환산
	n2 = mktime(&t2);

	printf("세계 표준 시와 대한민국의 시간 차이: %g시간 \n", difftime(n1, n2) / 3600.); // 초 차이 구하고 시간반환
}

 


Result :

Desc :

 

 


Source Code :

#include <stdio.h>
#include <time.h>

void main()
{
	struct tm chrismas = { 0,0,0,25,12-1,2021-1900}; // 날짜 초기화
	char *wday[] = { "일","월","화","수","목","금","토" };
	char buff[100];

	mktime(&chrismas); // 요일 변환됨

	strftime(buff, sizeof(buff), "2021년 12월 25일은 %A입니다", &chrismas); // 영문으로 설정

	puts(buff);
	printf("2021년 12월 25일은 %s요일입니다.\n", wday[chrismas.tm_wday]);
}

 


Result :

+ Recent posts