Skip to content

Commit

Permalink
minor changes to StringFromTime
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd committed Jul 19, 2024
1 parent a18ec71 commit 32466a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
35 changes: 10 additions & 25 deletions quill/include/quill/backend/StringFromTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,15 @@ class StringFromTime
if (_time_zone == Timezone::LocalTime)
{
// If localtime is used we will recalculate every 1 hour - this is because of the DST changes
// and an easy work-around

// Then round it down to the nearest hour
timestamp = _nearest_hour_timestamp(timestamp);

// Also calculate and store the next hour timestamp
_next_recalculation_timestamp = _next_hour_timestamp(timestamp);
// calculate and store the next hour timestamp
_next_recalculation_timestamp = _next_full_hour_timestamp(timestamp);
}
else if (_time_zone == Timezone::GmtTime)
{
// otherwise we will only recalculate every noon and midnight. the reason for this is in case
// user is using PM, AM format etc
_next_recalculation_timestamp = _next_noon_or_midnight_timestamp(timestamp, _time_zone);

// we don't need to modify timestamp in the case of UTC
_next_recalculation_timestamp = _next_noon_or_midnight_timestamp(timestamp);
}

// Now populate a pre formatted string for this hour,
Expand Down Expand Up @@ -119,11 +113,11 @@ class StringFromTime

if (_time_zone == Timezone::LocalTime)
{
_next_recalculation_timestamp = _next_hour_timestamp(timestamp);
_next_recalculation_timestamp = _next_full_hour_timestamp(timestamp);
}
else if (_time_zone == Timezone::GmtTime)
{
_next_recalculation_timestamp = _next_noon_or_midnight_timestamp(timestamp + 1, _time_zone);
_next_recalculation_timestamp = _next_noon_or_midnight_timestamp(timestamp);
}
}

Expand Down Expand Up @@ -456,26 +450,18 @@ class StringFromTime
}

/***/
QUILL_NODISCARD static time_t _next_hour_timestamp(time_t timestamp) noexcept
QUILL_NODISCARD static time_t _next_full_hour_timestamp(time_t timestamp) noexcept
{
time_t const next_hour_ts = _nearest_hour_timestamp(timestamp) + 3600;
return next_hour_ts;
}

/***/
QUILL_NODISCARD static time_t _next_noon_or_midnight_timestamp(time_t timestamp, Timezone timezone) noexcept
QUILL_NODISCARD static time_t _next_noon_or_midnight_timestamp(time_t timestamp) noexcept
{
// Get the current date and time now as time_info
tm time_info;

if (timezone == Timezone::GmtTime)
{
gmtime_rs(&timestamp, &time_info);
}
else
{
localtime_rs(&timestamp, &time_info);
}
gmtime_rs(&timestamp, &time_info);

if (time_info.tm_hour < 12)
{
Expand All @@ -493,9 +479,8 @@ class StringFromTime
}

// convert back to time since epoch
std::chrono::system_clock::time_point const next_midnight = (timezone == Timezone::GmtTime)
? std::chrono::system_clock::from_time_t(detail::timegm(&time_info))
: std::chrono::system_clock::from_time_t(std::mktime(&time_info));
std::chrono::system_clock::time_point const next_midnight =
std::chrono::system_clock::from_time_t(detail::timegm(&time_info));

// returns seconds since epoch of the next midnight.
return std::chrono::duration_cast<std::chrono::seconds>(next_midnight.time_since_epoch()).count() + 1;
Expand Down
16 changes: 8 additions & 8 deletions quill/test/unit_tests/StringFromTimeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,19 @@ TEST_CASE("string_from_time_localtime_format_s")
class StringFromTimeMock : public quill::detail::StringFromTime
{
public:
static time_t next_noon_or_midnight_timestamp(time_t timestamp, Timezone timezone) noexcept
static time_t next_noon_or_midnight_timestamp(time_t timestamp) noexcept
{
return quill::detail::StringFromTime::_next_noon_or_midnight_timestamp(timestamp, timezone);
return quill::detail::StringFromTime::_next_noon_or_midnight_timestamp(timestamp);
}

static time_t nearest_hour_timestamp(time_t timestamp) noexcept
{
return quill::detail::StringFromTime::_nearest_hour_timestamp(timestamp);
}

static time_t next_hour_timestamp(time_t timestamp) noexcept
static time_t next_full_hour_timestamp(time_t timestamp) noexcept
{
return quill::detail::StringFromTime::_next_hour_timestamp(timestamp);
return quill::detail::StringFromTime::_next_full_hour_timestamp(timestamp);
}

static std::vector<char> safe_strftime(char const* format_string, time_t timestamp, Timezone timezone)
Expand All @@ -373,15 +373,15 @@ TEST_CASE("next_noon_or_midnight_timestamp")
// Noon utc
time_t constexpr timestamp{1599033200};
time_t constexpr expected_timestamp{1599048000};
time_t const res = StringFromTimeMock::next_noon_or_midnight_timestamp(timestamp, Timezone::GmtTime);
time_t const res = StringFromTimeMock::next_noon_or_midnight_timestamp(timestamp);
REQUIRE_EQ(res, expected_timestamp);
}

{
// Midnight utc
time_t constexpr timestamp{1599079200};
time_t constexpr expected_timestamp{1599091200};
time_t const res = StringFromTimeMock::next_noon_or_midnight_timestamp(timestamp, Timezone::GmtTime);
time_t const res = StringFromTimeMock::next_noon_or_midnight_timestamp(timestamp);
REQUIRE_EQ(res, expected_timestamp);
}
}
Expand All @@ -395,11 +395,11 @@ TEST_CASE("nearest_hour_timestamp")
}

/***/
TEST_CASE("next_hour_timestamp")
TEST_CASE("next_full_hour_timestamp")
{
time_t constexpr timestamp = 1599473669;
time_t constexpr expected_timestamp = 1599476400;
REQUIRE_EQ(StringFromTimeMock::next_hour_timestamp(timestamp), expected_timestamp);
REQUIRE_EQ(StringFromTimeMock::next_full_hour_timestamp(timestamp), expected_timestamp);
}

/***/
Expand Down

0 comments on commit 32466a5

Please sign in to comment.