This is an archive of the discontinued LLVM Phabricator instance.

[libc++][chrono] Fixes formatter duration.
ClosedPublic

Authored by Mordante on Nov 28 2022, 8:35 AM.

Details

Reviewers
EricWF
Group Reviewers
Restricted Project
Commits
rGa4814bdc5371: [libc++][chrono] Fixes formatter duration.
Summary

@EricWF spotted this issue in the post-commit review comments of
D134742. However the suggestion to just use chrono calculations can
result in similar issues when using small fractional seconds.

Diff Detail

Event Timeline

Mordante created this revision.Nov 28 2022, 8:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 8:35 AM
Mordante requested review of this revision.Nov 28 2022, 8:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 8:35 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
mclow.lists added inline comments.
libcxx/test/std/time/time.syn/formatter.duration.pass.cpp
1169

Are you sure this number shouldn't be 65'535 (aka 0FFFF)?

Wouldn't it be better to write them all in hex, so it's clear what they're represententing?

Mordante updated this revision to Diff 478645.Nov 29 2022, 10:58 AM

Addresses review comments.

Mordante marked an inline comment as done.Nov 29 2022, 11:57 AM
Mordante added inline comments.
libcxx/test/std/time/time.syn/formatter.duration.pass.cpp
1169

Good catch, I've applied your suggestion.

ldionne added a subscriber: ldionne.Dec 6 2022, 9:33 AM

Deferring approval to @EricWF.

hiraditya added inline comments.
libcxx/include/__chrono/convert_to_tm.h
130

nit: tabs

Mordante updated this revision to Diff 489220.Jan 14 2023, 3:35 AM
Mordante marked an inline comment as done.

Rebased and addresses review comments.

Mordante marked an inline comment as done.Jan 14 2023, 3:49 AM
EricWF requested changes to this revision.Mar 6 2023, 12:18 PM
EricWF added inline comments.
libcxx/include/__chrono/convert_to_tm.h
123

In what case can it be converted to seconds but not hours?

Also is there a test case for the case for the case where the type cannot be converted to hours?

libcxx/test/std/time/time.syn/formatter.duration.pass.cpp
1163

Can't we just use std::chrono::years::max?

Do these test cases fail before your fixes? It looks to me that they're actively avoiding constructing test values that will overflow.

This revision now requires changes to proceed.Mar 6 2023, 12:18 PM
Mordante marked an inline comment as done.Apr 14 2023, 9:01 AM
Mordante added inline comments.
libcxx/include/__chrono/convert_to_tm.h
123

Is the comment unclear?

There are tests, I'll comment below.

libcxx/test/std/time/time.syn/formatter.duration.pass.cpp
43

This value can't be converted to hours.

1163

These tests test the Standard mandated limits as stated by the comment on line 1081. Using larger values will make the tests non-portable.

Mordante requested review of this revision.May 7 2023, 9:35 AM
Mordante marked an inline comment as done.
Mordante updated this revision to Diff 538656.Jul 10 2023, 8:16 AM

Rebased to trigger CI.

EricWF accepted this revision.Jul 10 2023, 4:42 PM

This is a better state of the world for sure. I still think there may be ways to do better, but perfect is the enemy of progress, so let's progress for now and think about perfect later.

libcxx/test/std/time/time.syn/formatter.duration.pass.cpp
1163

Right, but it's important we don't introduce UB if a user violates the limits (which they will).

This revision is now accepted and ready to land.Jul 10 2023, 4:42 PM
EricWF added inline comments.Jul 10 2023, 4:46 PM
libcxx/include/__chrono/convert_to_tm.h
123

If it can't be converted to hours because it potentially overflows, why is it that converting to seconds works?

libcxx/test/std/time/time.syn/formatter.duration.pass.cpp
1163

I don't understand where in the standard you're finding this requirement. Could you please provide the stable name?

Mordante marked 4 inline comments as done.Jul 15 2023, 3:01 AM

This is a better state of the world for sure. I still think there may be ways to do better, but perfect is the enemy of progress, so let's progress for now and think about perfect later.

Thanks for the review! I will give this approach some more thought. But I too prefer to get this fix in.

libcxx/include/__chrono/convert_to_tm.h
123

Since our implementation uses the same number of bits for hours and seconds.

libcxx/test/std/time/time.syn/formatter.duration.pass.cpp
1163

It's in [time.syn] http://eel.is/c++draft/time.syn
I've added that to the comment on line 1111.

This revision was automatically updated to reflect the committed changes.
Mordante marked 2 inline comments as done.