123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- #ifndef _DATE_TIME_TIME_PARSING_HPP___
- #define _DATE_TIME_TIME_PARSING_HPP___
- #include "boost/tokenizer.hpp"
- #include "boost/lexical_cast.hpp"
- #include "boost/date_time/date_parsing.hpp"
- #include "boost/date_time/special_values_parser.hpp"
- #include "boost/cstdint.hpp"
- #include <iostream>
- namespace boost {
- namespace date_time {
-
-
-
- template<class int_type>
- inline
- int_type power(int_type base, int_type exponent)
- {
- int_type result = 1;
- for(int i = 0; i < exponent; ++i){
- result *= base;
- }
- return result;
- }
-
-
-
- template<class time_duration, class char_type>
- inline
- time_duration
- str_from_delimited_time_duration(const std::basic_string<char_type>& s)
- {
- unsigned short min=0, sec =0;
- int hour =0;
- bool is_neg = (s.at(0) == '-');
- boost::int64_t fs=0;
- int pos = 0;
-
- typedef typename std::basic_string<char_type>::traits_type traits_type;
- typedef boost::char_separator<char_type, traits_type> char_separator_type;
- typedef boost::tokenizer<char_separator_type,
- typename std::basic_string<char_type>::const_iterator,
- std::basic_string<char_type> > tokenizer;
- typedef typename boost::tokenizer<char_separator_type,
- typename std::basic_string<char_type>::const_iterator,
- typename std::basic_string<char_type> >::iterator tokenizer_iterator;
-
- char_type sep_chars[5] = {'-',':',',','.'};
- char_separator_type sep(sep_chars);
- tokenizer tok(s,sep);
- for(tokenizer_iterator beg=tok.begin(); beg!=tok.end();++beg){
- switch(pos) {
- case 0: {
- hour = boost::lexical_cast<int>(*beg);
- break;
- }
- case 1: {
- min = boost::lexical_cast<unsigned short>(*beg);
- break;
- }
- case 2: {
- sec = boost::lexical_cast<unsigned short>(*beg);
- break;
- }
- case 3: {
- int digits = static_cast<int>(beg->length());
-
-
- #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
-
-
-
- time_duration td(hour,min,sec,fs);
- int precision = td.num_fractional_digits();
-
- if(digits >= precision) {
-
- fs = _atoi64(beg->substr(0, precision).c_str());
- }
- else {
- fs = _atoi64(beg->c_str());
- }
- #else
- int precision = time_duration::num_fractional_digits();
- if(digits >= precision) {
-
- fs = boost::lexical_cast<boost::int64_t>(beg->substr(0, precision));
- }
- else {
- fs = boost::lexical_cast<boost::int64_t>(*beg);
- }
- #endif
- if(digits < precision){
-
-
-
- fs *= power(10, precision - digits);
- }
-
- break;
- }
- default: break;
- }
- pos++;
- }
- if(is_neg) {
- return -time_duration(hour, min, sec, fs);
- }
- else {
- return time_duration(hour, min, sec, fs);
- }
- }
-
-
-
- template<class time_duration>
- inline
- time_duration
- parse_delimited_time_duration(const std::string& s)
- {
- return str_from_delimited_time_duration<time_duration,char>(s);
- }
-
- inline
- bool
- split(const std::string& s,
- char sep,
- std::string& first,
- std::string& second)
- {
- std::string::size_type sep_pos = s.find(sep);
- first = s.substr(0,sep_pos);
- if (sep_pos!=std::string::npos)
- second = s.substr(sep_pos+1);
- return true;
- }
- template<class time_type>
- inline
- time_type
- parse_delimited_time(const std::string& s, char sep)
- {
- typedef typename time_type::time_duration_type time_duration;
- typedef typename time_type::date_type date_type;
-
- std::string date_string, tod_string;
- split(s, sep, date_string, tod_string);
-
- date_type d = parse_date<date_type>(date_string);
-
- time_duration td = parse_delimited_time_duration<time_duration>(tod_string);
-
- return time_type(d, td);
- }
-
- template<class time_duration>
- inline
- time_duration
- parse_undelimited_time_duration(const std::string& s)
- {
- int precision = 0;
- {
-
-
-
- time_duration tmp(0,0,0,1);
- precision = tmp.num_fractional_digits();
- }
-
- int offsets[] = {2,2,2, precision+1};
- int pos = 0, sign = 0;
- int hours = 0;
- short min=0, sec=0;
- boost::int64_t fs=0;
-
- if(s.at(sign) == '-')
- {
- ++sign;
- }
-
-
- std::string remain = s.substr(sign);
-
- bool wrap_off = false;
- bool ret_part = true;
- boost::offset_separator osf(offsets, offsets+4, wrap_off, ret_part);
- typedef boost::tokenizer<boost::offset_separator,
- std::basic_string<char>::const_iterator,
- std::basic_string<char> > tokenizer;
- typedef boost::tokenizer<boost::offset_separator,
- std::basic_string<char>::const_iterator,
- std::basic_string<char> >::iterator tokenizer_iterator;
- tokenizer tok(remain, osf);
- for(tokenizer_iterator ti=tok.begin(); ti!=tok.end();++ti){
- switch(pos) {
- case 0:
- {
- hours = boost::lexical_cast<int>(*ti);
- break;
- }
- case 1:
- {
- min = boost::lexical_cast<short>(*ti);
- break;
- }
- case 2:
- {
- sec = boost::lexical_cast<short>(*ti);
- break;
- }
- case 3:
- {
- std::string char_digits(ti->substr(1));
- int digits = static_cast<int>(char_digits.length());
-
-
-
- #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200))
-
- if(digits >= precision) {
-
- fs = _atoi64(char_digits.substr(0, precision).c_str());
- }
- else if(digits == 0) {
- fs = 0;
- }
- else {
- fs = _atoi64(char_digits.c_str());
- }
- #else
- if(digits >= precision) {
-
- fs = boost::lexical_cast<boost::int64_t>(char_digits.substr(0, precision));
- }
- else if(digits == 0) {
- fs = 0;
- }
- else {
- fs = boost::lexical_cast<boost::int64_t>(char_digits);
- }
- #endif
- if(digits < precision){
-
-
-
- fs *= power(10, precision - digits);
- }
-
- break;
- }
- default: break;
- }
- pos++;
- }
- if(sign) {
- return -time_duration(hours, min, sec, fs);
- }
- else {
- return time_duration(hours, min, sec, fs);
- }
- }
-
- template<class time_type>
- inline
- time_type
- parse_iso_time(const std::string& s, char sep)
- {
- typedef typename time_type::time_duration_type time_duration;
- typedef typename time_type::date_type date_type;
- typedef special_values_parser<date_type, std::string::value_type> svp_type;
-
-
-
-
- if (svp_type::likely(s)) {
- typedef typename svp_type::stringstream_type ss_type;
- typedef typename svp_type::stream_itr_type itr_type;
- typedef typename svp_type::match_results mr_type;
- svp_type p;
- mr_type mr;
- ss_type ss(s);
- itr_type itr(ss);
- itr_type end;
- if (p.match(itr, end, mr)) {
- return time_type(static_cast<special_values>(mr.current_match));
- }
- }
-
- std::string date_string, tod_string;
- split(s, sep, date_string, tod_string);
-
- date_type d = parse_undelimited_date<date_type>(date_string);
-
- time_duration td = parse_undelimited_time_duration<time_duration>(tod_string);
-
- return time_type(d, td);
- }
- } }
- #endif
|