Skip to content

Option to format timestamp as [ HH:MM:SS.MMM ] #5

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

Closed
MattStarfield opened this issue Aug 26, 2020 · 1 comment · Fixed by #28
Closed

Option to format timestamp as [ HH:MM:SS.MMM ] #5

MattStarfield opened this issue Aug 26, 2020 · 1 comment · Fixed by #28
Labels
conclusion: resolved Issue was resolved topic: code Related to content of the project itself type: enhancement Proposed improvement

Comments

@MattStarfield
Copy link

Showing the timestamp as a raw ms count can be helpful for doing time-math, but for visual inspection, it can be much easier to have the timestamp formatted as something like [ HH:MM:SS.MMM ]. For more than 100 hours, an extra H digit can be added.

I suggest the addition of a format switch function, something like formatTimestampOn();

@MattStarfield
Copy link
Author

If it's helpful, here's how I implemented this in my own application...

I have this function:

// return a millisecond value as a string formatted as "HH:MM:SS.mmm"
    char  formattedMillisBuffer[17];                                 // global char buffer for the printing of milliseconds in formatted time
    char* getFormattedMillisString(uint32_t msCount)
    {
      uint16_t milliseconds  = msCount % 1000;          // ms remaining when converted to seconds
      uint16_t allSeconds   = msCount / 1000;           // total number of seconds to calculate remaining values

      uint16_t hours            = allSeconds / 3600;    // convert seconds to hours
      uint16_t secondsRemaining = allSeconds % 3600;    // seconds left over

      uint16_t minutes  = secondsRemaining / 60 ;       // convert seconds left over to minutes
      uint16_t seconds  = secondsRemaining % 60;        // seconds left over

      snprintf(formattedMillisBuffer, sizeof(formattedMillisBuffer),           // "prints" formatted output to a char array (string)
                "%02d:"   //HH:
                "%02d:"   //MM:
                "%02d."   //SS.
                "%03d"    //MMM
                ,
                hours,
                minutes,
                seconds,
                milliseconds
            );

      return formattedMillisBuffer;

    } // END -- getFormattedMillisString()

which I'm calling like this

// add print of Debug Mode if debugLevel is at or above DBG_INFO
// use Serial.print instead of Debug.print() to ignore the state of Debug.timestampXX()
    if(DEBUG_MODE_DEFAULT >= DBG_INFO)
    {
       // Print formatted time
       Serial.print(F("["));
       Serial.print(getFormattedMillisString(millis()));
       Serial.print(F("] "));
       Serial.print(F("debug message\n\r"));
    }

in place of:

Debug.print(DBG_INFO,    F("debug message") );

the "formatted time" debug message looks like this:

[00:00:02.823] debug message

which is helpful because it aligns all of the times and message text to fixed widths, making the debug output more pleasant and readable.

aentinger added a commit that referenced this issue Jun 23, 2022
aentinger added a commit that referenced this issue Jun 23, 2022
@per1234 per1234 added type: enhancement Proposed improvement conclusion: resolved Issue was resolved topic: code Related to content of the project itself labels Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: resolved Issue was resolved topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants