diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..9ba4f8e --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,36 @@ +#include "utils.h" + +namespace utils { + +char *ax25_base91enc(char *s, uint8_t n, uint32_t v) { + /* Creates a Base-91 representation of the value in v in the string */ + /* pointed to by s, n-characters long. String length should be n+1. */ + for(s += n, *s = '\0'; n; n--) { + *(--s) = v % 91 + 33; + v /= 91; + } + return(s); +} + +static String padding(unsigned int number, unsigned int width) { + String result; + String num(number); + if (num.length() > width) { + width = num.length(); + } + for (unsigned int i = 0; i < width - num.length(); i++) { + result.concat('0'); + } + result.concat(num); + return result; +} + +String createDateString(time_t t) { + return String(padding(year(t), 4) + "-" + padding(month(t), 2) + "-" + padding(day(t), 2)); +} + +String createTimeString(time_t t) { + return String(padding(hour(t), 2) + ":" + padding(minute(t), 2) + ":" + padding(second(t), 2)); +} + +} \ No newline at end of file diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..8b992f2 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,14 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#include +#include + +namespace utils { + +char *ax25_base91enc(char *s, uint8_t n, uint32_t v); +String createDateString(time_t t); +String createTimeString(time_t t); + +} +#endif \ No newline at end of file