I have a file which contains data matched to a unix timestamp. I would like to read this timestamp, then display it in a human readable form. Unfortunately all of the examples I can find deal with displaying time that is already in a time_t variable or tm struct, not going from a timestamp string read from a file to something human readable.
I have had a dig through the time library and I don't see anything that would make this a simple process.
Am I missing something, or is there no help for it and I'll have to write a function to convert from the timestamp into a tm struct then a time_t variable myself?
Unfortunately all of the examples I can find deal with displaying time that is already in a time_t variable
A time_t variable is a unix time. You just need to convert your string to a time_t which is really nothing more than an unsigned long. strtoul() will do that, IF you have a string.
This works, am I setting myself up for a fall using an int in some possible scenario?
No, since toInt() is a stupid name for a function that does not convert a String to an int. What is does is convert a String instance to a long. Why they chose a misleading name is one of the mysteries of life that will never be explained.
Well, it seems I celebrated too early... I am not getting the results I expected trying to display the time_t variable.
Example code:
#include <Time.h>
#include <Console.h>
void setup() {
Serial.begin(9600);
Bridge.begin();
Console.begin();
}
void loop() {
String l_line = "1468268346";
time_t t = l_line.toInt();
Console.println("The following line should be the weekday + hour:");
Console.println(weekday(t) + " " + hour(t));
Console.println("The following line should be the weekday:");
Console.println(weekday(t));
Console.println("The following line should be the hour:");
Console.println(hour(t));
Console.println("The following line should be the hour + minute:");
Console.println(hour(t) + ":" + minute(t));
Console.println("The following line should be the minute:");
Console.println(minute(t));
delay(10000);
}
This gives me the output below:
The following line should be the weekday + hour:
hould be the weekday:
The following line should be the weekday:
2
The following line should be the hour:
20
The following line should be the hour + minute:
te:
The following line should be the minute:
19
Moving code around a bit seems to change what gets spat out when I try and print portions of the time. In my full program it was even printing out some portions of text from a console.print() 100 lines below these lines...
What is it actually doing to end up spitting out bits of text from elsewhere in the code?
C and C++ are pass by value languages, where possible. Pass by reference (address) is used when a value can not be passed. The address of the string literal is added to, and passed to the print() method. You have no control over all of memory, so passing invalid pointers around isn't really a good idea.