Since it looks like you're using Windows (I'm inferring it from TCHAR), you probably want to use FileTimeToSystemTime()
. It works with years since 1601.
Example:
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
SYSTEMTIME sysTime;
FILETIME fileTime;
long long seconds;
sysTime.wYear = 1896;
sysTime.wMonth = 4;
sysTime.wDayOfWeek = 0;
sysTime.wDay = 15;
sysTime.wHour = 0;
sysTime.wMinute = 0;
sysTime.wSecond = 0;
sysTime.wMilliseconds = 0;
if (SystemTimeToFileTime(&sysTime, &fileTime))
{
seconds = *(long long*)&fileTime;
seconds /= 10000000; // 100-nanoseconds to seconds since Jan 1st 1601
seconds -= 11644473600; // 1601 to 1970
printf("%d.%d.%d is %lld seconds from Jan 1st 1970\n",
sysTime.wDay,
sysTime.wMonth,
sysTime.wYear,
seconds);
}
else
{
printf("SystemTimeToFileTime() failed with error 0x%X\n", GetLastError());
}
// Now, convert it back...
seconds += 11644473600; // 1970 to 1601
seconds *= 10000000; // seconds since Jan 1st 1601 to 100-nanoseconds
*(long long*)&fileTime = seconds;
memset(&sysTime, 0, sizeof(sysTime));
if (FileTimeToSystemTime(&fileTime, &sysTime))
{
seconds /= 10000000; // 100-nanoseconds to seconds since Jan 1st 1601
seconds -= 11644473600; // 1601 to 1970
printf("%lld seconds from Jan 1st 1970 is %d.%d.%d\n",
seconds,
sysTime.wDay,
sysTime.wMonth,
sysTime.wYear);
}
else
{
printf("FileTimeToSystemTime() failed with error 0x%X\n", GetLastError());
}
return 0;
}
Output:
15.4.1896 is -2326147200 seconds from Jan 1st 1970
-2326147200 seconds from Jan 1st 1970 is 15.4.1896
A rough estimation (1896-1970+3.5/12)*365.2425*24*3600 gives -2326010337. So, we're good.
EDIT:
If you want a DIY solution without involving any OS- or compiler-specific functions, use my seconds to date converter from this answer.
This snippet shows how to use it:
struct tm t;
SecondsSinceEpochToDateTime(&t, -2326924800LL);
printf("-2326924800 is %d.%d.%d %d:%d:%d\n",
t.tm_mday,
t.tm_mon + 1,
t.tm_year + 1900,
t.tm_hour,
t.tm_min,
t.tm_sec);
And this is the output of it:
-2326924800 is 6.4.1896 0:0:0
Yeah, -2326924800 is not 15.4.1896 by the Gregorian calendar. It's 6.4.