3

这似乎是任何人都不应该做的事情,但我正在为嵌入式系统(OpenWRT)开发一个内核模块,其中似乎time.h 确实包括timespecandtime_t类型和clock_gettimeandgmtime函数,但包括localtime, ctime, time,或者,重要的是,tm类型。

当我尝试将返回指针从 gmtime 转换为我自己的结构时,我得到一个段错误。

所以我想我会满足于以两种方式解决这个问题——弄清楚如何访问那个丢失的类型,或者如何使用我自己的方法来分解一个 unix 时间戳,这将是很棒的。

4

3 回答 3

5

这应该是准确的(填写 a 的缩减模仿struct tm,我year使用 Common Era 而不是 1900 CE epoch):

struct xtm
{
    unsigned int year, mon, day, hour, min, sec;
};

#define YEAR_TO_DAYS(y) ((y)*365 + (y)/4 - (y)/100 + (y)/400)

void untime(unsigned long unixtime, struct xtm *tm)
{
    /* First take out the hour/minutes/seconds - this part is easy. */

    tm->sec = unixtime % 60;
    unixtime /= 60;

    tm->min = unixtime % 60;
    unixtime /= 60;

    tm->hour = unixtime % 24;
    unixtime /= 24;

    /* unixtime is now days since 01/01/1970 UTC
     * Rebaseline to the Common Era */

    unixtime += 719499;

    /* Roll forward looking for the year.  This could be done more efficiently
     * but this will do.  We have to start at 1969 because the year we calculate here
     * runs from March - so January and February 1970 will come out as 1969 here.
     */
    for (tm->year = 1969; unixtime > YEAR_TO_DAYS(tm->year + 1) + 30; tm->year++)
        ;

    /* OK we have our "year", so subtract off the days accounted for by full years. */
    unixtime -= YEAR_TO_DAYS(tm->year);

    /* unixtime is now number of days we are into the year (remembering that March 1
     * is the first day of the "year" still). */

    /* Roll forward looking for the month.  1 = March through to 12 = February. */
    for (tm->mon = 1; tm->mon < 12 && unixtime > 367*(tm->mon+1)/12; tm->mon++)
        ;

    /* Subtract off the days accounted for by full months */
    unixtime -= 367*tm->mon/12;

    /* unixtime is now number of days we are into the month */

    /* Adjust the month/year so that 1 = January, and years start where we
     * usually expect them to. */
    tm->mon += 2;
    if (tm->mon > 12)
    {
        tm->mon -= 12;
        tm->year++;
    }

    tm->day = unixtime;
}

我为所有神奇的数字道歉。367*month/12 是生成日历的 30/31 天序列的巧妙技巧。该计算适用于从 3 月开始直到最后修正的年份,这使事情变得容易,因为闰日落在“年”的末尾。

于 2009-08-14T01:44:11.493 回答
0

在用户空间中,glibc 将在处理时间表示的“本地”部分方面做很多工作。在内核中这是不可用的。可能你不应该尝试在你的模块中打扰这个,如果需要的话在用户空间中做。

于 2009-08-13T22:56:21.007 回答
0

Atime_t是自 1970 年 1 月 1 日 UTC 以来的秒数,因此只要您想要 UTC 格式的结果,将其分解为月、日和年并不难。谷歌搜索“gmtime source”很多可用的资源。大多数嵌入式系统都忽略了本地时间处理,因为由于依赖于时区设置和环境,它有点困难。

于 2009-08-14T00:25:45.693 回答