Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
preston

Ctime - Handling Dates As Far Back As Imaginable

Recommended Posts

Hi

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

mktime(timeinfo);

 

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

Share this post


Link to post
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......

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Hi

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 :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...