Jump to content
Larry Ullman's Book Forums

Ctime - Handling Dates As Far Back As Imaginable

Recommended Posts


I'm wondering about the limitations of ctime and mktime.

For example:

time_t t;

struct tm * timeinfo;

string weekday[] = { "Sunday", "Monday",

"Tuesday", "Wednesday",

"Thursday", "Friday", "Saturday"};

// get current timeinfo and modify it to user's choice

t = time(NULL);

timeinfo = localtime(&t);

timeinfo->tm_year = local_year - 1900;

timeinfo->tm_mon = local_month - 1;

timeinfo->tm_mday = local_day;


// call mktime: timeinfo->tm_wday will be set



cout << "That day is a " << weekday[timeinfo->tm_wday] << endl;


I am trying to determine the day of ANY date. Ideally, I would like it to be able to handle dates as far back as 5000 BC (if that is even possible) or year -5000....if not then maybe atleast 100 AD........


Am I asking C++ to do something it can't handle any more?

How do I do what I described above in C++?

What's the earliest date that ctime can handle? Is it only from 1970???


Thank you

Link to comment
Share on other sites

I just tested the above code...it only works properly from 1902 onwards...

so now the question is...

is there a convenient way for me to be able to determine the day of a particular date before 1902?

or do I have to write that class myself?

It's gonna be tricky......

Link to comment
Share on other sites

This is very tricky. You'll have to check online for the details, but around 1100, 10 days were added to the calendar and we switched from Julian to Gregorian calendars (or vice versa). And I don't know when leap days were introduced. It would kind of surprise me if there isn't a C++ library that handles this (to some degree) already.

Link to comment
Share on other sites


Yes...I will check....for now I went with using NSDate and NSDateComponenets of Objective-C for the iOS version of the app....but with the Android version, I'll have to research more about other C++ libraries if I wanna reuse my C++ classes.....

Thank you very much :)

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...