Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed use of struct tm and fixed extra line highlight bug #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AndreaEgan06
Copy link

@AndreaEgan06 AndreaEgan06 commented Jun 15, 2024

The math in the long if condition was refactored as it was causing the bug.

time(0) returns the number of seconds since 01/01/1970 as a time_t, which at least for Macs is just a typedef for long

The math in the long if condition was refactored as it was causing the bug.
@willkill07
Copy link

I also watched the most recent stream and have a better suggestion:

Rely on ctime() and simply parse its output. Starting from offset 11, an exact ASCII string representation of the local time is returned (though you only need the first 8 characters [11,19) to determine the digit). Furthermore, since ord(':') == ord('9') + 1, you can simply subtract by '0' to get the zero-based index of the character, even if it's a colon.

Example code:

time_t t = time(0);
char* a = ctime(&t) + 11;
for (int i = 0; i < 8; ++i) {
  int index = a[i];
  /* do what you need to with index */
}

Here's my playground in cling, a C++ interpreter:

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <time.h>
[cling]$ time_t t = time(0);
[cling]$ char* a = ctime(&t);
[cling]$ a
(char *) "Sat Jun 15 18:41:17 2024
"
[cling]$ a + 11
(char *) "18:41:17 2024
"
[cling]$ a[19]=0;
[cling]$ a + 11
(char *) "18:41:17"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants