컴퓨터/프로그래밍
C언어 strftime 함수 날짜와 시간 문자열 구하기
2020. 3. 14. 06:39
C strftime() 날짜와 시간 문자열 구하기
struct tm 값으로 포맷에 맞춘 시간 문자열을 구합니다. struct tm에 대한 구조를 보시려면 localtime()을 참고하여 주십시오.
- 헤더: time.h
- 형태: size_t strftime(char *restrict s, size_t maxsize, const char *restrict format,
const struct tm *restrict timeptr)
- 인수: char *restrict s 문자열을 받을 버퍼 포인터
size_t maxsize 버퍼의 크기
const char *restrict format 날짜와 시간 정보를 나열하기 위한 문자열 포맷
const struct tm *restrict timeptr 날짜와 시간 정보
- 반환: size_t 문자열의 길이
*restrict format에 사용하는 변환 코드 표
변환 코드 |
의미 |
변환 코드 |
의미 |
%a |
요일 이름의 약자 |
%M |
분(00-59) |
%A |
요일 이름 |
%p |
AM 또는 PM |
%b |
월 이름의 약자 |
%S |
초(00-59) |
%B |
월 이름 |
%w |
요일(0-6) |
%c |
지역 날짜와 시간 |
%x |
지역 날짜 |
%d |
날짜( 01-31) |
%X |
지역 시간 |
%H |
시간(00-23) |
%y |
연도(00-99) |
%l(엘) |
시간(01-12) |
%Y |
연도(예, 2003 |
%j |
1월 1일 이후의 날짜(001-366) |
%% |
퍼센트 기호(%) |
%m |
월(01-12) |
|
|
C언어 strftime() 함수 예제
#include <stdio.h>
#include <time.h>
#define BUFF_SIZE 1204
int main( void)
{
time_t tm_time;
struct tm *st_time;
char buff[BUFF_SIZE];
time( &tm_time);
st_time = localtime( &tm_time);
strftime( buff, BUFF_SIZE, "%Y년 %m월 %d일 - %l시 %M분 %S초 %p\n", st_time);
puts( buff);
return 0;
}
C언어 strftime() 예제 실행 결과
]$ ./a.out
2020년 03월 16일 - 12시 09분 01초 PM
]$