Find the answer to your Linux question:
Results 1 to 3 of 3
I searched the web, but did not find anything addressing date mathematics in C. I looked at many of the functions surrounding time_t and "struct tm" and could not find ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5

    Question Date (time) math in C?

    I searched the web, but did not find anything addressing date mathematics in C.

    I looked at many of the functions surrounding time_t and "struct tm" and could not find what I need.

    I need the ability to take a date, add a number of years, and get the number of days between now and then. E.G. if I take today, 9/22/2010, and add ten years I get 9/22/2020. I know there are three leap years so the number of days should be 3650+3 or 3653 days. I also need to add days to a date, but I am pretty sure I know how to add days to time_t (time_t tval = time()+((60*60*24)*<days>) ) so I think I have that covered.

    Before setting down and sharpening my pencil, I thought it might be prudent to ask if I missed something and the capability to do this already exists in a GNU C method somewhere or in some freeware C library I am not aware of.

    I know I can get there, so I don't need someone providing me source code. I just don't want to re-invent the wheel.

  2. #2
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    That is pretty much the way. You might be able to find some code out there that someone has written if you search around a bit. I am sure this is a problem that has been done time and time again.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You need to convert the dates to Julian numbers, and then you can do date math, such as calculating the difference between them, very easily. This web page on Wikipedia has the math you need to write gregorian to julian functions - I've done so in the past: Julian day - Wikipedia, the free encyclopedia
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...