![]() |
|
Calculating days between two dates in C - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58) +--- Thread: Calculating days between two dates in C (/showthread.php?tid=49210) |
Calculating days between two dates in C - Panacea - 2011-11-30 Okay, so with the help of Alex and a few others for the past couple of days I managed to produce: Code: #include <stdio.h>I run into a problem whenever the dates input into the program are not in the same year, though, with the program usually being about 6 or more days over what the actual day count is. I figure it has something to do with the algorithm of my calc_days function, but I can't seem to find the error. If you can help in any way I would greatly appreciate it. For ease of access, here's the calc_days function: Code: int calc_days(int year1, int year2, int month1, int month2, int day1, int day2, int days_in_month1, int days_in_month2){Calculating days between two dates in C - SaptaZapta - 2011-11-30 C has a function, mktime(), for converting from a struct with year/month/day/hour/minute/second fields to a single long int (seconds since the epoch). Use that to convert both dates, subtract the ints, divide by seconds in a day. Calculating days between two dates in C - Panacea - 2011-11-30 We just learned about structs last week so I'll give it a shot when I get back home from class today. Calculating days between two dates in C - happylight - 2011-11-30 What is the reason for this? Code: days_until += (month_days - day2 - 5);Also you're assuming month1 < month2. Your code wouldn't work for inputs like date1 = 12/31/2011 date2 = 1/1/2012 But yeah SaptaZapta's way is much easier. Calculating days between two dates in C - Panacea - 2011-11-30 Oh, that was me just messing around, thinking that it might be easier to subtract five if my results were consistently five over what they're supposed to be. I'm currently stuck on the structure part, and I'll elaborate when I get home. Edit: You weren't wrong about that second part, either. I get 390 days between 12/31/2011 and 1/1/2012. Blah. Calculating days between two dates in C - Panacea - 2011-11-30 I was able to get it working correctly once I corrected the algorithm I was using. Thanks for the help all. Calculating days between two dates in C - thinbear - 2011-11-30 this is more about coding style Code: while(year1 < year2)I'd prefer Code: while(year1 < year2)I could be wrong, I coded c++ before but not C add on: say comparing 2011 Dec 1 and 2012 Jan 1 You cannot just add 365/366 days base on the year2 > year1 Calculating days between two dates in C - SaptaZapta - 2011-11-30 C style would be more like Code: while (year1 < year2)Calculating days between two dates in C - Nikkey - 2011-12-01 SaptaZapta Wrote:C style would be more like more like Code: int i;Unless you actually want to modify the contents of year1, that is. Calculating days between two dates in C - SaptaZapta - 2011-12-01 Nikkey Wrote:more like Look at the original code. It modifies year1. Calculating days between two dates in C - Nikkey - 2011-12-01 SaptaZapta Wrote:Look at the original code. It modifies year1. I know that - but while we're here, some code style should be learnt, shouldn't it? There's no reason to modify year1 in this case, thus you shouldn't want to - it makes it harder to reason around the code for others later on. Calculating days between two dates in C - Panacea - 2011-12-02 My professor never really went over style, to my knowledge... just more of, "This is what the finished product looks like" and sent us on our way. I know my coding/style are messy - that's my thought process, or understanding of it. Calculating days between two dates in C - Kalovale - 2011-12-03 Panacea Wrote:My professor never really went over style, to my knowledge... just more of, "This is what the finished product looks like" and sent us on our way. I think the idea is not to change things unnecessarily. If you have a function that does X, it shouldn't do Y if Y doesn't serve any practical purpose towards X, especially changing global values. Calculating days between two dates in C - SaptaZapta - 2011-12-03 Kalovale Wrote:I think the idea is not to change things unnecessarily. If you have a function that does X, it shouldn't do Y if Y doesn't serve any practical purpose towards X, especially changing global values. But it's not a global value. It's an argument to the function, passed by value, so the function can do whatever it pleases with it. Calculating days between two dates in C - Nikkey - 2011-12-04 SaptaZapta Wrote:But it's not a global value. It's an argument to the function, passed by value, so the function can do whatever it pleases with it. Yeah, but then, what does the variable year1 represent? What if someone later on came and had to edit the code, thinking that year1 is the first year? They would get pretty confused when year1 and year2 were equal (unless it's specifically mentioned in the code). It's just good code practise to avoid changing state unless you really have to. |