Database: Identified current data file name

This commit is contained in:
Paul Beckingham 2016-03-05 00:15:44 -05:00
parent 39a3a4aed6
commit a3c468e8a6
2 changed files with 31 additions and 1 deletions

View file

@ -29,21 +29,29 @@
#include <FS.h>
#include <sstream>
#include <iostream> // TODO Remove
#include <iomanip>
#include <ctime>
////////////////////////////////////////////////////////////////////////////////
void Database::initialize (const std::string& location)
{
_location = location;
_current = currentDataFile ();
_data_files.push_back (_current);
Directory d (_location);
for (const auto& file : d.list ())
{
if (1 /* looks like one of our data files */)
{
_data_files.push_back (file);
if (file != _current)
_data_files.push_back (file);
std::cout << "# data file " << file << "\n";
}
}
// TODO If there is no data file named YYYY—MM.data, then create it.
}
////////////////////////////////////////////////////////////////////////////////
@ -58,3 +66,21 @@ std::string Database::dump () const
}
////////////////////////////////////////////////////////////////////////////////
std::string Database::currentDataFile () const
{
time_t current;
time (&current);
struct tm* t = gmtime (&current);
std::stringstream out;
out << _location
<< '/'
<< std::setw (4) << std::setfill ('0') << (t->tm_year + 1900)
<< '-'
<< std::setw (2) << std::setfill ('0') << (t->tm_mon + 1)
<< ".data";
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -37,8 +37,12 @@ public:
void initialize (const std::string&);
std::string dump () const;
private:
std::string currentDataFile () const;
private:
std::string _location {"~/.timewarrior/data"};
std::string _current {};
std::vector <std::string> _data_files {};
};