Hello,
one thing I am working on right now as part of my still ongoing Arduino trip computer project is converting a given Unix timestamp on the ESP32 into a specific date and time.
I would like to translate a Unix timestamp into a char array like so, for example:
Apr 09 - 21:22
I've read up on how to retrieve day, hour and minutes from a timestamp using day(), month(), hour() and minute(), but I'm not quite sure yet what's the best way to turn a numeric month into a three-letter month. Does the time library have any functions for that?
Here's my code so far, at the moment the time is printed out via serial, when I'm done with it, as I said, I would like to have it all in a char array because I will need that char array to print actual text on my TFT display using my bespoke font libraries.
#include <Time.h>
#include <TimeLib.h>
unsigned long lastTime = millis();
unsigned long timePassed;
byte i = 0;
//Creating union
union unixTime {
long unixTimeValue;
byte unixByteNr[4];
} unixBytes;
void setup() {
Serial.begin(115200);
Serial.println("Ready. Requesting UNIX timestamp.");
}
void loop() {
//Reading UNIX time input from serial
while (Serial.available()) {
//Populating array with received serial bytes
unixBytes.unixByteNr[i] = Serial.read();
i++;
if (i == 3) {
unsigned long unixTimestamp = unixBytes.unixTimeValue;
//Synchronizing system time with the received timestamp
setSyncProvider(unixTimestamp);
i = 0;
}
}
if ((timePassed = millis() - lastTime) >= 1000) {
Serial.print("Time: ");
Serial.print(year());
Serial.print("-");
Serial.print(month());
Serial.print("-");
Serial.print(day());
Serial.print(" -- ");
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.println(second());
lastTime = millis();
}
}
Right now, the code doesn't fully compile, I'm not quite sure at first glance what I have done to it that it doesn't, because it was working fine before I made the last changes and then unsuccessfully tried to revert to a previous stable version.
So what is the best way to go from months expressed in numbers from 1 to 12 to the months as three letter words?