Search code examples
c++c++11sqlitec++-chrono

C++ and Sqlite3: How to store date/time with milliseconds precision


I´m building a C++ application that will be running in Ubuntu and will use Sqlite3 as a database.

One of my goals is to have a C++ class containing time/date fields and store then on database.

In the past I´ve used time_t as the variable type on my class and stored them as INTEGER type on Sqlite3, like:

C++ Class:

class MyClass {

        time_t dateTimeInfo;
}

Sqlite3:

CREATE TABLE MYCLASS (....., INTEGER DATETIMEINFO, ...);

That approach allows me to SELECT times with different comparasion operatiors (>, >=, < , <=, ==) with no problems at all, as I´m dealing with simple number. At the user level the time_t is converted to ISO 8601 std::string´s so that I can have a human readable interface.

This works very well except that it does not support millisecods. In my current project I need to support them, so I need to make changes to this.

As far I had studies I undestand I need to use std::chrono::time_point as the class type, as follows:

C++ Class:

class MyClass {

        std::chrono::time_point dateTimeInfo;
}

But I really don´t know what data type to use in Sqlite3 and if it will work the same way time_t used to...

Sqlite3:

CREATE TABLE MYCLASS (....., ???? DATETIMEINFO, ...);

My questions:

a) Is std::chrono::time_point the correct option here ?

b) What is the Sqlite3 type equivalent ? Still an INTEGER ?

c) Is this the recommended approach (no boost please, C++11) ?

Thanks for helping.


Solution

  • Yes, std::chrono is a very good approach (C++ internally). You can convert time points from/to milliseconds using std::chrono like this:

    using namespace std::chrono;
    
    typedef time_point<system_clock, milliseconds> time_points_millis;
    
    time_points_millis tp = system_clock::now();
    
    // An at least 43 bit signed integral type
    auto epoch = tp.time_since_epoch();
    

    In this case you should use an 64-bit integral type to store it with SQLite because the data can exceed 32 bits.

    Comment: I don't really know much about SQLite, yet, but I found this for 64-bit types: official docs.