I'm fairly new to the C++ world (did some C work in the past years ago) so I'm a little rusty on some things :) I've got a small table in a database and I'm trying to grab some date information out of a UNIX EPOCH time stamp (all seconds). I've tried tackling this with time.h but I can't seem to find anything in time.h that allows a 'from string' type of approach so I thought I'd ask for some advice.
My database is on MySQL 5 (5.0.45) and is on a Fedora box. The table looks like this:
Table: weather
+-------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| id | char(4) | NO | PRI | | |
| last_update | int(11) | YES | | 0 | |
+-------------+---------+------+-----+---------+-------+
The data looks like this:
mysql> select * from site where id = 'kict';
+------+-------------+
| id | last_update |
+------+-------------+
| kict | 1218689222 |
+------+-------------+
1 row in set (0.07 sec)
The date string you see in the table is pulled via cURL as a the FILETIME. I checked to see if you could specify the date format that it supplies but this is not possible (as far as I could tell).
What I am trying to accomplish is taking the date from the above database and pulling out certain pieces such as the month, day and year.
Here is why code so far with my 'rough' implementation but it isn't working.
#include <iostream>
#include <iomanip>
#include <mysql++.h>
#include <time.h>
using namespace std;
char dbHost[20] = "localhost";
char dbUser[20] = "***";
char dbPass[20] = "***";
char dbName[20] = "weather";
int main(int argc, char *argv[])
{
// Connect to the DB
mysqlpp::Connection conn(false);
if (!conn.connect(dbName, dbHost, dbUser, dbPass)) {
cerr << "DB Connection Failed: " << conn.error() << endl;
return 1;
}
// Query
mysqlpp::Query query = conn.query("select id,last_update,from_unixtime(last_update) from site");
if (mysqlpp::StoreQueryResult res = query.store())
{
for (int i = 0; i < res.num_rows(); ++i) {
// Here is where I want to convert the time from the result set
cout << res[i][0] << " - " << res[i][1] << endl;
}
}
else
{
cerr << "Failed to get item list: " << query.error() << endl;
return 1;
}
return 0;
}
Could someone give me some pointers? Like I said, I'm at wit's end here trying to figure this out!
Thanks!
Kelly