【问题标题】:Problems with my unix-epoch time converter我的 unix-epoch 时间转换器的问题
【发布时间】:2013-04-03 20:36:37
【问题描述】:

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

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 转换为本地时区

【问题讨论】:

  • printf 仅在代码中,因为我正在尝试调试它。
  • 您确实意识到一年中没有365*24*60*60 秒,对吧?闰年是有原因的……
  • 你在确定年份时没有考虑闰年,我怀疑你没有把 2000 年算作闰年。
  • @twalberg:4 次中有 3 次还不错。 ;-)
  • 您的更新在检查闰年时有 while ... 循环,这可能不太正确......这将基本上选择一个循环,具体取决于您的开始年份是闰年,而不是决定每年...

标签: c unix-timestamp


【解决方案1】:

本节可能有一个问题:

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

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

【讨论】:

  • 这基本上是我改成的,但还是不行。
【解决方案2】:

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

【讨论】:

  • 我刚学C语言,还不知道怎么用结构。
【解决方案3】:

我根据你的用 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-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多