c语言解析时间字符串,c – 提升日期时间解析字符串

错误

%Z是:

Full time zone name (output only).

它还说%ZP:

Posix time zone string (available to both input and output).

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

Posix时区字符串

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

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

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

以下是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<:string std::string> zone_map;

zone_map["EST"] = "EST-5EDT,M4.1.0,M10.5.0"; // Eastern Standard Time

zone_map["EDT"] = zone_map["EST"]; // Eastern Daylight Time

zone_map["PST"] = "PST-8PDT,M4.1.0,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

#include

#include

#include

#include

using namespace boost::local_time;

int main()

{

// A little database of time zones.

std::map<:string std::string> zone_map;

zone_map["EST"] = "EST-5EDT,M4.1.0,M10.5.0"; // Eastern Standard Time

zone_map["EDT"] = zone_map["EST"]; // Eastern Daylight Time

zone_map["PST"] = "PST-8PDT,M4.1.0,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 datetime 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,M4.1.0,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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值