public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r13-6396] libstdc++: Make std::chrono::current_zone() default to UTC
Date: Wed,  1 Mar 2023 21:26:19 +0000 (GMT)	[thread overview]
Message-ID: <20230301212619.B2B5F3858D33@sourceware.org> (raw)

https://gcc.gnu.org/g:4abd5bc600193e821fbc41995a0b8d9ea42b42c3

commit r13-6396-g4abd5bc600193e821fbc41995a0b8d9ea42b42c3
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Feb 28 21:07:48 2023 +0000

    libstdc++: Make std::chrono::current_zone() default to UTC
    
    This is consistent with the behaviour of glibc, which assumes UTC when
    /etc/localtime and TZ do not identify a valid time zone. The fallback
    tzdb used when no valid tzdata exists always contains the UTC zone, so
    this change means we have a valid tzdb and valid current zone even in
    the degenerate case.
    
    With this default we no longer need the AIX-specific kluge to try and
    identify TZ values specifying a 0-offset zone. We can just use the UTC
    default for those, as it has the same effect.
    
    It's still possible for chrono::current_zone() to fail, because the user
    could have provided a custom tzdata.zi file which doesn't contain the
    UTC time zone, so the "UTC" default would fail to find a valid zone, and
    throw an exception. That's just user error, they should not provide bad
    data and expect reasonable behaviour.
    
    libstdc++-v3/ChangeLog:
    
            * src/c++20/tzdb.cc (chrono::tzdb::current_zone()) Use "UTC" if
            current time zone cannot be determined.
            * testsuite/std/time/tzdb/1.cc: Remove conditions based on
            HAVE_TZDB macro and test all members unconditionally.

Diff:
---
 libstdc++-v3/src/c++20/tzdb.cc            | 15 ++++++---------
 libstdc++-v3/testsuite/std/time/tzdb/1.cc | 22 +++++++++++-----------
 2 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/libstdc++-v3/src/c++20/tzdb.cc b/libstdc++-v3/src/c++20/tzdb.cc
index 2e7e173f0ef..2b68888e3b9 100644
--- a/libstdc++-v3/src/c++20/tzdb.cc
+++ b/libstdc++-v3/src/c++20/tzdb.cc
@@ -1692,19 +1692,16 @@ namespace std::chrono
     // https://www.ibm.com/support/pages/managing-time-zone-variable-posix
     if (const char* env = std::getenv("TZ"))
       {
-	string_view s(env);
-	if (s == "GMT0")
-	  s = "Etc/GMT";
-	else if (s.size() == 4 && s[3] == '0')
-	  s = "Etc/UTC";
-
-	// This will fail unless TZ contains an IANA time zone name,
-	// or one of the special cases above.
-	if (auto tz = do_locate_zone(this->zones, this->links, s))
+	// This will fail unless TZ contains an IANA time zone name.
+	if (auto tz = do_locate_zone(this->zones, this->links, env))
 	  return tz;
       }
 #endif
 
+    // Default to UTC.
+    if (auto tz = do_locate_zone(this->zones, this->links, "UTC"))
+      return tz;
+
     __throw_runtime_error("tzdb: cannot determine current zone");
   }
 
diff --git a/libstdc++-v3/testsuite/std/time/tzdb/1.cc b/libstdc++-v3/testsuite/std/time/tzdb/1.cc
index 64e42701d3b..877a55b8d31 100644
--- a/libstdc++-v3/testsuite/std/time/tzdb/1.cc
+++ b/libstdc++-v3/testsuite/std/time/tzdb/1.cc
@@ -1,7 +1,6 @@
 // { dg-options "-std=gnu++20" }
 // { dg-do run { target c++20 } }
 // { dg-require-effective-target cxx11_abi }
-// { dg-additional-options "-DHAVE_TZDB" { target tzdb } }
 
 #include <chrono>
 #include <testsuite_hooks.h>
@@ -14,22 +13,25 @@ test_version()
   const tzdb& db = get_tzdb();
   VERIFY( &db == &get_tzdb_list().front() );
 
-#ifdef HAVE_TZDB
-  VERIFY( db.version == remote_version() );
-  const tzdb& reloaded = reload_tzdb();
-  if (reloaded.version == db.version)
-    VERIFY( &reloaded == &db );
-#endif
+  const char* func;
+  try {
+    func = "remote_version";
+    VERIFY( db.version == remote_version() );
+    func = "reload_tzdb";
+    const tzdb& reloaded = reload_tzdb();
+    if (reloaded.version == db.version)
+      VERIFY( &reloaded == &db );
+  } catch (const std::exception&) {
+    std::printf("std::chrono::%s() failed\n", func);
+  }
 }
 
 void
 test_current()
 {
-#ifdef HAVE_TZDB
   const tzdb& db = get_tzdb();
   const time_zone* tz = db.current_zone();
   VERIFY( tz == std::chrono::current_zone() );
-#endif
 }
 
 void
@@ -43,9 +45,7 @@ test_locate()
   VERIFY( tz == db.locate_zone("Etc/GMT") );
   VERIFY( tz == db.locate_zone("Etc/GMT+0") );
 
-#ifdef HAVE_TZDB
   VERIFY( db.locate_zone(db.current_zone()->name()) == db.current_zone() );
-#endif
 }
 
 int main()

                 reply	other threads:[~2023-03-01 21:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230301212619.B2B5F3858D33@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).