我的UNIX的纪元时间转换器问题 [英] Problems with my unix-epoch time converter

查看:104
本文介绍了我的UNIX的纪元时间转换器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote a simple function to fill three variables with the current year, month, and day. However, for some reason it is not working correctly, and I can't seem to find the problem.

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 =一月,等它使用一年来测试它是否是闰年。

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

这是目前2013年4月3日;然而,我的函数查找4月15日,我似乎无法找到在哪里我的问题是。

It is currently April 3, 2013; however, my function finds April 15, and I can't seem to find where my problem is.

编辑:
我知道了。我的第一个问题是,当我想起找到个月的时候检查闰年,我忘了,要找到每一年,这让我几天假时。
我的第二个问题是,我没有从UTC转换为本地时区

I got it. My first problem was that while I remembered to check for leap years when finding the months, I forgot about that when finding each year, which put me several days off. My second problem was that I didn't convert to the local time zone from UTC

推荐答案

一个问题可以在这个部分:

One problem could in this section:

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

此循环的每次迭代中,在对应于一个秒的时间的正常的年被扣除。这种不考虑闰年,在那里你需要减去对应到366天的时间。

Each iteration of this loop, a time in seconds corresponding to one normal year is subtracted. This does not account for leap years, where you need to subtract a time corresponding to 366 days.

有关的部分,你可能想:

For that section, you may want:

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年)的是,返回给定年份的天长度的函数。

with findYearLength(int year) being a function that returns the length in days of a given year.

一个小问题是都不占该闰秒。因为只有这些35已被添加,可以安全地在一个给定的一天的计算中忽略。

One minor issue is that leap seconds are not accounted for. As only 35 of these have been added, that can be safely ignored in a calculation for a given day.

这篇关于我的UNIX的纪元时间转换器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆