2014-03-05 3 views
1

libpq 라이브러리를 사용하여 C++에서 postgreSQL으로 연결합니다. postgreSQL에서 날짜 (타임 존이없는 타임 스탬프)를 요청하고 얻습니다. 그러나 결과에는 수정 방법을 알지 못하는 오프셋이 있습니다.C++ 시간이없는 postgres 타임 스탬프를 time_t에 변환

포스트 그레스 테이블 : C++ 코드에서

id    date 
integer   timestamp without time zone 
29996   2014-02-28 23:59:00 

결과 :

id: 29996, Date: Sat Mar 01 10:59:00 2014 

당신은 날짜가 오프셋 것을 볼 수 있습니다. 아래는 내가 사용하고있는 코드입니다. 어떤 도움을 크게 환영합니다

PGconn *m_connection; 
PGresult *res; 

string strConn = "dbname=test host=localhost user=username password=secret"; 
m_connection = PQconnectdb(strConn.c_str()); 

string query = "SELECT id, extract(epoch from date)::bigint ";  
query += "FROM table_test ORDER BY id DESC LIMIT 1"; 

// send query 
res = PQexecParams(m_connection, query.c_str(), 0, 0, 0, 0, 0, 0); 

string id = PQgetvalue(res, 0, 0); 

unsigned char *data = (unsigned char*)PQgetvalue(res, 0, 1); 
unsigned int length = PQgetlength(res, 0 , 1); 
time_t time = _atoi64((char*)data); 

PQclear(res); 


std::cout << "id:"<< id << ", Date: " << ctime(&time) << "\n"; 

답변

0

ctime은 로컬 시간을 사용하므로 오프셋에서 끝납니다.

GMT를 사용하려면 asctime(gmtime(&time))을 사용해야합니다. 현지 시간의 영향을받지 않고 날짜/시간을 제공합니다.

ctimeasctime(localtime(&time))

+0

감사하는 것과 동일합니다! 완벽하게 작동합니다. – miguel

관련 문제