public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Fix std::format output of %C for negative years
@ 2023-12-12 22:46 Jonathan Wakely
  2023-12-13 12:30 ` [committed] libstdc++: Fix regression in std::format output of %Y " Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2023-12-12 22:46 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested x86_64-linux. Pushed to trunk.

-- >8--

During discussion of LWG 4022 I noticed that we do not correctly
implement floored division for the century. We were just truncating
towards zero, rather than applying the floor function. For negative
values that rounds the wrong way.

libstdc++-v3/ChangeLog:

	* include/bits/chrono_io.h (__formatter_chrono::_M_C_y_Y): Fix
	rounding for negative centuries.
	* testsuite/std/time/year/io.cc: Check %C for negative years.
---
 libstdc++-v3/include/bits/chrono_io.h      | 9 +++++++--
 libstdc++-v3/testsuite/std/time/year/io.cc | 7 +++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h
index 16e8fc58dff..b63b8592eba 100644
--- a/libstdc++-v3/include/bits/chrono_io.h
+++ b/libstdc++-v3/include/bits/chrono_io.h
@@ -820,9 +820,14 @@ namespace __format
 
 	  if (__conv == 'Y' || __conv == 'C')
 	    {
-	      if (__is_neg)
-		__s.assign(1, _S_plus_minus[1]);
 	      int __ci = __yi / 100;
+	      if (__is_neg) [[unlikely]]
+		{
+		  __s.assign(1, _S_plus_minus[1]);
+		  // For floored division -123//100 is -2 and -100//100 is -1
+		  if ((__ci * 100) != __yi)
+		    ++__ci;
+		}
 	      if (__ci >= 100) [[unlikely]]
 		{
 		  __s += std::format(_S_empty_spec, __ci / 100);
diff --git a/libstdc++-v3/testsuite/std/time/year/io.cc b/libstdc++-v3/testsuite/std/time/year/io.cc
index 6157afae253..a6683ae20df 100644
--- a/libstdc++-v3/testsuite/std/time/year/io.cc
+++ b/libstdc++-v3/testsuite/std/time/year/io.cc
@@ -43,8 +43,11 @@ test_format()
   s = std::format("{}", --year::min()); // formatted via ostream
   VERIFY( s == "-32768 is not a valid year" );
 
-  s = std::format("{:%y} {:%y}", 1976y, -1976y);
-  VERIFY( s == "76 76" ); // LWG 3831
+  s = std::format("{:%C %y} {:%C %y}", 1976y, -1976y);
+  VERIFY( s == "19 76 -20 76" ); // LWG 3831
+
+  s = std::format("{:%C %y} {:%C %y} {:%C %y}", -9y, -900y, -555y);
+  VERIFY( s == "-01 09 -09 00 -06 55" ); // LWG 4022
 
   s = std::format("{0:%EC}{0:%Ey} = {0:%EY}", 1642y);
   VERIFY( s == "1642 = 1642" );
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [committed] libstdc++: Fix regression in std::format output of %Y for negative years
  2023-12-12 22:46 [committed] libstdc++: Fix std::format output of %C for negative years Jonathan Wakely
@ 2023-12-13 12:30 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-12-13 12:30 UTC (permalink / raw)
  To: libstdc++, gcc-patches

It seems that what I pushed didn't match what I tested, due to testing
on a different machine!

Tested x86_64-linux, on the right machine this time. Pushed to trunk.

-- >8 --

The change in r14-6468-ga01462ae8bafa8 was only supposed to apply to %C
formats, not %Y.

libstdc++-v3/ChangeLog:

	* include/bits/chrono_io.h (__formatter_chrono::_M_C_y_Y): Do
	not round century down for %Y formats.
---
 libstdc++-v3/include/bits/chrono_io.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h
index b63b8592eba..bcd76e4ab7b 100644
--- a/libstdc++-v3/include/bits/chrono_io.h
+++ b/libstdc++-v3/include/bits/chrono_io.h
@@ -825,7 +825,7 @@ namespace __format
 		{
 		  __s.assign(1, _S_plus_minus[1]);
 		  // For floored division -123//100 is -2 and -100//100 is -1
-		  if ((__ci * 100) != __yi)
+		  if (__conv == 'C' && (__ci * 100) != __yi)
 		    ++__ci;
 		}
 	      if (__ci >= 100) [[unlikely]]
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-13 12:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-12 22:46 [committed] libstdc++: Fix std::format output of %C for negative years Jonathan Wakely
2023-12-13 12:30 ` [committed] libstdc++: Fix regression in std::format output of %Y " Jonathan Wakely

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).