Skip to content

Commit

Permalink
Refactor time and date conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuscu committed Aug 19, 2024
1 parent 19a033f commit 00d69bf
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 158 deletions.
6 changes: 3 additions & 3 deletions packages/concurrency/source/common/ExecutorService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ namespace l::concurrency {
}

void Runnable::Reschedule() {
auto time = l::string::get_unix_timestamp_ms();
auto time = l::string::get_unix_epoch_ms();
mNextTry = time + static_cast<int64_t>(500 + 500 * (rand() / (float)RAND_MAX)); // retry within 1 second
}

void Runnable::Backoff() {
mTries++;
auto time = l::string::get_unix_timestamp_ms();
auto time = l::string::get_unix_epoch_ms();
mNextTry = time + static_cast<int64_t>(round(powf(static_cast<float>(mTries), 2.5f))); // 1 sec, 3 sec, 5 sec, 8 sec, 11 sec etc
}

Expand Down Expand Up @@ -186,7 +186,7 @@ namespace l::concurrency {
mCondition.wait(lock);
}
else {
auto time = l::string::get_unix_timestamp_ms();
auto time = l::string::get_unix_epoch_ms();
for (uint32_t i = 0; i < mRunnables.size(); i++) {
if (mRunnables.at(i)->CanRun(time)) {
runnable = std::move(mRunnables.at(i));
Expand Down
41 changes: 25 additions & 16 deletions packages/logging/include/logging/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,34 @@

namespace l {
namespace string {
int32_t get_local_timezone();
int32_t get_local_daylight_savings(bool inHours = false);

tm get_time_info(int32_t unixtime, bool adjustYearAndMonth = false);
int32_t get_unix_time(const tm& timeinfo, bool adjustYearAndMonth = false);
void get_time_info(int32_t* fullDateAndTime, int32_t unixtime);
int32_t get_unix_time(int32_t* fullDateAndTime);
time_t convert_to_local_time_from_utc_time(const time_t time);
time_t convert_to_utc_time_from_local_time(const time_t time);

size_t get_time_string(char* buf, size_t maxSize);
std::string get_time_string(const int64_t unixtime, std::string_view format = "%Y-%m-%d %X");
void convert_to_tm(const time_t time, tm* timeinfo, bool adjustYearAndMonth = true);
time_t convert_to_time(const tm* timeinfo, bool adjustYearAndMonth = true);
void convert_to_local_tm_from_utc_time(const time_t utctime, tm* localtimeinfo, bool adjustYearAndMonth = true);
time_t convert_to_utc_time_from_local_tm(const tm* localtimeinfo, bool adjustYearAndMonth = true);

int32_t get_unix_epoch();
int64_t get_unix_epoch_ms();

int32_t to_unix_time(int year, int month, int day, int hour = 0, int min = 0, int sec = 0);
int32_t to_unix_time(std::string_view date = "2024-01-18 14:04:00");
int32_t to_unix_time2(std::string_view date = "2024-01-18T14:04:00000Z");

int32_t to_unix_time_from_local(const int32_t* dateAndTime);
int32_t to_unix_time_from_local(const tm& timeinfo);
int32_t to_unix_time_local(std::string_view utcdate = "2024-01-18 14:04:00");
int32_t to_unix_time_local2(std::string_view utcdate = "2024-01-18T14:04:00000Z");

void to_local_time(const int32_t unixtime, int32_t* fullDateAndTime);
std::string to_local_time(const int32_t unixtime, std::string_view format = "%Y-%m-%d %X");

size_t get_local_time_string(char* buf, size_t maxSize);

int32_t get_unix_timestamp();
int64_t get_unix_timestamp_ms();

template<class T>
T get_number(std::string_view number) {
Expand All @@ -41,14 +58,6 @@ namespace string {
}
}



int32_t to_unix_time(std::string_view date = "2024-01-18 14:04:00");
int32_t to_unix_time2(std::string_view date = "2024-01-18T14:04:00000Z");
int32_t to_local_unix_time(std::string_view date = "2024-01-18 14:04:00");
int32_t to_local_unix_time2(std::string_view date = "2024-01-18T14:04:00000Z");
int32_t to_unix_time(int year = 2024, int month = 1, int day = 1, int hour = 0, int min = 0, int sec = 0);

bool cstring_equal(const char* a, const char* b, size_t a_offset = 0, size_t b_offset = 0);
bool partial_equality(const char* a, const char* b, size_t a_offset = 0, size_t b_offset = 0);
bool partial_equality(std::string_view a, std::string_view b, size_t a_offset = 0, size_t b_offset = 0);
Expand Down
Loading

0 comments on commit 00d69bf

Please sign in to comment.