Skip to content

Conversation

sipa
Copy link
Member

@sipa sipa commented Apr 13, 2018

And replace them with just hardcoded ISO8601 strings and gmtime_r.

Pointed out by @laanwj here: #12970 (comment)

src/utiltime.cpp Outdated
char buf[256];
struct tm time_struct;
time_t time_val = nTime;
gmtime_r(&time_val, &time_struct);
Copy link
Member

Choose a reason for hiding this comment

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

Is this portable? Afiak all of the madness is usually an abstraction of these variants.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well at least this code doesn't need any allocations.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't just use gmtime? It's more portable.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ken2812221 gmtime is not thread safe. We either user gmtime_r, or introduce a lock around the call.

@practicalswift
Copy link
Contributor

Concept ACK

@laanwj
Copy link
Member

laanwj commented Apr 13, 2018

A potential problem is that strftime depends on the locale, just like many other C string formatting functions, which has made us avoid them in general. From the developer notes:

  • Use std::string, avoid C string manipulation functions
  • Rationale: C++ string handling is marginally safer, less scope for buffer overflows and surprises with \0 characters. Also some C string manipulations tend to act differently depending on platform, or even the user locale

The imbue stuff forces using the 'C' locale, I'm afraid if we want to use C functions we'd need a C equivalent of that and that's probably just as bad.

@laanwj
Copy link
Member

laanwj commented Apr 13, 2018

FWIW it doesn't need full strftime support. The only datetime formats left in non-test code are ISO8601:

src/utiltime.cpp:    return DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", nTime);
src/utiltime.cpp:    return DateTimeStrFormat("%Y-%m-%d", nTime);
src/utiltime.cpp:    return DateTimeStrFormat("%H:%M:%SZ", nTime);

Another potential simplification could be around that they all deal with UTC, and have UNIX epoch format as input.

Edit: so to be clear, big concept ACK on getting rid of this madness, but let's think about the replacement a bit. Maybe there's something like tinyformat.h that we can just import from somewhere...

@laanwj
Copy link
Member

laanwj commented Apr 13, 2018

The imbue stuff forces using the 'C' locale, I'm afraid if we want to use C functions we'd need a C equivalent of that and that's probably just as bad.

A reminder that this is where we're coming from: 3e8ac6a

Edit: Thinking about it further - it becomes less clear to me whether the slowness is due to our own code, or due to formatting, or something else. After all, the speed seems to depend on the operating system. Maybe the slowdown (fixed by #12970) is due to barraging the kernel with "get current time" system calls. This is fairly fast on Linux - due to vdso user-space implementation of some syscalls - but not on windows or other OSes.

@sipa
Copy link
Member Author

sipa commented Apr 13, 2018

@laanwj Thanks, you're right that this approach ignores the reasons why the old code existed in the first place. I've replaced it with just hardcoded ISO8601 format strings... please let me know if you think this is too NIH.

Perhaps this is overkill as it may not help for performance. I remember we've had multiple horror stories in the past with this code though; I'd prefer to get rid of it.

@laanwj
Copy link
Member

laanwj commented Apr 13, 2018

I've replaced it with just hardcoded ISO8601 format strings... please let me know if you think this is too NIH.

No I think this is great 👍 It's very straightforward.

tACK 1527015

@practicalswift
Copy link
Contributor

utACK 1527015

return DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", nTime);
struct tm ts;
time_t time_val = nTime;
gmtime_r(&time_val, &ts);
Copy link
Member

@laanwj laanwj Apr 13, 2018

Choose a reason for hiding this comment

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

I'm happy that this works on windows as-is as well!

@jonasschnelli
Copy link
Contributor

utACK 1527015

@laanwj laanwj merged commit 1527015 into bitcoin:master Apr 14, 2018
laanwj added a commit that referenced this pull request Apr 14, 2018
1527015 Avoid std::locale/imbue in DateTimeStrFormat (Pieter Wuille)

Pull request description:

  And replace them with just hardcoded ISO8601 strings and `gmtime_r`.

  Pointed out by @laanwj here: #12970 (comment)

Tree-SHA512: a459758b42ca56f8462115aefe8e6377c1319fce509ea64dbb767f3f087c9b848335954cb684e5896c38008847684045505a3e1559fb3e83b8e80e10b003d1e7
@sipsorcery
Copy link
Contributor

As an fyi Windows doesn't come with gmtime_r so this PR broke the msvc build.

I'd say there's an easy enough workaround and will look into it.

laanwj added a commit that referenced this pull request Apr 26, 2018
abd58a2 Fix for utiltime to compile with msvc. (Aaron Clauson)

Pull request description:

  This PR allows utiltime.cpp to compile with msvc after the changes introduced in #12973.

Tree-SHA512: 7233b1c23400bf19aef2fcb6168009ef58b9e7f8e49c46d8cf9d04394091f370e39496d24ca00b294c4164bcfc04514e329bf6bb05169406c34ce7cd8c382565
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 13, 2021
…rmat

1527015 Avoid std::locale/imbue in DateTimeStrFormat (Pieter Wuille)

Pull request description:

  And replace them with just hardcoded ISO8601 strings and `gmtime_r`.

  Pointed out by @laanwj here: bitcoin#12970 (comment)

Tree-SHA512: a459758b42ca56f8462115aefe8e6377c1319fce509ea64dbb767f3f087c9b848335954cb684e5896c38008847684045505a3e1559fb3e83b8e80e10b003d1e7
xdustinface added a commit to xdustinface/dash that referenced this pull request Apr 14, 2021
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 14, 2021
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 14, 2021
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 15, 2021
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 17, 2021
…rmat

1527015 Avoid std::locale/imbue in DateTimeStrFormat (Pieter Wuille)

Pull request description:

  And replace them with just hardcoded ISO8601 strings and `gmtime_r`.

  Pointed out by @laanwj here: bitcoin#12970 (comment)

Tree-SHA512: a459758b42ca56f8462115aefe8e6377c1319fce509ea64dbb767f3f087c9b848335954cb684e5896c38008847684045505a3e1559fb3e83b8e80e10b003d1e7
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 18, 2021
…rmat

1527015 Avoid std::locale/imbue in DateTimeStrFormat (Pieter Wuille)

Pull request description:

  And replace them with just hardcoded ISO8601 strings and `gmtime_r`.

  Pointed out by @laanwj here: bitcoin#12970 (comment)

Tree-SHA512: a459758b42ca56f8462115aefe8e6377c1319fce509ea64dbb767f3f087c9b848335954cb684e5896c38008847684045505a3e1559fb3e83b8e80e10b003d1e7
kwvg pushed a commit to kwvg/dash that referenced this pull request Apr 23, 2021
…rmat

1527015 Avoid std::locale/imbue in DateTimeStrFormat (Pieter Wuille)

Pull request description:

  And replace them with just hardcoded ISO8601 strings and `gmtime_r`.

  Pointed out by @laanwj here: bitcoin#12970 (comment)

Tree-SHA512: a459758b42ca56f8462115aefe8e6377c1319fce509ea64dbb767f3f087c9b848335954cb684e5896c38008847684045505a3e1559fb3e83b8e80e10b003d1e7
random-zebra added a commit to PIVX-Project/PIVX that referenced this pull request May 5, 2021
f3f3d6c util: fix compilation with mingw-w64 7.0.0 (Fuzzbawls)
f24da7d util: Avoid potential uninitialized read in FormatISO8601DateTime (Fuzzbawls)
120a12a util: Add type safe GetTime (Fuzzbawls)
be48766 Fix for utiltime to compile with msvc. (Aaron Clauson)
2833406 Avoid std::locale/imbue in DateTimeStrFormat (Pieter Wuille)
3331c25 Format automatic wallet backup file names in ISO 8601 basic format (Fuzzbawls)
c32a208 Format timestamps using ISO 8601 formatting (e.g. "2018-02-28T12:34:56Z") (practicalswift)
f8bd173 [logging] log system time and mock time (John Newbery)

Pull request description:

  This is a backport of a collection of upstream PRs to bring our time utility more up to date. The following upstream PRs are included:

  - bitcoin#10383
  - bitcoin#12567
  - bitcoin#12973
  - bitcoin#13031
  - bitcoin#16046
  - bitcoin#18162
  - bitcoin#18358

  The two user-facing notable changes here have been documented in the release notes template. I've also connected the functionality that `-logtimemicros` was supposed to provide.

ACKs for top commit:
  furszy:
    code review ACK f3f3d6c
  random-zebra:
    utACK f3f3d6c after rebase, and merging...

Tree-SHA512: 64f9cc1f7fc65c192f3b3ab869c057a81e007cf0fae82ecd23993c6b51830e4bc80656c699ba40bb8e019115a24d1ea88a618be0c0d7112749d9ce125d62ce44
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request May 21, 2021
abd58a2 Fix for utiltime to compile with msvc. (Aaron Clauson)

Pull request description:

  This PR allows utiltime.cpp to compile with msvc after the changes introduced in bitcoin#12973.

Tree-SHA512: 7233b1c23400bf19aef2fcb6168009ef58b9e7f8e49c46d8cf9d04394091f370e39496d24ca00b294c4164bcfc04514e329bf6bb05169406c34ce7cd8c382565
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request May 25, 2021
abd58a2 Fix for utiltime to compile with msvc. (Aaron Clauson)

Pull request description:

  This PR allows utiltime.cpp to compile with msvc after the changes introduced in bitcoin#12973.

Tree-SHA512: 7233b1c23400bf19aef2fcb6168009ef58b9e7f8e49c46d8cf9d04394091f370e39496d24ca00b294c4164bcfc04514e329bf6bb05169406c34ce7cd8c382565
TheArbitrator pushed a commit to TheArbitrator/dash that referenced this pull request Jun 4, 2021
abd58a2 Fix for utiltime to compile with msvc. (Aaron Clauson)

Pull request description:

  This PR allows utiltime.cpp to compile with msvc after the changes introduced in bitcoin#12973.

Tree-SHA512: 7233b1c23400bf19aef2fcb6168009ef58b9e7f8e49c46d8cf9d04394091f370e39496d24ca00b294c4164bcfc04514e329bf6bb05169406c34ce7cd8c382565
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants