언제나휴일 2016. 1. 3. 16:07
반응형
size_t strftime(char *s, size_t maxsize, const char *format,  const struct tm * timeptr); 일시로 포멧 문자열을 만드는 함수
 
입력 매개 변수 리스트
s 문자열로 표현한 일시(Date Time)를 설정할 버퍼
maxsize 버퍼 크기
format 출력 포멧
timeptr 일시
 
반환 값
출력 문자 개수
 
format에 자주 사용하는 것들은 다음과 같습니다.
%a 요일을 축약
%A 요일
%b 월을 축약
%B
%c 지역에 적합한 날짜와 시간 표현
%d (01~31)
%H 시각 (00~23)
%I 시각 (01~12)
%j 1년에서 일(001~355)
%m (01~12)
%M (00~59)
%p A.M/P.M 표시
%S (00~59)
%U 년도에서 주 (00~53, 기준은 일요일)
%w 요일(0~6, 일요일은 0)
%W 년도에서 주(00~53, 기준은 월요일)
%x 지역 날짜 표현
%X 지역 시간 표현
%y 년도(00~99)
%Y 년도
 
사용 예
//C언어 표준 라이브러리 함수 사용법 가이드
//size_t strftime(char *s, size_t maxsize, const char *format,  const struct tm * timeptr); 일시로 포멧 문자열을 만드는 함수
//다양한 포멧으로 현재 시간 출력
 
#include <locale.h>
#include <time.h>
#include <stdio.h>
int main(void)
{
    time_t now;
    struct tm now_tm;
    char buf[256];
 
    setlocale(LC_ALL, "Korean");//지역을 한국으로 설정
    time(&now);
    localtime_s(&now_tm, &now);
 
    strftime(buf, sizeof(buf), "%Y %m %d %p %I:%M:%S", &now_tm);
    puts(buf);
    strftime(buf, sizeof(buf), "%x %X", &now_tm);
    puts(buf);
    strftime(buf, sizeof(buf), "%Y %m %d %H:%M:%S", &now_tm);
    puts(buf);
    strftime(buf, sizeof(buf), "%y %m %d %H:%M:%S", &now_tm);
    puts(buf);
    strftime(buf, sizeof(buf), "%y %m %d %a %H:%M:%S", &now_tm);
    puts(buf);
    strftime(buf, sizeof(buf), "%y %m %d %A %H:%M:%S", &now_tm);
    puts(buf);
 
    setlocale(LC_ALL, "US");   
    strftime(buf, sizeof(buf), "%x %X", &now_tm);
    puts(buf);
    strftime(buf, sizeof(buf), "%x %X %a", &now_tm);
    puts(buf);
    strftime(buf, sizeof(buf), "%x %X %A", &now_tm);
    puts(buf);
    return 0;
}
 
출력
2015 10 31일 오후 01:01:28
2015-10-31 오후 1:01:28
2015 10 31 13:01:28
15 10 31 13:01:28
15 10 31일 토 13:01:28
15 1031일 토요일 13:01:28
10/31/2015 1:01:28 PM
10/31/2015 1:01:28 PM Sat
10/31/2015 1:01:28 PM Saturday
반응형