-2

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;

}
PresidentRFresh
  • 93
  • 1
  • 2
  • 10
  • 1
    [spoiler](http://stackoverflow.com/a/11197532/968261). But look at the comments, how I arranged the years to cope with leap years. – Alexey Frunze Apr 15 '13 at 16:38
  • 1
    Hi welcome, to Stackoverflow. This is a forum for getting help with programming and it is in poor taste to post something without attempting the problem. If you've given this a shot and can't figure out why it's not working post your code and you'll get much more help. – rhinoinrepose Apr 15 '13 at 16:39
  • I've tried the problem. I have a copy and paste method right now which is terribly inefficient. Its also 230 lines long (the program does other things as well) so I figured noone would want to read through all of that. – PresidentRFresh Apr 15 '13 at 16:41
  • Why not use an array and an index into it instead of that humongous switch? – Alexey Frunze Apr 15 '13 at 16:48
  • Thats what Im trying to do. But after finding the years im trying to implement a table using an index to lookup the appropirate month and day. The index part is screwing me up because the logic of what I have too look for escapes me. I mean, do i use the remainder of days after dividing or modding some value as the index...etc. – PresidentRFresh Apr 15 '13 at 16:52

2 Answers2

0

Do a little web surfing for information on how to calculate a Julian Date (or more precisely a Julian Day Number) ... that will solve your problem or get you well on your way.

Other than that, it would be unethical to do someone's homework for them... though... I do have a PayPal account ~lol~

K Scott Piel
  • 4,320
  • 14
  • 19
  • Make sure you are looking at "julian date", not "julian calendar". – mbeckish Apr 15 '13 at 16:38
  • @mbeckish: Also, the word "julian date" is sometimes *incorrectly* used to refer to the day number within a year. A Julian date (more precisely a Julian Day Number) is the number of days since a particular arbitrary date in the past. – Keith Thompson Apr 15 '13 at 16:58
  • Edited to add specificity in accordance with the foregoing comments – K Scott Piel Apr 15 '13 at 17:01
-2
if(year % 4 == 0 && year % 100 == 0 && year % 25 != 0) 
    this is a leap year.

This was actually the hardest kind of homework i had on college... all in all, depending on how accurate this needs to be, i'll tell you to [not] look into timezones and when different zones enter daylight savings...

Sorry if the answer isn't terribly helpful, but this is the formula for leap years. If you keep going the route you're headed, you'll need to bring in the Chinese theorem into this...

But if you can do gettimeofday() it returns the number of milliseconds since 1st January 1970. So you can just put a little for in it that will 'waste' the seconds and simulate passage of time up until now, and run that loop until you run out of time. When you stop you should be able to see at which date you stopped ;)

abelenky
  • 63,815
  • 23
  • 109
  • 159
Shark
  • 6,513
  • 3
  • 28
  • 50