My Project
format_timestamp_ms.cpp
1 /*
2  * uuid-log - Microcontroller logging framework
3  * Copyright 2019,2022 Simon Arlott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <uuid/log.h>
20 
21 #include <Arduino.h>
22 
23 #include <array>
24 #include <cstdint>
25 #include <string>
26 
27 namespace uuid {
28 
29 namespace log {
30 
31 std::string format_timestamp_ms(uint64_t timestamp_ms, unsigned int days_width) {
32  unsigned long days;
33  unsigned int hours, minutes, seconds, milliseconds;
34 
35  days = timestamp_ms / 86400000UL;
36  timestamp_ms %= 86400000UL;
37 
38  hours = timestamp_ms / 3600000UL;
39  timestamp_ms %= 3600000UL;
40 
41  minutes = timestamp_ms / 60000UL;
42  timestamp_ms %= 60000UL;
43 
44  seconds = timestamp_ms / 1000UL;
45  timestamp_ms %= 1000UL;
46 
47  milliseconds = timestamp_ms;
48 
49  std::array<char,12 + 1 /* days */ + 2 + 1 /* hours */ + 2 + 1 /* minutes */ + 2 + 1 /* seconds */ + 3 /* milliseconds */ + 1> text;
50 
51  snprintf_P(text.data(), text.size(), PSTR("%0*lu+%02u:%02u:%02u.%03u"), std::min(days_width, 12U), days, hours, minutes, seconds, milliseconds);
52 
53  return text.data();
54 }
55 
56 } // namespace log
57 
58 } // namespace uuid
uuid
Common utilities.
Definition: get_uptime_ms.cpp:28
uuid::log::format_timestamp_ms
std::string format_timestamp_ms(uint64_t timestamp_ms, unsigned int days_width)
Format a system uptime timestamp as a string.
Definition: format_timestamp_ms.cpp:31