C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 提升日期时间解析字符串大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我看过很多例子似乎都解决了这个简单的案例.我要解析的字符串是:

“2012-06-01 16:45:34 EDT”

我试图用folloiwng创建一个local_time_input_faCET
“%Y-%m-%d%H:%M:%S%Z”

始终未设置local_date_time对象的区域指针.阅读文档令人困惑:

“EDT”//东部夏令时

有没有人这样做过?

更新:我已更新代码以更好地说明问题:

using namespace std;
using namespace boost::local_time;

int main()
{
    Stringstream ss;

    // Set up the input datetiR_715_11845@e format.
    local_time_input_faCET *input_faCET 
    = new local_time_input_faCET("%Y-%m-%d %H:%M:%s %ZP");
    ss.imbue(std::locale(ss.getloc(),input_faCET));

    local_date_time ldt(not_a_date_time),ldt1(not_a_date_time);

    // Read a timE into ldt
    ss.str("2012-06-01 17:45:34 EDT");
    ss >> ldt;

    ss.str("2012-06-01 17:45:34 CDT");
        ss >> ldt1;
        std::cerr << (ldt - ldt1).@R_729_10586@l_seconds() << std::endl;

    // Write the time to stdout.
    cout << "Full Time:\t"   << ldt.to_String() << endl;
    cout << "Local time:\t"  << ldt.local_time() << endl;
    cout << "Time zone:\t"   << ldt.zone_as_posix_String() << endl;
    cout << "Zone abbrev:\t" << ldt.zone_abbrev() << endl;
    cout << "Zone offset:\t" << ldt.zone_abbrev(true) << endl;

    cout << "Full Time:\t"   << ldt1.to_String() << endl;
    cout << "Local time:\t"  << ldt1.local_time() << endl;
    cout << "Time zone:\t"   << ldt1.zone_as_posix_String() << endl;
    cout << "Zone abbrev:\t" << ldt1.zone_abbrev() << endl;
    cout << "Zone offset:\t" << ldt1.zone_abbrev(true) << endl;

    return 0;
}

OUTPUT:

0
Full Time:  2012-Jun-01 17:45:34 EDT
Local time: 2012-Jun-01 17:45:34
Time zone:  EDT+00
Zone abbrev:    EDT
Zone offset:    +0000
Full Time:  2012-Jun-01 17:45:34 CDT
Local time: 2012-Jun-01 17:45:34
Time zone:  CDT+00
Zone abbrev:    CDT
Zone offset:    +0000

解决方法

错误

根据boost的文档:http://www.boost.org/doc/libs/1_57_0/doc/html/date_time/date_time_io.html#date_time.format_flags

%Z是:

它还说%ZP:

因此,您需要将%Z更改为%ZP.现在你的时间戳将解析.但是,您会注意到不会设置区域偏移.

Posix时区字符串

对于Posix时区字符串,您至少需要指定区域缩写和UTC的偏移量,例如: EST-5.

完整的Posix时区字符串格式为:

"std offset dst [offset],start[/time],end[/time]"

根据http://www.boost.org/doc/libs/1_57_0/doc/html/date_time/local_time.html#date_time.local_time.posix_time_zone,没有空格

以下是EST和PST的完整Posix时区字符串的一些示例:

EST-5EDT,M3.2.0,M11.1.0
PST-8PDT,M4.1.0,M10.1.0

其中包含有关夏令时何时生效的信息.

但是,您可以在您的情况下使用EDT-4,具体取决于您正在使用它.

应该注意的是,就像Posix时区一样简单,它们受到限制,因为它们不虑时区规则的历史变化.我认为最好不要首先避免使用时区.

如果我无法控制输入格式怎么办?

正如评论中提到的OP,偏移量未列在其输入时间戳中.一种解决方案是从输入时间戳的末尾读取区域缩写(例如“EDT”),并根据已知区域缩写的映射进行检查:

std::map<std::string,std::string> zone_map;
zone_map["EST"] = "EST-5EDT,M10.5.0";  // Eastern Standard Time
zone_map["EDT"] = zone_map["EST"];            // Eastern Daylight Time
zone_map["PST"] = "PST-8PDT,M10.1.0";  // Pacific Standard Time
zone_map["PDT"] = zone_map["PST"];            // Pacific Daylight Time
// ...

(请注意,上面的DST区域应与标准时区相同.)

您可能只需存储简单的UTC偏移即可:

zone_map["EST"] = "EST-5";  // Eastern Standard Time
zone_map["EDT"] = "EDT-4";  // Eastern Daylight Time
// ...

工作实例

这是一个使用内置时区数据库的示例:

#include <map>
#include <String>
#include <sstream>
#include <iostream>
#include <boost/date_time/local_time/local_time.hpp>

using namespace boost::local_time;

int main()
{
    // A little database of time zones.
    std::map<std::string,std::string> zone_map;
    zone_map["EST"] = "EST-5EDT,M10.5.0";  // Eastern Standard Time
    zone_map["EDT"] = zone_map["EST"];            // Eastern Daylight Time
    zone_map["PST"] = "PST-8PDT,M10.1.0";  // Pacific Standard Time
    zone_map["PDT"] = zone_map["PST"];            // Pacific Daylight Time
    // ...

    // This is our input timestamp.
    std::string timestamp = "2012-06-01 16:45:34 EDT";

    // replace time zone abbrev with full Posix time zone.
    const size_t abbrev_pos = timestamp.find_last_of(' ') + 1;
    const std::string abbrev = timestamp.substr(abbrev_pos);
    timestamp.replace(abbrev_pos,std::string::npos,zone_map[abbrev]);

    std::cout << "Time stamp with full timezone: " << timestamp << std::endl;

    // Set up the input datetiR_715_11845@e format.
    local_time_input_faCET *input_faCET = new local_time_input_faCET("%Y-%m-%d %H:%M:%s %ZP");
    std::stringstream ss;
    ss.imbue(std::locale(ss.getloc(),input_faCET));

    // This is our output date time.
    local_date_time ldt(not_a_date_time);

    // Read the timestamp into ldt.
    ss.str(timestamp);
    ss >> ldt;

    // Write the time to stdout.
    std::cout << "Full Time:\t"   << ldt.to_String() << std::endl
              << "Local time:\t"  << ldt.local_time() << std::endl
              << "Time zone:\t"   << ldt.zone_as_posix_String() << std::endl
              << "Zone abbrev:\t" << ldt.zone_abbrev() << std::endl
              << "Zone offset:\t" << ldt.zone_abbrev(true) << std::endl;

    return 0;
}

输出

Time stamp with full timezone: 2012-06-01 16:45:34 EST-5EDT,M10.5.0
Full Time:      2012-Jun-01 16:45:34 EDT
Local time:     2012-Jun-01 16:45:34
Time zone:      EST-05EDT+01,M4.1.0/02:00,M10.5.0/02:00
Zone abbrev:    EDT
Zone offset:    -0400

然这里的解决方案可能并不理想,但这是我能看到的唯一方法.

大佬总结

以上是大佬教程为你收集整理的c – 提升日期时间解析字符串全部内容,希望文章能够帮你解决c – 提升日期时间解析字符串所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。