Thread: Date

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Date

    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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dates in C++ is horribly messed up.
    Try using something like Boost.Date. It has its flaws, but it's better than the current date implementation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    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.
    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps the deficiency is because the C dates are horribly messed up in the first place so the dev wasn't aware?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    because C's dates starts at 1950 or something
    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.

    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.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by bithub View Post
    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.
    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?
    Last edited by Elysia; 07-20-2009 at 12:09 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

    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.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    And once it has been converted into a tm struct, then what? It's not finished there.
    Why not? tm is a representation of the date in human readable form. You would know that if...

    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.
    Since Y2K a different field has been introduced for the full year: You were probably using the legacy field.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 08:35 AM