Epoch time is incorrect for dates prior to 1970?

I have a problem where I am trying to parse dates and convert them to Unix time stamps using Time library.

When I test the date [1-1-1970 0:0:00] I get the expected result of 0.
When I test the date [1-1-1969 0:0:00] the expected result is "-31536000" but I get "3752069504".

Any ideas why this is happening?

#include <Time.h>

int Year = 1969;
int Month = 1;
int Day = 1;

time_t getEpoch(){
  tmElements_t tm;
  tm.Second = 0;
  tm.Hour = 0;
  tm.Minute = 0;
  tm.Day = Day;
  tm.Month = Month;
  tm.Year = Year - 1970;
  return makeTime(tm);
}

void setup(){
  Serial.begin(9600);
}

void loop () {
  time_t epoch = getEpoch();
  Serial.println( epoch ); //prints "3752069504" where it should be "-31536000", Doc. Brown would not be pleased.
  delay(1000);
}

Thanks.

The epoch is an unsigned long. No negative numbers with unsigned.

Apparently negative numbers are ok to use as epoch time stamps and they just indicate dates earlier than Jan 1st 1970. However as suferTim pointed out the range I need will not fit into a single unsigned long. and using a signed long would only allow for dates up to 2038.

So I have improvised by using a "sign" variable and just calculating the absolute difference from 1970:

#include <Time.h>

int Year = 1969;
int Month = 1;
int Day = 1;

int sign;
unsigned long epoch;

void getEpoch(){
  tmElements_t tm;
  tm.Second = 0;
  tm.Hour = 0;
  tm.Minute = 0;
  tm.Day = Day;
  tm.Month = Month;
  tm.Year = abs(Year - 1970);

  if (Year < 1970) {
    sign = -1;
  } else {
    sign = 1;
  }
  
  epoch = makeTime(tm);
}

void setup(){
  Serial.begin(9600);
}

void loop () {
  getEpoch();
  
  Serial.print(sign); //prints "-1"
  Serial.print(" ");
  Serial.println(epoch); //prints "31536000"
  delay(1000);
}

SurferTim:
The epoch is an unsigned long. No negative numbers with unsigned.

That's how the Arduino implementation of Time library does it. NBut this is NOT general standard.

Originally Unix time is based on signed(!) 32-bit data type('long'). I think the programmers of the original reference implementation wanted to be able to use Unix time stamps for their own birthday (date perhaps somewhere in the mid 1940s.

This is perfectly possible with 32-bit signed Unix time.

But at some point somebody recognized that a signed variable will overflow on January 19th, 2038.

Therefore people decided to circumvent the Y2K38 problem wth existing Unix time and did newer implementations.

As 64-bit processors and 64-bit operating systems became more and more popularduring the 200s, most programmers changed their variables from 32-bit signed to 64-bit signed, still being compatible with dates before 1/1/1970.

But the programmer of the Arduino Time library (which is to be run on 8-bit microcontrollers did a different decision and decided to use 32-bit unsigned instead 32-bit signed.

So in that implementazion of Arduino Time library you cannot have dates before 01/01/1970.