3

I know I can convert SQL timestamp to unix timestamp, using the following way.

SELECT extract(epoch FROM now());

Now, I have a stored procedure function, which will directly return a table row to the caller. One of the row field is "timestamp" type.

In my application, I am using libpq. I wish to use libpq functions (or any c/c++ function), to convert "2010-01-11 13:10:55.283" into unix timestamp. Off course, I can create another stored procedure named

SQLTimestamp2UnixTimestamp
SELECT extract(epoch FROM $1);

But I just wish to accomplish this task with a single c/c++ function call, without involving stored procedure.

Any suggestion? Thanks!

1

2 Answers 2

4

Why not simply (untested):

/* PostgreSQL sent "date" */
strptime(date, "%Y-%m-%d %H:%M:%S", &result);

then

strftime(epoch, MAX, "%s", result);
/* print epoch */
Sign up to request clarification or add additional context in comments.

Comments

1
boost::posix_time::ptime t(boost::posix_time::time_from_string(ts));
boost::posix_time::ptime start(boost::gregorian::date(1970,1,1)); 
boost::posix_time::time_duration dur = t - start; 
time_t epoch = dur.total_seconds();

long timestamp = static_cast<long>(epoch);

1 Comment

a solution with native c++ without boost library should be mark as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.