Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix time utils time_t 64 bits in windows #134

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

EugenioCollado
Copy link

This PR addresses an issue where Timestamp::max() or Timestamp::min() may exceed the representable range of std::tm, as time_t can be a 64-bit long long int. Specifically, std::tm in Windows cannot express dates before 1970 or beyond 2038.

To resolve this, the PR truncates time_t when converting it to std::tm and adjusts Timestamp::max() and Timestamp::min() to fit within the accepted range on Windows (0 to 2^32).

Signed-off-by: Eugenio Collado <[email protected]>
@codecov-commenter
Copy link

codecov-commenter commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.

Project coverage is 65.85%. Comparing base (328d165) to head (ffd1418).

Files with missing lines Patch % Lines
cpp_utils/src/cpp/time/time_utils.cpp 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #134      +/-   ##
==========================================
- Coverage   65.97%   65.85%   -0.13%     
==========================================
  Files          64       64              
  Lines        1699     1693       -6     
  Branches      484      481       -3     
==========================================
- Hits         1121     1115       -6     
  Misses        278      278              
  Partials      300      300              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Eugenio Collado <[email protected]>
@@ -32,7 +32,8 @@ using Duration_ms = uint32_t;
/**
* Type used to represent time points
*/
using Timestamp = std::chrono::time_point<std::chrono::system_clock>;
using Timeclock = std::chrono::system_clock;
using Timestamp = std::chrono::time_point<Timeclock>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please comment properly.

// future time
{
Timestamp future_time = date_to_timestamp(2233u, 5u, 22u);
Timestamp future_time = date_to_timestamp(2033u, 5u, 22u);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha

}

Timestamp the_end_of_time() noexcept
{
return std::chrono::time_point<std::chrono::system_clock>::max();
#if _EPROSIMA_WINDOWS_PLATFORM // In Windows std::gmtime does not support negative values nor values greater than 2^32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be constraining precision due to limitations in a method we are only using for time to calendar conversions. I believe we should address the limitation in that method only, hoping the fix the problem in the future or we might as well change the implementation to not use that problematic method.

}

Timestamp the_end_of_time() noexcept
{
return std::chrono::time_point<std::chrono::system_clock>::max();
#if _EPROSIMA_WINDOWS_PLATFORM // In Windows std::gmtime does not support negative values nor values greater than 2^32
return Timeclock::from_time_t(std::numeric_limits<long>::max());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Timeclock::from_time_t(std::numeric_limits<long>::max());
return Timeclock::from_time_t(std::numeric_limits<int32_t>::max());

}

Timestamp the_beginning_of_time() noexcept
{
return std::chrono::time_point<std::chrono::system_clock>::min();
#if _EPROSIMA_WINDOWS_PLATFORM // In Windows std::gmtime does not support negative values nor values greater than 2^32
return Timeclock::from_time_t(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Apparently, at least in the Windows machine I have been testing, beginning of time actually is "1969-12-31_12-00-00", but I agree better to cut at 0. Mind blowing...

Comment on lines +108 to +111
#if _EPROSIMA_WINDOWS_PLATFORM // In Windows std::gmtime does not support negative values nor values greater than 2^32
time_t max_value = std::numeric_limits<long>::max();
duration_seconds = std::max((time_t) 0, std::min(max_value, duration_seconds));
#endif // if _EPROSIMA_WINDOWS_PLATFORM
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly what I meant. However perhaps we should print a warning if clamping has been done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also be handling similar problems in string_to_timestamp and date_to_timestamp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants