C++ Logo

std-proposals

Advanced search

[std-proposals] The comparison between time_zone and zoned_time is inconsistent

From: Yexuan Xiao <bizwen_at_[hidden]>
Date: Sun, 25 Aug 2024 20:47:53 +0000
Hello everyone,

I noticed that the comparison for time_zone is based on the time zone name, whereas the comparison for zoned_time depends on the pointer of time_zone.

https://eel.is/c++draft/time.zone.timezone#time.zone.nonmembers-1
https://eel.is/c++draft/time.zone.zonedtime.nonmembers#1

When two versions of tzdb exist in a program, the same time zone may have different addresses.

However, the comparison function for zoned_time only checks the stored pointer to the time_zone and doesn’t consider the time zone name. As a result, two zoned_times with the same time point and the same time zone may compare as unequal if they use different time_zone pointers.

The following code demonstrates this difference:

#include <chrono>
#include <cassert>
int main()
{
      using namespace std::chrono;
      const tzdb& db = get_tzdb();
      const tzdb& new_db = reload_tzdb();
      const time_zone* ltzone = db.current_zone();
      const time_zone* new_ltzone = new_db.current_zone();

      if (db.version == new_db.version)
            return 1;

      auto now = system_clock::now();

      zoned_time lztime{ ltzone, now };
      zoned_time new_lztime{ new_ltzone, now };

      assert(*ltzone == *new_ltzone);

      if (ltzone not_eq new_ltzone)
            assert(lztime not_eq new_lztime);
}

I believe that when comparing two time_zones, one should first consider comparing their addresses, and then their names. Similarly, when comparing zoned times, the same approach should be taken.

Received on 2024-08-25 20:48:00