Hello, my name is Pablo. I have a problem with the following code:
unsigned int sgs=3531955673; //seconds
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
int y=sgs/31556926;
int y_aux=(sgs%31556926);
int mes=(y_aux/2629743);
int mes_aux=(y_aux%2629743);
int d = (mes_aux/86400);
int d_aux = (mes_aux%86400);
int h = d_aux/3600;
int h_aux = d_aux%3600;
int m = h_aux/60;
int m_aux = h_aux%60;
int s = h_aux/60;
Serial.println(String(h)+":"+String(m)+":"+String(s)+" "+String(d)+":"+String(mes)+":"+String(y));
Serial.println(y_aux);
Serial.println(y);
sgs=sgs+5;
delay(5000);
}
It´s suppose has to convert the seconds (sgs) in a date with format Seconds:Minutes:Hour Day:Month:Year, but when I run the programme the result is:
pablodg92:
I saw that and I changed the type to "unsigned long", and now I solve this part, but the code isn´t running how it´d be, the result now is:
16:3:3 1:11:111
29071378
111
PaulS brought a problem to your attention. You fixed it in one place, but not in others.
The most important thing to remember about programming is this: The computer will not do what you mean it to do. It will do what you tell it to do.
If you tell the computer to use an int, it will use an int.
If you need a long, then tell the computer to use a long, and it will use a long.
If you need some int variables and some long variables, then you must specify exactly which variables are to be ints and which are to be longs.
#include <TimeLib.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
time_t t = 3531955673UL; //Unix time assumed
setTime(t);
Serial.print("Date: ");
Serial.print(day());
Serial.print("/");
Serial.print(month());
Serial.print("/");
Serial.println(year()); //prints Date: 3/12/2081
}
void loop() {
// put your main code here, to run repeatedly:
}
Hello! I have a problem trying to convert seconds to date. I don´t want to use a RTC in this moment , soy Im using the function setTime from the Timelib.h to get the date manually. I´m using this programme to get the date and the most of the time run fine:
unsigned long y=sgs/ye; //in this lines convert seconds to date sgs=seconds since 2015
unsigned long y_aux=(sgs%ye);
unsigned long mes=(y_aux/me);
unsigned long mes_aux=(y_aux%me);
unsigned long d = (month_aux/da);
unsigned long d_aux = (mes_aux%da);
unsigned long h = d_aux/ho;
unsigned long h_aux = d_aux%ho;
unsigned long m = h_aux/mi_se;
unsigned long s = h_aux%mi_se;
unsigned long y_now=y+2016; //I had 2016 because I started to count the seconds from 01/01/2015
//at 00:00:00
unsigned long month_now=month+1;
unsigned long d_now=d+1;
The problem is I get some dates to convert year and month to seconds, and it´s just a mean, so the seconds I´m using in the programme per month are 2629743. For 30 days months there is no problem, but with 31 days months I had a problem. Really I just had the problem with the day 31 in every month.
An example:
The seconds I get from the day 30/12/2035 at 23:50:30 are 631100197 and the conversion go well, but with the 31/12/2035 at 23:50:30 the conversion return me 631186597 seconds, and the conversion to date is 13:21:17 1/1/2036
Someone knows how to solve this problem?
Thanks!!!
unsigned long sgs=631186597; //seconds since 01/01/2015 at 00:00:00
unsigned long ye=31556926; //seconds per year
unsigned long me=2629743; //seconds per month
unsigned long da=86400; //seconds per day
unsigned long ho=3600; //seconds per hour
unsigned long mi_se=60; //seconds per minute
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// int y= (sgs/31556926);
// int mes=(sgs/2629743)%86400;
//int d = (sgs/86400)%3600;
//unsigned int h = sgs / 3600;
//unsigned int m = (sgs % 3600) / 60;
//unsigned int s = sgs % 60;
unsigned long y=sgs/ye; //en estas lineas convierte los segundos a la hora
unsigned long y_aux=(sgs%ye);
unsigned long mes=(y_aux/me);
unsigned long mes_aux=(y_aux%me);
unsigned long d = (mes_aux/da);
unsigned long d_aux = (mes_aux%da);
unsigned long h = d_aux/ho;
unsigned long h_aux = d_aux%ho;
unsigned long m = h_aux/mi_se;
unsigned long s = h_aux%mi_se;
unsigned long y_now=y+2016; //se le suma 2016 a la fecha porque aunque el tiempo se cuenta desde 2015, en 2018 no han pasado 3 años, sino que 2
unsigned long mes_now=mes+1; //igual que año
unsigned long d_now=d+1; //igual que año
pablodg92:
The problem is I get some dates to convert year and month to seconds, and it´s just a mean, so the seconds I´m using in the programme per month are 2629743. For 30 days months there is no problem, but with 31 days months I had a problem. Really I just had the problem with the day 31 in every month.
That is what happens when you use 30-day months. There will never be a "31st day". The day after the 30th of the month will always be the 1st of the next month. And there will be five days at the end of the year that are in the 13th month.