Convert time to epoch

Hi, I wrote this code and it should be convert time to epoch (seconds since 1970), but don’t >:(

#include <time.h>

void setup() {


  Serial.begin(9600);


  
  struct tm time_now_tm;
  time_now_tm.tm_mon = 1970 - 1900; //time since 1900
  time_now_tm.tm_mday = 1 - 1; // months sice January
  time_now_tm.tm_hour = 1; // hourse
  time_now_tm.tm_min = 0; // minutes
  time_now_tm.tm_sec = 0; // seconds
  time_t time_now_epoch = mktime(&time_now_tm);
  Serial.print("Seconds since 1900: ");
  Serial.println(time_now_epoch);
}

void loop() {

}

It returns 1260170896 (Mon, 07 Dec 2009 07:28:16 GMT) What Am I doing wrong?

chromrade:
It returns 1260170896 (Mon, 07 Dec 2009 07:28:16 GMT) What Am I doing wrong?

I do it like this:

#include <Time.h>

void setup()
{
  Serial.begin(9600);
  time_t myTimeStamp = tmConvert_t(2016,4,15,7,30,00);
  Serial.println(myTimeStamp);
}

void loop()
{
}

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); 
}

What Am I doing wrong?

Just about everything.

What do you think this does?

time_now_tm.tm_mon = 1970 - 1900; //time since 1900

I’m sorry :slight_smile:

But when I have this:

#include <time.h>

void setup() {


  Serial.begin(9600);


  
  struct tm time_now_tm; // nastavim si pomocne datum na aktualni
  time_now_tm.tm_year = 1970 - 1900; //time since 1900
  time_now_tm.tm_mon = 1 - 1; // months sice January
  time_now_tm.tm_mday = 1; //day of the month
  time_now_tm.tm_hour = 1; // hours
  time_now_tm.tm_min = 0; // minutes
  time_now_tm.tm_sec = 0; // seconds
  time_t time_now_epoch = mktime(&time_now_tm);
  Serial.print("Seconds since 1970: ");
  Serial.println(time_now_epoch);
}

void loop() {
  // put your main code here, to run repeatedly:

}

It returns 3348372495 (what is Sat, 08 Feb 2076 07:28:15 GMT), I don’t get it.

chromrade:
I don’t get it.

your code compiles?

#include <time.h>

??

Yes, but I didn’t considered it weird until now. Now I can see that is AVR_Standard_C_Time_Library downloaded library. So perhaps bug there. Thanks to all :slight_smile: