解析ISO 8601持续时间 [英] Parse ISO 8601 durations

查看:1309
本文介绍了解析ISO 8601持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ISO 8601中,持续时间以格式 P [n] Y [n] M [n] DT [n] H [n] M [n] S



示例:



20秒:

  PT20.0S 

一年,2个月,3天,4小时,5分6秒:

  P1Y2M3DT4H5M6S 



问题:



包含以iso 8601格式的持续时间。我想获得该持续时间的总秒数。



$ b

b

例如,在boost DateTime中有ptime from_iso_string(std :: string),不适合这里。

解决方案

使用标准的regex库,你想要的正则表达式是这样的:

 P\(\([0-9] + \)Y \)?\ \([0-9] + \)M \)?\(\([0-9] + \)D\)?T \(\([0-9] + \)H \)?\(\([0-9] + \)M \)?\(\([0-9] + \(\。 ] + \)?S \)?

年,月并计算总秒数。


In ISO 8601, durations are given in the format P[n]Y[n]M[n]DT[n]H[n]M[n]S.

Examples:

20 seconds:

PT20.0S

One year, 2 month, 3 days, 4 hours, 5 minutes, 6 seconds:

P1Y2M3DT4H5M6S

Question:

Given a string that contains a duration in iso 8601 format. I want to obtain the overall number of seconds of that duration. What is the recommended way in standard C++11 to achieve this?

Remarks:

E.g., there is ptime from_iso_string(std::string) in boost DateTime which does not fit here. Is there a similar way without doing a regex by hand?

解决方案

Use the standard regex library, the regex you want is something like:

"P\(\([0-9]+\)Y\)?\(\([0-9]+\)M\)?\(\([0-9]+\)D\)?T\(\([0-9]+\)H\)?\(\([0-9]+\)M\)?\(\([0-9]+\(\.[0-9]+\)?S\)?"

from that you can pull out the number of years, months etc and calculate total seconds.

这篇关于解析ISO 8601持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆