diff --git a/src/Database.cpp b/src/Database.cpp index c64b29a6..4d7b0486 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -29,21 +29,29 @@ #include #include #include // TODO Remove +#include +#include //////////////////////////////////////////////////////////////////////////////// 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 (¤t); + struct tm* t = gmtime (¤t); + + 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 (); +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Database.h b/src/Database.h index db9c172b..1df2ebd6 100644 --- a/src/Database.h +++ b/src/Database.h @@ -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 _data_files {}; };