【问题标题】:Converting a unix time to a human readable format将 Unix 时间转换为人类可读的格式
【发布时间】:2010-12-11 05:18:48
【问题描述】:

我正在构建自己的 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、时间或本地时间,所以我还是要自己动手。 :(

【问题讨论】:

  • 如果考虑到闰秒,即使是几秒也会很困难。
  • 附言。自 1970 年以来已有 24 个
  • 如果 Windows Mobile 没有 strftime,我会寻找它可能有的其他东西......我想你可能真的需要自己动手,但它让我印象深刻他们肯定提供了以某种方式...不是吗? (注意:无论是作为用户还是作为开发人员,我对 Windows Mobile 的体验为零。)

标签: c++ unix time


【解决方案1】:

您在寻找 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 */

【讨论】:

    【解决方案2】:

    如果你想格式化打印,你需要@987654321@,这是标准的解决方案,与e.g.一起使用。 @987654322@ 将原始时间戳转换为更人性化的格式。

    【讨论】:

      猜你喜欢
      • 2014-05-26
      • 2021-03-14
      • 2020-12-12
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 2020-04-12
      相关资源
      最近更新 更多