I get the date in a string format,after the user makes selection in activex control in vc++...
Is there a function to calculate the difference between two dates..In vb yes i know there is a function called DateDiff.()
Is there anything like that
I get the date in a string format,after the user makes selection in activex control in vc++...
Is there a function to calculate the difference between two dates..In vb yes i know there is a function called DateDiff.()
Is there anything like that
Convert the date from a string format to an integer format. The easiest way would be to use a function like strptime() to convert the date, then it's just a matter of doing some subtraction.
Well I don't think "horribly messed up" is the way to put it: More like any date functionality comes from C. Boost.Date is a great suggestion though.
Yes, the fact that C++ only uses the C date/time functionality makes it a mess.
There are so many different date/time formats, and functions for converting between them and comparing them and whatnot, and at many times, there's just no easy way to convert a date from one format to another, and to make things worse, the functionality in the standard library usually takes a date/time in a specific format.
That is why it's such a horrible mess. There's just no easy way for programmers to program using dates in C/C++. Boost.Date solves that somewhat, thankfully.
My current project does quite a bit of date/time manipulation using the functions in time.h. Once you get a solid understanding of what function does what, it's not that bad. I've never used Boost.Date before, so I can't comment on that though.
Well, everything the OP needs is in the ctime header though. Contrary to what you say, there are functions for converting between the two types you will use more often, time_t and the tm structure. difftime is the time comparison function. The only trouble is that it returns a double type: the difference expressed in seconds.
Not the simplest of affairs maybe, but the trouble with a standard library is that it must conform to very general needs. In example, the return type of difftime may seem odd at first since time_t or tm might be more useful, but really it's the correct thing to do. In the difference lies the exact days, hours, minutes, and remaining seconds of what might not be a meaningful date.
Last edited by whiteflags; 07-20-2009 at 11:22 AM.
I had huge troubles with the time/date when trying to use it.
Boost.Filesystem returned one type of date.
Then The ftp code I used returned another type of date. And to make it worse, that date was malformed, because C's dates starts at 1950 or something, and it was setting the year to 2009, which really should be like 59, and C starts counting months from 0, but that code didn't, of course...
Regardless, however I turned and streches the dates, I had to spend eternity to be able to convert the dates into the same format so I could compare them. It was truly a nightmare.
Boost.Date was much easier, but it lacks support for wide strings, which is really a shame, since that made it a non-solution for me.
The point is, the date/time works. Sure. But it sure isn't easy or trivial. It's a mess.
With C++ support, classes or even functions could be overloaded to accept every type of date/time to easily add days, years, months, compare days, etc.
But there is no such support. Boost.Date adds it.
To me it sounds like a profound lack of planning on your part than any deficiency with one particular library. You're blaming your tools for different conventions that you should have made yourself aware of.
Jan 1, 1970. This is not a C thing. This epoch date is used by Linux, BSD, Java, PHP, most (maybe all) database systems, OSX, javascript, UNIX, etc.because C's dates starts at 1950 or something
I agree with whiteflags. It sounds like your problem is that the ftp code you were using was written by a bad programmer. This has nothing to do with C's date/time functions, and everything to do with interfacing with poorly written code.
But for crying out loud, how difficult is a conversion?
If you have a struct with years, months, etc lined out, you would expect them to be the REAL dates, not some subtracted dates.
And that the documentation is poor doesn't help.
In fact, it's not really often that you would manually create dates, especially due to the difficulty.
And one function for every thing just makes it a mess.
Bad code or no, sorry, I just do not agree with you.
Also, consider the usability issue. From the viewpoint of usability, it is indeed a mess.
For C, usability was not a primary concern, so I understand why it is as it is, and it is fine.
However, for C++, it is not acceptable since it offers so much more capabilities to make it better. It's not quality C++ code.
So if we combine those two, C++ and usability, the result is a mess. That is my opinion.
Also, I wonder if there are any functions for creating dates in C?
Re-read the thread: Someone mentioned the function that does it when the input is a C-string. If you have a tm and need a time_t, then mktime usually works.Also, I wonder if there are any functions for creating dates in C?
Although you may have to adjust your date to the user's locale with one of the other functions (you either want localtime or gmtime). Daylight Savings Time is another issue to consider.
And once it has been converted into a tm struct, then what? It's not finished there. Yet, Boost.Date will provide a 100% solution to the problem if the OP gets acquainted with it. Or he/she can search for additional C functions that compares C dates.
You bring up more issues into the pot, which complicates the time/date facilities yet more. It's simply a mess.
Why not? tm is a representation of the date in human readable form. You would know that if...
Since Y2K a different field has been introduced for the full year: You were probably using the legacy field.But for crying out loud, how difficult is a conversion?
If you have a struct with years, months, etc lined out, you would expect them to be the REAL dates, not some subtracted dates.