使用ICU库在字符串中解析时间戳,我做错了什么?

时间:2015-05-24 04:45:28

标签: c++ parsing time icu

我尝试使用ICU库将字符串中的时间戳转换为time_t值。

为此,我要做的就是创建一个时间实例并调用其parse()函数。不幸的是,我得到的只是来自解析器的U_ILLEGAL_ARGUMENT_ERROR。

有一个简化版本的代码可以显示意外行为:

#include <iostream>
#include <unicode/datefmt.h>
#include <unicode/errorcode.h>
#include <unicode/locid.h>
#include <unicode/smpdtfmt.h>
#include <unicode/timezone.h>

int main(int argc, char *argv[])
{
    LocalPointer<DateFormat> dt(DateFormat::createTimeInstance());

    UErrorCode err(U_ZERO_ERROR);
    UDate const result(dt->parse("12:05:33", err));

    std::cerr << "*** err = " << u_errorName(err)
              << " and result = " << result << "\n";

    return 0;
}

当我运行它时,输出是

*** err = U_ILLEGAL_ARGUMENT_ERROR and result = 0

我期待的东西更像是时间的表示,例如12 x 3600 + 5 * 60 + 33 = 43533。

我做错了吗?从documentation开始,看起来该库能够转换各种类型的时间戳。

1 个答案:

答案 0 :(得分:0)

最有可能的是,时间字符串的格式与DateFormat返回的createTimeInstance()对象的默认样式/语言环境不匹配。使用style的{​​{1}}和aLocale参数指定createTimeInstance()应该期望的特定样式/区域设置。