0

我写了一个简单的函数来用当前的年、月和日填充三个变量。但是,由于某种原因,它无法正常工作,而且我似乎找不到问题所在。

void getDate(int *year, int *month, int *date)
{
int   epochTime,
      monthLength,
      functionYear,
      functionMonth,
      functionDate;

functionYear   = 1970;
functionMonth  = 1;
functionDate   = 1;

epochTime = time(NULL);
while (epochTime > 1 * 365 * 24 * 60 * 60)
{
   epochTime -= 1 * 365 * 24 * 60 * 60;
   functionYear++;
}
monthLength = findMonthLength(functionYear, functionMonth, false);
while (epochTime > 1 * monthLength * 24 * 60 * 60)
{
   printf("%d\n", epochTime);
   epochTime -= 1 * monthLength * 24 * 60 * 60;
   functionMonth++;
   monthLength = findMonthLength(functionYear, functionMonth, false);      
   printf("functionMonth = %d\n", functionMonth);
}
while (epochTime > 1 * 24 * 60 * 60)
{
   printf("%d\n", epochTime);
   epochTime -= 1 * 24 * 60 * 60;
   functionDate++;
   printf("functionDate = %d\n", functionDate);
}
*year    = functionYear;
*month   = functionMonth;
*date    = functionDate;
}

findMonthLength()返回一个整数值,它是发送月份的长度。1 = 一月等。它使用年份来测试它是否是闰年。

目前是 2013 年 4 月 3 日;但是,我的函数找到了 4 月 15 日,我似乎找不到我的问题所在。

编辑:我明白了。我的第一个问题是,虽然我记得在查找月份时检查闰年,但在查找每年时却忘记了这一点,这让我休息了好几天。我的第二个问题是我没有从 UTC 转换为本地时区

4

3 回答 3

2

本节可能存在一个问题:

while (epochTime > 1 * 365 * 24 * 60 * 60)
{
    epochTime -= 1 * 365 * 24 * 60 * 60;
    functionYear++;
} 

此循环的每次迭代,都会减去对应于正常年份的时间(以秒为单位)。这不考虑闰年,您需要减去对应于 366 天的时间。

对于该部分,您可能需要:

int yearLength = findYearLength(functionYear + 1);
while (epochTime > 1 * yearLength * 24 * 60 * 60)
{
    epochTime -= 1 * yearLength * 24 * 60 * 60;
    functionYear++;
    yearLength = findYearLength(functionYear + 1);
}

findYearLength (int year)是一个返回给定年份的天数的函数。

一个小问题是没有考虑闰秒。由于仅添加了其中的 35 个,因此可以在给定日期的计算中安全地忽略它。

于 2013-04-03T20:53:04.110 回答
1

为什么不使用 gmtime() 或 localtime() 并完成它?他们返回一个包含你需要的一切的结构。

于 2013-04-03T21:42:38.940 回答
0

我在 Python 中根据您的解决方案制作了完整的解决方案。我希望有一天它会对某人有所帮助。干杯!

使用:getDate(unixtime)

def leapYear(year):
        #returns True if the year is a leap year, return False if it isn't
        if year % 400 == 0:
                return True
        elif year % 100 == 0:
                return False
        elif year % 4 == 0: 
                return True
        else:
             return False

def findMonthLength(year, month):
        #returns an integer value with the length of the month it is sent.
        #1 = January, etc. It uses the year to test if it is a leap year.

        months1 = [0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        months2 = [0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

        if (leapYear(year)==True):
                return months2[month]
        else:
                return months1[month]

def findYearLength(year):
        #returns an integer value with the length of the year it is sent.
        #It uses the year to test if it is a leap year.

        if leapYear(year)==True:
                return 366
        else:
                return 365

def getDate(epoch):
        SECONDS_PER_YEAR = 0
        SECONDS_PER_MONTH = 0
        SECONDS_PER_DAY = 24 * 60 * 60
        SECONDS_PER_HOUR = 60*60
        SECONDS_PER_MIN = 60

        epochTime = epoch
        monthLength = 0
        yearLength = 0
        year = 1970
        month = 1
        day = 1
        hour = 0
        minu = 0
        seg = 0

        #Years
        yearLength = findYearLength(year)
        SECONDS_PER_YEAR = yearLength * SECONDS_PER_DAY
        while (epochTime >= SECONDS_PER_YEAR):
                epochTime -= SECONDS_PER_YEAR
                year += 1
                yearLength = findYearLength(year)
                SECONDS_PER_YEAR = yearLength * SECONDS_PER_DAY

        #Months
        monthLength = findMonthLength(year, month)
        SECONDS_PER_MONTH = monthLength * SECONDS_PER_DAY
        while (epochTime >= SECONDS_PER_MONTH):
                epochTime -= SECONDS_PER_MONTH;
                month += 1
                monthLength = findMonthLength(year, month)
                SECONDS_PER_MONTH = monthLength * SECONDS_PER_DAY

        #Days
        while (epochTime >= SECONDS_PER_DAY):
                epochTime -= SECONDS_PER_DAY;
                day += 1

        #Hours
        while (epochTime >= SECONDS_PER_HOUR):
                epochTime -= SECONDS_PER_HOUR;
                hour += 1

        #Minutes
        while (epochTime >= SECONDS_PER_MIN):
                epochTime -= SECONDS_PER_MIN;
                minu += 1

        #Seconds
        seg = epochTime

        print ("%d-%d-%d %d:%d:%d") % (year, month, day, hour, minu, seg)
于 2013-08-16T14:06:36.237 回答