Gopal
Gopal

Reputation: 797

incorrect epoch unix time value for larger year (2048)

i need to get the epoch time for the date and time enter, but when i enter like 2048(year), i am getting very large value, "18446744073709551615", which is supposed to be incorrect..

when i enter the date like 2012(year), 2015(year), it epoch value is correct, any changes i need to do for the 2048 (year)

time_t get_unix_time(int,int,int,int,int,int,int);
int main()
{
unsigned long long m_process_date;
m_process_date = get_unix_time (12,31,2048,23,59,58,-1);
std::cout<<"\n m_process_date:"<< m_process_date<<std::endl;
return 1;
}


time_t get_unix_time(   int         month,
                        int         day,
                        int         year,
                        int         hour,
                        int         minute,
                        int         second,
                        int         dst )
{
    tm          ts;

    ts.tm_mon = month - 1;
    ts.tm_mday = day;

    if( year < 100 )
        ts.tm_year = year + 100;
    else
        ts.tm_year = year - 1900;

    ts.tm_hour = hour;
    ts.tm_min = minute;
    ts.tm_sec = second;
    ts.tm_isdst = dst;

    return mktime( &ts );
}

Upvotes: 1

Views: 745

Answers (1)

Dacto
Dacto

Reputation: 2911

The standard Unix time is stored in a signed int. This is normal 32-bits but may differ depending on implementation (some new implementations store time_t as signed 64-bit int).

Thus, for a 32-bit signed int, this means the maximum representable date would be Jan 19, 2038.

Upvotes: 2

Related Questions