gpt4 book ai didi

c++ - 在 C 中使用 Gettimeofday() 的日月年算法

转载 作者:行者123 更新时间:2023-11-30 20:43:27 25 4
gpt4 key购买 nike

作为实验室的一部分,我需要设计一种计算当前月日和年份的方法。我将使用 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;

}

最佳答案

在网上冲浪一下,了解如何计算儒略日期(或更准确地说是儒略日数)……这将解决您的问题或让您顺利上路。

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

关于c++ - 在 C 中使用 Gettimeofday() 的日月年算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16020318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com