Date

This is a discussion on Date within the C++ Programming forums, part of the General Programming Boards category; I get the date in a string format,after the user makes selection in activex control in vc++... Is there a ...

  1. #1
    dpp
    dpp is offline
    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,246
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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,246
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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,246
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    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.

Page 1 of 2 12 LastLast
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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21