Hi,
Thanks again for all the useful responses, and it was also interesting to read the background information about the Arduino history and future problems.
In the last two days I modified my Arduino code, putting the mentioned UnixTime to string conversion in a function, because I have to use this code several times in my current project.
Initially I did use the time.h library with the UNIX_OFFSET option, and after a lot of trial and error I finished with the following three variants:
// Variant 1, using time.h and String
#include <time.h>
#include <locale.h>
// Function to convert a UnixTime value to a formatted string using time.h
String getDateTimeString( long ut ) {
time_t t = ut - UNIX_OFFSET;
struct tm * tm = localtime(&t);
char dts[22];
strftime(dts, sizeof(dts), "%F - %T", tm);
return dts;
}
void setup(void) {
Serial.begin(57600);
while (!Serial) {
;
}
long UnixTime = 1591107846;
String dateTime = getDateTimeString(UnixTime);
Serial.println(dateTime);
}
void loop() {
// put your main code here, to run repeatedly:
}
// Output: 2020-06-02 - 14:24:06
// Sketch: 7328 bytes on Arduino Uno
// Global vars: 330 bytes on Arduino Uno
// Variant 2, using time.h and char
#include <time.h>
#include <locale.h>
// Function to convert a UnixTime value to a formatted Char using time.h
char * getDateTimeChar( long ut ) {
time_t t = ut - UNIX_OFFSET;
struct tm * tm = localtime(&t);
static char dtc[22];
strftime(dtc, sizeof(dtc), "%F - %T", tm);
return dtc;
}
void setup(void) {
Serial.begin(57600);
while (!Serial) {
;
}
long UnixTime = 1591107846;
char * dateTime = getDateTimeChar(UnixTime);
Serial.println(dateTime);
}
void loop() {
// put your main code here, to run repeatedly:
}
// Output: 2020-06-02 - 14:24:06
// Sketch: 6284 bytes on Arduino Uno
// Global vars: 342 bytes on Arduino Uno
// Variant 3, using time.h and char, and isotime()
#include <time.h>
#include <locale.h>
// Function to convert a UnixTime value to a formatted ISO-Time string using time.h
char * getIsoTimeChar(long ut) {
time_t t = ut - UNIX_OFFSET;
struct tm * tm = localtime(&t);
char * it = isotime(tm);
return it;
}
void setup(void) {
Serial.begin(57600);
while (!Serial) {
;
}
long UnixTime = 1591107846;
char * dateTime = getIsoTimeChar(UnixTime);
Serial.println(dateTime);
}
void loop() {
// put your main code here, to run repeatedly:
}
// Output: 2020-06-02 14:24:06
// Sketch: 2642 bytes on Arduino Uno
// Global vars: 234 bytes on Arduino Uno
Especially variant 3 is interesting due to the low resources and line count, but a minor disadvantage in my case is that the intermediate dash is missing in the ISO string.
But for using the time.h library I had to include it together with the locale.h as extra includes in my current project. And because I am already running low on resources of my Arduino Mega 2560 with my current project, which already is using the TimeLib.h library for some other applications, I decided to have another go with the TimeLib.h library instead.
And again after a lot of trial and error I finished with the following additional two variants:
// Variant 4, using TimeLib.h and String
#include <TimeLib.h>
// Function to always return Two Digits for Date and Time using String
String twoDigits(String digits) {
if ( digits.length() < 2 ) {
digits = "0" + digits;
}
return digits;
}
// Function to convert a UnixTime value to a formatted String using TimeLib.h
String getDateTimeString( time_t ut ) {
String dts =
String(year(ut)) +
"-" +
twoDigits(String(month(ut))) +
"-" +
twoDigits(String(day(ut))) +
" - " +
twoDigits(String(hour(ut))) +
":" +
twoDigits(String(minute(ut))) +
":" +
twoDigits(String(second(ut)));
return dts;
}
void setup(void) {
Serial.begin(57600);
while (!Serial) {
;
}
long UnixTime = 1591107846;
String dateTime = getDateTimeString( UnixTime );
Serial.println( dateTime );
}
void loop() {
// put your main code here, to run repeatedly:
}
// Output: 2020-06-02 - 14:24:06
// Sketch: 4354 bytes on Arduino Uno
// Global vars: 231 bytes on Arduino Uno
// Variant 5, using TimeLib.h and char
#include <TimeLib.h>
// Function to always return Two Digits for Date and Time using Char
char * twoDigitsChar(int digits) {
static char digitsChar[3];
char nul[2];
char inpt[3];
strcpy(nul, "0");
itoa(digits, inpt, 10 );
if ( digits < 10 ) {
strcat( nul, inpt );
strcpy(digitsChar, nul);
} else {
strcpy(digitsChar, inpt);
}
return digitsChar;
}
// Function to convert a UnixTime value to a formatted Char using TimeLib.h
char * getDateTimeChar( time_t ut ) {
static char dtc[22];
char yrc[5];
strcat( dtc, itoa(year(ut), yrc, 10) );
strcat( dtc, "-" );
strcat( dtc, twoDigitsChar(month(ut)) );
strcat( dtc, "-" );
strcat( dtc, twoDigitsChar(day(ut)) );
strcat( dtc, " - " );
strcat( dtc, twoDigitsChar(hour(ut)) );
strcat( dtc, ":" );
strcat( dtc, twoDigitsChar(minute(ut)) );
strcat( dtc, ":" );
strcat( dtc, twoDigitsChar(second(ut)) );
return dtc;
}
void setup(void) {
Serial.begin(57600);
while (!Serial) {
;
}
long UnixTime = 1591107846;
char * dateTimeChar = getDateTimeChar( UnixTime );
Serial.println( dateTimeChar );
}
void loop() {
// put your main code here, to run repeatedly:
}
// Output: 2020-06-02 - 14:24:06
// Sketch: 2454 bytes on Arduino Uno
// Global vars: 246 bytes on Arduino Uno
It is clear from these examples that I needed an extra function to assure having always two digits for month, day, hour etc in the resulting output, because the standard TimeLib.h output does not provide this as far as I can see it.
So it is clear from these examples that for using the TimeLib.h library I need a lot more lines of code. But it looks like this does not have a negative effect on the finally required resources?
In the end all five variants work for me, but since I consider myself still to be a novice in the Arduino world, I would very much appreciate to get any comments on this.
Please let me know any improvements that you might have, and which variant is best, especially resource-wise. Or you might even have another better variant?
My current take is that variant 5 is the best choice because it needs the least resources and does not need extra includes in my case. Do you agree?
Cheers,
Ed