Hi,
I am using the Time.h-library and try to convert from a given time-input(hh:mm:ss DD:MM:YYYY) to the time_t-format. How could I do it?
additional question: Is the time_t-format the same as unix-format (if UTC)?
Thanks
but why does not this one go wrong:
setTime(10,00,00,19,05,2011);
Yes, same as Unix time. Here is a somewhat crude but effective example.
void readCommand()
{
//Read command from the Arduino serial monitor to set the RTC.
//Case-sensitive and must be entered exactly as (24-hour clock):
// Set yyyy-mm-dd hh:mm:ss
char cmd[32]="Set yyyy-mm-dd hh:mm:ss";
int i;
tmElements_t tmSet;
time_t tSet;
if (Serial.available() > 0) { //anything there?
i = 0; //yes, read the available characters
while (Serial.available() > 0) {
cmd[i++] = char(Serial.read());
}
cmd[i] = 0x00; //put in string terminator
if (strncmp(cmd, "Set ", 4) == 0) {
tmSet.Year = 1000 * (cmd[4] - '0') + 100 * (cmd[5] - '0') + 10 * (cmd[6] - '0') + cmd[7] - '0' - 1970;
tmSet.Month = 10 * (cmd[9] - '0') + cmd[10] - '0';
tmSet.Day = 10 * (cmd[12] - '0') + cmd[13] - '0';
tmSet.Hour = 10 * (cmd[15] - '0') + cmd[16] - '0';
tmSet.Minute = 10 * (cmd[18] - '0') + cmd[19] - '0';
tmSet.Second = 10 * (cmd[21] - '0') + cmd[22] - '0';
tSet = makeTime(tmSet); //convert to time_t
setTime(tSet); //set the local time
breakTime(now(), tmSet); //convert the current local time back to tmElements_t (to ensure weekday is calculated)
RTC.write(tmSet); //set the rtc
Serial.println("RTC set!");
}
}
}
system
May 18, 2011, 11:46am
3
I thought, that there had to be a function for that, but just could not find it.
putting it in a function would look like this - right?
time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss
{
tmElements_t tmSet;
tmSet.Year = YYYY - 1970;
tmSet.Month = MM;
tmSet.Day = DD;
tmSet.Hour = hh;
tmSet.Minute = mm;
tmSet.Second = ss;
return makeTime(tmSet); //convert to time_t
}
Yep, looks like you're barking up the right tree XD
KevinT:
I thought, that there had to be a function for that, but just could not find it.
Browsing through the library files (e.g. Time.cpp, Time.h, DateStrings.cpp, Readme.txt) is often a fairly instructive exercise XD
Function does not work. Why?
tried:
#include <Time.h>
time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss)
{
tmElements_t tmSet;
tmSet.Year = YYYY - 1970;
tmSet.Month = MM;
tmSet.Day = DD;
tmSet.Hour = hh;
tmSet.Minute = mm;
tmSet.Second = ss;
return makeTime(tmSet); //convert to time_t
}
void setup()
{
time_t s_tm = tmConvert_t(2000,01,01,09,59,50); // ~10:00 Uhr
}
void loop()
{
}
error: invalid digit "9" in octal constant
system
May 19, 2011, 11:01am
7
Function does not work. Why?
How do you know the function doesn't work? You are calling it incorrectly.
You have defined the function to take several arguments, all of which require passing numerical (not character) values.
When you prefix a numerical value with certain prefixes, the compiler expects certain digits in the remaining string of digits. The prefixes that it cares about are B, b, 0, and 0x. The 0 tells the compiler that you are going to provide an octal value ( base 8 ), meaning that all the digits in the number must be between 0 and 7. 9 is not between 0 and 7, so the compiler complains (really should be called a complainer instead, a lot of the time).
void setup()
{
time_t s_tm = tmConvert_t(2000,1,1,9,59,50);
}
capedra
November 11, 2016, 3:34am
9
If you guys would like to convert any given date and time to epoch/timestamp using an ESP8266 or NODEMCU board, DO NOT forget to include TimeLib.h at the beginning of the sketch. So, here's an example:
#include <time.h>
#include <TimeLib.h>
time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss)
{
tmElements_t tmSet;
tmSet.Year = YYYY - 1970;
tmSet.Month = MM;
tmSet.Day = DD;
tmSet.Hour = hh;
tmSet.Minute = mm;
tmSet.Second = ss;
return makeTime(tmSet);
}
void setup()
{
Serial.begin(9600);
time_t s_tm = tmConvert_t(2016,11,15,0,0,0);
Serial.println((unsigned long) s_tm);
}
void loop()
{
}
To anybody that wants to reply
Be aware that this thread is over 5 years old.