将unix时间转换为人类可读格式

时间:2009-10-29 11:56:40

标签: c++ unix time

我正在构建自己的unix时间进行人类可读转换,而且我被卡住了。

我可以提取一年就好了,但这一天证明太棘手了。

/*
   Converts a 32-bit number of seconds after 01-01-1970 to a _SYSTEMTIME structure
*/
static _SYSTEMTIME Convert(unsigned long a_UnixTime)
{   
    newtime.wMilliseconds = 0;
    newtime.wYear   = (unsigned long)((float)a_UnixTime / (364.24f * 24.f * 60.f * 60.f));
    newtime.wDay    = (a_UnixTime - (unsigned long)((float)newtime.wYear * 364.24f * 24.f * 60.f * 60.f)); // returns 65177

    return newtime;
}

或者是否有我忽略的内置功能?

提前致谢。

更新:

嗯......似乎Windows Mobile不支持strftime,time或localtime,所以我仍然需要自己动手。 :(

2 个答案:

答案 0 :(得分:2)

你在寻找gmtime吗?

struct tm * gmtime(const time_t *clock);

External declarations, as well as the tm structure definition, are contained in the <time.h> include file.  The tm structure includes at least the following fields:

       int tm_sec;     /* seconds (0 - 60) */
       int tm_min;     /* minutes (0 - 59) */
       int tm_hour;    /* hours (0 - 23) */
       int tm_mday;    /* day of month (1 - 31) */
       int tm_mon;     /* month of year (0 - 11) */
       int tm_year;    /* year - 1900 */
       int tm_wday;    /* day of week (Sunday = 0) */
       int tm_yday;    /* day of year (0 - 365) */
       int tm_isdst;   /* is summer time in effect? */
       char *tm_zone;  /* abbreviation of timezone name */
       long tm_gmtoff; /* offset from UTC in seconds */

答案 1 :(得分:1)

如果要格式化打印,则需要strftime(),这是标准解决方案,与localtime()将原始时间戳转换为更加人性化的格式。