I need to write a program that calculates the number of years, months, weeks, days, and hours since January 1st, 1970. The only catch is, the number of months cannot exceed 12, weeks exceed 4, etc...
I have been trying different methods like using the modulus operator, and doing some playing around with total seconds, but the logic is beyond me at this point.
Here's my code
#include <stdio.h>
#include <time.h>
int main() {
long now = time(NULL);
long secondsInMinute = 60;
long secondsInHour = secondsInMinute * 60;
long secondsInDay = secondsInHour * 24;
long secondsInWeek = secondsInDay * 7;
long secondsInMonth = secondsInDay * 30.42;
long secondsInYear = secondsInMonth * 12;
long years = now/secondsInYear;
long months = (now-(years*secondsInYear))/secondsInMonth;
long weeks = (now-((years*secondsInYear)+(months*secondsInMonth)))/secondsInWeek;
long days;
long hours;
printf("Since Jan 1st, 1970: ");
printf("\n%ld years have passed!", years);
printf("\n%ld months have passed!", months);
printf("\n%ld weeks have passed", weeks);
printf("\n%ld days have passed", days);
printf("\n%ld hours have passed", hours);
return 0;
}
My instructions tell me to assume there are 30.42 days in a month and 365 days in a year.
I was able to get the number of years, but the number of months comes out to 9, which would be incorrect based on a few online calculators that I've looked at. This is mainly a logic issue with me and I can't seem to wrap my head around how I would do this.
An example output would be
30 years, 4 months, 2 weeks, 4 days, 5 hours
Obviously this isn't the actual amount of time, but just an example for the format my output needs to be in. Any help is appreciated.