我需要作为实验室的一部分来设计计算当前月份日和年的方法。我将使用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;
}
答案 0 :(得分:0)
进行一些网上冲浪,了解如何计算朱利安日期(或更确切地说是朱利安日数字)......这将解决您的问题或让您顺利前往。
除此之外,为他们做一些人的作业是不道德的......虽然......我确实有一个PayPal账号〜大声笑〜
答案 1 :(得分:-2)
if(year % 4 == 0 && year % 100 == 0 && year % 25 != 0)
this is a leap year.
这实际上是我在大学时最困难的作业......总而言之,根据需要的准确程度,我会告诉你[不]看时区以及当不同的区域进入夏令时......
很抱歉,如果答案不是很有帮助,但这是闰年的公式。如果你继续前进的路线,你需要将Chinese theorem引入此...
但是如果你可以做gettimeofday()
它会返回自1970年1月1日以来的毫秒数。所以你可以在其中加入一点“浪费”秒数并模拟到现在为止的时间流逝,并运行该循环,直到你没时间。当你停下来时,你应该能够看到你停在哪个日期;)