# Calculating epoch [Solved]

Hi,

I’m trying to calculate a time in Unix format using the code below but, for some reasons, it’s not working. The code returns an incorrect date.

Any help is appreciated.
TIA

``````void setup() {
Serial.begin(115200);
Serial.println();

//https://stackoverflow.com/questions/21975472/how-to-calculate-epoch-day
// date march, 2, 2017 or 1488420000

int tm_sec = 0;
int tm_min = 0;
int tm_hour = 2;
int tm_yday = 61;
int tm_year = 2017;

int tz;

tz = tm_sec + tm_min * 60 + tm_hour * 3600 + tm_yday * 86400 +
(tm_year - 70) * 31536000 + ((tm_year - 69) / 4) * 86400 -
((tm_year - 1) / 100) * 86400 + ((tm_year + 299) / 400) * 86400;

Serial.println(tz);

// returns 1317108256 instead of 1488420000 for a date of 3/2/2017 2:0:0
}

void loop() {

}
``````

I don't think 'int' is going to be big enough to hold tz.

@evanmars You're right. However, the change to unsigned int tz didn't help unsigned long?

no it didn't help either. I remind you that the code is returning a 10 digit number.

Unsigned long can hold 10 digits but only to 4,294,967,295. If you don't go too far in the future, you should be OK

@jbellavance it didn't help. perhaps the formula is wrong. I got it from here

ebolisa:
@jbellavance it didn't help. perhaps the formula is wrong. I got it from here

What do you get if you enter it into a calculator?

ebolisa:

``````  //https://stackoverflow.com/questions/21975472/how-to-calculate-epoch-day
``````

// date march, 2, 2017 or 1488420000

int tm_sec = 0;
int tm_min = 0;
int tm_hour = 2;
int tm_yday = 61;
int tm_year = 2017;

Look again at the information in the link you provided.
For the year 2017, `tm_year` should be 2017 minus 1900, which equals 117.
The numbering for `tm_yday` counts January 1 as day 0. For your example date (March 2 of a non-leap year), we count 31 days from Jan. 1 to get to Feb. 1, then another 28 days to get to March 1, then finally 1 more day to get to March 2. We add up 31+28+1 to get 60, thus `tm_yday` should be 60 for March 2, 2017.

@odometer that was my error. Thank you much.