-2

作为实验室的一部分,我需要设计一种方法来计算当前月份的日期和年份。我将使用 gettimeofday() 函数,它给出了自 1970 年 1 月 1 日以来的秒数。

我知道有些函数可以为我进行转换,但是设计要求是我创建自己的算法来将秒数转换为月日和年。我想要实现我的设计的方式是为十二个月中的每个月和相应的天数提供一个查找表。现在的逻辑让我有点困惑。

棘手的部分是处理闰年。我知道 1972 年是自 1970 年以来的第一个闰年。自该日期起每 4 年发生一次闰年。在这个作业中给我的提示是,天之后的下一个最大周期是 4 年。因此,如果我将自 1970 年以来的天数乘以 1461(4 年的天数),我知道我可以得到剩余的天数。在这一点上,我的逻辑迷路了。如果我将它除以 1461,它只会告诉我已经过去了多少个 4 年。

我想实现的表看起来像这样(我知道编码不完全正确,只是为了显示我得到了什么):

struct Monthdays
{
int days;
char* Monthname[]
};

Monthdays lookupMonths[]
{
{31,"January"}
{28,"February"}
.
.
.

};

我试图弄清楚如何使用天数或一些东西来创建正确的索引来遍历这个“表”............我希望在这里问这个没问题。我一直在与逻辑或几天作斗争......

这是我现在遇到的这个问题的代码,效率很低。

    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;

}
4

2 回答 2

0

上网浏览一下有关如何计算儒略日(或更准确地说是儒略日数)的信息……这将解决您的问题或让您顺利上路。

除此之外,为他们做作业是不道德的……不过……我确实有一个 PayPal 帐户~lol~

于 2013-04-15T16:36:49.720 回答
-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 ;)

于 2013-04-15T16:39:29.247 回答