Im required as part of a lab to devise a way to calculate the current month day and year. I'll be using the gettimeofday() function which gives me the number of seconds since january 1 1970.
I know that there are functions that will do the conversoins for me, however the design requirement is that i create my own algorithm for converting the seconds to months days and years. The manner in which I want to implement my design is with a lookup table for each of the twelve months and the corresponding number of days. The logic is a little befuddling to me right now.
The tricky part is handling the leap years. I know that 1972 is the first leap year since 1970. And a leap year occurs every 4 years since that date. The hint given to me in this assignment is that the next largest cycle after days is 4 years. So if I modulus the number of days since 1970 by 1461 (number of days in 4 years) I know I can get the number of days left over. Its at this point my train of logic gets lost. If I DIVIDE it by 1461 it just tells me the how many 4 year periods have gone by.
The table I want to implement will look something like this ( i know the coding isnt completely right but just to show what im getting at):
struct Monthdays
{
int days;
char* Monthname[]
};
Monthdays lookupMonths[]
{
{31,"January"}
{28,"February"}
.
.
.
};
Im trying to figure out how to create a proper index using the number of days or something to walk through this "table".........I hope asking this here is okay. I've been struggling with the logic or a couple of days right now....
Here is the code for this problem i have now which is very inefficient.
ExpandedTime* localTime(
struct timeval* tv, // Pointer to timeval struct
ExpandedTime* etime // '' '' to expandedtime strct
)
{
tzset(); // Corrects for timezone
int epochT = (tv->tv_sec) - timezone; // Epoch seconds with
int epochUT = tv->tv_usec; // epochtime microseconds
int edays; // Days since epochtime
etime->et_usec = (epochUT/milli) % milli; // Find the milliseconds
etime->et_sec = epochT % 60;
epochT /= 60; // Turn into minutes
etime->et_min = epochT % 60;
epochT /= 60; // Turn into hours
if (localtime(&tv->tv_sec)->tm_isdst !=0)
etime->et_hour = (epochT % 24) + daylight; // Hours with DST correc
else
etime->et_hour = (epochT % 24);
edays = epochT /= 24; // Turn into days
etime->et_day = epochT; // Delete up to here
etime->et_year = (epochT/365) + epochyear; // Get the current year
int trackyear; // Counter for years
int trackdays = -1; // Subtracting janurary 1st
// from days
// This will determine if it is a leapyear and adjust days accordingly
// from 1970 to current year (2013)
for (trackyear = epochyear; trackyear < etime->et_year; trackyear++)
{
if (trackyear % leapy == 0)
{
trackdays = trackdays + 366;
}
else
{
trackdays = trackdays + 365;
}
}
etime->et_day = edays - trackdays;
int trackmonth = -1; // Counter for months
// with offset to make
// january = 0
// This will give me the number of months for the buffer
do
{
switch (trackmonth)
{
// Months with 31 days
case 0:
etime->et_day = (etime->et_day) - 31;
break;
case 2:
etime->et_day = (etime->et_day) - 31;
break;
case 4:
etime->et_day = (etime->et_day) - 31;
break;
case 6:
etime->et_day = (etime->et_day) - 31;
break;
case 7:
etime->et_day = (etime->et_day) - 31;
break;
case 9:
etime->et_day = (etime->et_day) - 31;
break;
case 11:
etime->et_day = (etime->et_day) - 31;
break;
// Months with only 30 days
case 3:
etime->et_day = (etime->et_day) - 30;
break;
case 5:
etime->et_day = (etime->et_day) - 30;
break;
case 8:
etime->et_day = (etime->et_day) - 30;
break;
case 10:
etime->et_day = (etime->et_day) - 30;
break;
// Leap year month a.k.a Febuary
case 1:
if (trackyear % leapy)
{
etime->et_day = (etime->et_day) - 28;
}
else
{
etime->et_day = (etime->et_day) - 29;
}
break;
}
trackmonth++;
}
while(etime->et_day > 0);
etime->et_mon = trackmonth - 1;
// Reverts day offset from previous switch to
// accurately represent the current day
switch (etime->et_mon)
{
// Months with 31 days
case 0:
etime->et_day = (etime->et_day) + 31;
break;
case 2:
etime->et_day = (etime->et_day) + 31;
break;
case 4:
etime->et_day = (etime->et_day) + 31;
break;
case 6:
etime->et_day = (etime->et_day) + 31;
break;
case 7:
etime->et_day = (etime->et_day) + 31;
break;
case 9:
etime->et_day = (etime->et_day) + 31;
break;
case 11:
etime->et_day = (etime->et_day) + 31;
break;
// Months with only 30 days
case 3:
etime->et_day = (etime->et_day) + 30;
break;
case 5:
etime->et_day = (etime->et_day) + 30;
break;
case 8:
etime->et_day = (etime->et_day) + 30;
break;
case 10:
etime->et_day = (etime->et_day) + 30;
break;
// Leap year month a.k.a Febuary
case 1:
if (trackyear % leapy)
{
etime->et_day = (etime->et_day) + 28;
}
else
{
etime->et_day = (etime->et_day) + 29;
}
break;
}
return etime;
}