Skip to content

Commit

Permalink
Refactor time utils (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioborondo authored Dec 27, 2023
1 parent 8630be7 commit be98b3a
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD 17)
add_library(time_tracker_common STATIC
src/db.cc
src/record.cc
src/time_utils.cc
src/timestamp.cc
)

target_include_directories(time_tracker_common
Expand Down
10 changes: 1 addition & 9 deletions src/db.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "db.h"

#include "time_utils.h"

#include <fmt/format.h>

#include <iostream>
Expand Down Expand Up @@ -34,12 +32,6 @@ void Db::Log(LogType log_type)

std::string Db::Summary(const std::string& date)
{
std::string day = date;
if(day.empty())
{
day = time_utils::GetCurrentDate();
}

bool tempRecord{GetLastType() == 1};

if(tempRecord)
Expand Down Expand Up @@ -86,7 +78,7 @@ std::string Db::Summary(const std::string& date)
" ) as records2 "
") "};

const auto sql{fmt::format(sql_format_string, day)};
const auto sql{fmt::format(sql_format_string, date)};

sqlite3_stmt* stmt{nullptr};

Expand Down
3 changes: 2 additions & 1 deletion src/db.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "record.h"
#include "timestamp.h"

#include <string>

Expand All @@ -27,7 +28,7 @@ class Db

void DeleteLast();

std::string Summary(const std::string& date = "");
std::string Summary(const std::string& date = timestamp::GetCurrentDate());

bool DeleteRecords();

Expand Down
4 changes: 1 addition & 3 deletions src/record.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "record.h"

#include "time_utils.h"

Record::Record(Record::Type type, const std::string& timestamp):
type_{type},
timestamp_{timestamp.empty() ? time_utils::GetCurrentDateAndTime() : timestamp}
timestamp_{timestamp}
{
}

Expand Down
4 changes: 3 additions & 1 deletion src/record.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "timestamp.h"

#include <string>

class Record
Expand All @@ -11,7 +13,7 @@ class Record
kStart,
};

Record(Type type, const std::string& timestamp = "");
Record(Type type, const std::string& timestamp = timestamp::GetCurrentDateAndTime());

Type GetType() const;

Expand Down
4 changes: 2 additions & 2 deletions src/time_utils.cc → src/timestamp.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "time_utils.h"
#include "timestamp.h"

#include <ctime>

Expand All @@ -15,7 +15,7 @@ std::string GetCurrentTimestamp(const std::string& format)
}
}

namespace time_utils
namespace timestamp
{
std::string GetCurrentDate()
{
Expand Down
2 changes: 1 addition & 1 deletion src/time_utils.h → src/timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <string>

namespace time_utils
namespace timestamp
{
std::string GetCurrentDate();
std::string GetCurrentDateAndTime();
Expand Down

0 comments on commit be98b3a

Please sign in to comment.