getting the next day's day, month, year etc

This is a discussion on getting the next day's day, month, year etc within the C Programming forums, part of the General Programming Boards category; Hello, well, I know that you can get the current time with time() function, but after you get the tm ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    getting the next day's day, month, year etc

    Hello,

    well, I know that you can get the current time with time() function, but after you get the tm structure, how do you easily get the days, months etc for the next day?

    mktime function adjusts based on the day of the month supplied, etc, not based on the amount of days after the beginning of the year etc..

    Is the only possible way to get the next day of a date, by getting the time_t value (seconds after the unix date on 1970), adding 86400 (24 hours x 60 minutes x 60 seconds), and converting the number back to the tm structure?

    It just seems a bit hacky, please tell me there's a more elegant way

    also, while I'm at it.. does the time.h header support things such as leap years, leap seconds (even) etc ?

    cheers

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > how do you easily get the days, months etc for the next day?
    Just do tm_day++;
    then call mktime() to normalise it. mktime() will automatically adjust day 32 to be day 1 and roll forward the month / year as appropriate.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    champ! thanks for that tip!

    I presume this works for seconds, hours etc as well?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    It does
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  3. day of week
    By s_ny33 in forum C Programming
    Replies: 18
    Last Post: 11-02-2005, 10:56 AM
  4. Counting days
    By TheShaggmeister in forum C++ Programming
    Replies: 5
    Last Post: 07-17-2003, 10:29 AM
  5. need current day, month, year
    By ihatejava in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2002, 11:41 AM

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