2009-07-28 3 views

답변

0

OS에 따라 다르겠습니까? Windows 또는 여기에 나온 POSIX를 사용하고 있습니까?

+0

내가 그것을 날짜의 방식을 윈도우 스토어에 근본적인 차이의 나머지는 –

+0

수 있습니다로는 표준되고 싶어 ... 그것은 가능한 한 표준되고 싶어 세계. 운영 체제 설명서에서만 대답하는 것 중 하나입니다. http://en.wikipedia.org/wiki/System_date – ewanm89

+0

POSIX 호환 시스템 용이라고 가정 해 보겠습니다. –

6

localtime 기능을 사용하십시오. 값을 0에서 numeric_limits<time_t>::max()으로 전달합니다. 허용되지 않는 값의 경우이 함수는 널 포인터를 리턴합니다. 바이너리 검색 알고리즘을 사용하여 적절한 값을보다 빨리 찾을 수 있습니다. O (로그 N) 여기서 N은 numeric_limits<time_t>::max()입니다.

다음 샘플에서는 부스트 라이브러리를 사용하지만 플랫폼에 독립적입니다. 필요한 경우 STL 또는 부스트없이 구현할 수 있습니다.

#include <iostream> 
#include <time.h> 
#include <limits> 
#include <algorithm> 
#include <boost/iterator/counting_iterator.hpp> 

using namespace std; 
using namespace boost; 

bool less_time(time_t val1, time_t val2) 
{ 
    tm* v1 = localtime(&val1); 
    tm* v2 = localtime(&val2); 
    if (v1 && v2) return false; 
    if (!v1 && !v2) return false; 
    if (v1 && !v2) return true; 
    return false; 
}; 

int main() { 
    counting_iterator<time_t> x = upper_bound(counting_iterator<time_t>(0), counting_iterator<time_t>(numeric_limits<time_t>::max()), 0, less_time); 
    time_t xx = *x; 
    --xx; // upper_bound gives first invalid value so we use previous one 
    cout << "Max allowed time is: " << ctime(&xx) << endl; 

    return 0; 
}
관련 문제