【问题标题】:convert const char * to struct tm under Android with C++使用 C++ 在 Android 下将 const char * 转换为 struct tm
【发布时间】:2014-07-18 12:04:02
【问题描述】:

我想从const char * 获得struct tm。为了实现目标,我使用以下代码:

CCLog("ServerTimeStamp:%s",servertimestamp);
struct tm servertime;
servertime.tm_isdst=0;
servertime.tm_gmtoff=0;
CCLog("servertime prev:{%i,%i,%i,%i:%i:%i+%i+%li}",servertime.tm_year,servertime.tm_mon,servertime.tm_mday,servertime.tm_hour,servertime.tm_min,servertime.tm_sec,servertime.tm_isdst,servertime.tm_gmtoff);
strptime(servertimestamp, "%FT%TZ", &servertime);
CCLog("servertime post:{%i,%i,%i,%i:%i:%i+%i+%li}",servertime.tm_year,servertime.tm_mon,servertime.tm_mday,servertime.tm_hour,servertime.tm_min,servertime.tm_sec,servertime.tm_isdst,servertime.tm_gmtoff);

但是输出是:

ServerTimeStamp:2014-07-18T11:51:09Z
servertime prev:{24,1803363120,22,1803363016:0:1796668624+0+0}
servertime post:{24,1803363120,22,1803363016:0:1796668624+0+0}

这意味着strptime 可能什么也没做。这个功能在Android上JNI下没有实现吗(cocos2d-x环境下,2.1rc0-x-2.1.4版本)?什么是合适的选择?如何将const char * 转换为struct tm

我的App是跨平台的,同样的代码在iOS下也能完美运行(环境cocos2d-x,版本2.1rc0-x-2.1.4)。

提前致谢!

【问题讨论】:

  • 你的意思是strptime(): convert a character string to values to be stored in a tm structure
  • 您使用了 %F 说明符。它应该怎么做?它不在列表中:pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html
  • @greenapps %F Equivalent to %Y-%m-%d, the ISO 8601 date format. from this page,但它似乎是一个 GNU 扩展,也许它没有在 Android 中实现。
  • @greenapps 你是对的,这就是问题所在。如果您将其转换为答案,我可以接受。

标签: android c++ java-native-interface cocos2d-x


【解决方案1】:

不要对日期使用 %F 说明符。

【讨论】:

  • 如果有人遇到同样的问题,%F 相当于%Y-%m-%d
猜你喜欢
  • 1970-01-01
  • 2015-09-09
  • 2015-05-06
  • 2013-12-31
  • 2012-01-16
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多