Chris@16: #ifndef _DATE_TIME_TIME_PARSING_HPP___ Chris@16: #define _DATE_TIME_TIME_PARSING_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include "boost/tokenizer.hpp" Chris@16: #include "boost/lexical_cast.hpp" Chris@16: #include "boost/date_time/date_parsing.hpp" Chris@16: #include "boost/cstdint.hpp" Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: //! computes exponential math like 2^8 => 256, only works with positive integers Chris@16: //Not general purpose, but needed b/c std::pow is not available Chris@16: //everywehere. Hasn't been tested with negatives and zeros Chris@16: template Chris@16: inline Chris@16: int_type power(int_type base, int_type exponent) Chris@16: { Chris@16: int_type result = 1; Chris@16: for(int i = 0; i < exponent; ++i){ Chris@16: result *= base; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: //! Creates a time_duration object from a delimited string Chris@16: /*! Expected format for string is "[-]h[h][:mm][:ss][.fff]". Chris@16: * If the number of fractional digits provided is greater than the Chris@16: * precision of the time duration type then the extra digits are Chris@16: * truncated. Chris@16: * Chris@16: * A negative duration will be created if the first character in Chris@16: * string is a '-', all other '-' will be treated as delimiters. Chris@16: * Accepted delimiters are "-:,.". Chris@16: */ Chris@16: template Chris@16: inline Chris@16: time_duration Chris@16: str_from_delimited_time_duration(const std::basic_string& s) Chris@16: { Chris@16: unsigned short min=0, sec =0; Chris@16: int hour =0; Chris@16: bool is_neg = (s.at(0) == '-'); Chris@16: boost::int64_t fs=0; Chris@16: int pos = 0; Chris@16: Chris@16: typedef typename std::basic_string::traits_type traits_type; Chris@16: typedef boost::char_separator char_separator_type; Chris@16: typedef boost::tokenizer::const_iterator, Chris@16: std::basic_string > tokenizer; Chris@16: typedef typename boost::tokenizer::const_iterator, Chris@16: typename std::basic_string >::iterator tokenizer_iterator; Chris@16: Chris@16: char_type sep_chars[5] = {'-',':',',','.'}; Chris@16: char_separator_type sep(sep_chars); Chris@16: tokenizer tok(s,sep); Chris@16: for(tokenizer_iterator beg=tok.begin(); beg!=tok.end();++beg){ Chris@16: switch(pos) { Chris@16: case 0: { Chris@16: hour = boost::lexical_cast(*beg); Chris@16: break; Chris@16: } Chris@16: case 1: { Chris@16: min = boost::lexical_cast(*beg); Chris@16: break; Chris@16: } Chris@16: case 2: { Chris@16: sec = boost::lexical_cast(*beg); Chris@16: break; Chris@16: }; Chris@16: case 3: { Chris@16: int digits = static_cast(beg->length()); Chris@16: //Works around a bug in MSVC 6 library that does not support Chris@16: //operator>> thus meaning lexical_cast will fail to compile. Chris@16: #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) Chris@16: // msvc wouldn't compile 'time_duration::num_fractional_digits()' Chris@16: // (required template argument list) as a workaround a temp Chris@16: // time_duration object was used Chris@16: time_duration td(hour,min,sec,fs); Chris@16: int precision = td.num_fractional_digits(); Chris@16: // _atoi64 is an MS specific function Chris@16: if(digits >= precision) { Chris@16: // drop excess digits Chris@16: fs = _atoi64(beg->substr(0, precision).c_str()); Chris@16: } Chris@16: else { Chris@16: fs = _atoi64(beg->c_str()); Chris@16: } Chris@16: #else Chris@16: int precision = time_duration::num_fractional_digits(); Chris@16: if(digits >= precision) { Chris@16: // drop excess digits Chris@16: fs = boost::lexical_cast(beg->substr(0, precision)); Chris@16: } Chris@16: else { Chris@16: fs = boost::lexical_cast(*beg); Chris@16: } Chris@16: #endif Chris@16: if(digits < precision){ Chris@16: // trailing zeros get dropped from the string, Chris@16: // "1:01:01.1" would yield .000001 instead of .100000 Chris@16: // the power() compensates for the missing decimal places Chris@16: fs *= power(10, precision - digits); Chris@16: } Chris@16: Chris@16: break; Chris@16: } Chris@16: default: break; Chris@16: }//switch Chris@16: pos++; Chris@16: } Chris@16: if(is_neg) { Chris@16: return -time_duration(hour, min, sec, fs); Chris@16: } Chris@16: else { Chris@16: return time_duration(hour, min, sec, fs); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Creates a time_duration object from a delimited string Chris@16: /*! Expected format for string is "[-]h[h][:mm][:ss][.fff]". Chris@16: * If the number of fractional digits provided is greater than the Chris@16: * precision of the time duration type then the extra digits are Chris@16: * truncated. Chris@16: * Chris@16: * A negative duration will be created if the first character in Chris@16: * string is a '-', all other '-' will be treated as delimiters. Chris@16: * Accepted delimiters are "-:,.". Chris@16: */ Chris@16: template Chris@16: inline Chris@16: time_duration Chris@16: parse_delimited_time_duration(const std::string& s) Chris@16: { Chris@16: return str_from_delimited_time_duration(s); Chris@16: } Chris@16: Chris@16: //! Utility function to split appart string Chris@16: inline Chris@16: bool Chris@16: split(const std::string& s, Chris@16: char sep, Chris@16: std::string& first, Chris@16: std::string& second) Chris@16: { Chris@16: std::string::size_type sep_pos = s.find(sep); Chris@16: first = s.substr(0,sep_pos); Chris@16: if (sep_pos!=std::string::npos) Chris@16: second = s.substr(sep_pos+1); Chris@16: return true; Chris@16: } Chris@16: Chris@16: Chris@16: template Chris@16: inline Chris@16: time_type Chris@16: parse_delimited_time(const std::string& s, char sep) Chris@16: { Chris@16: typedef typename time_type::time_duration_type time_duration; Chris@16: typedef typename time_type::date_type date_type; Chris@16: Chris@16: //split date/time on a unique delimiter char such as ' ' or 'T' Chris@16: std::string date_string, tod_string; Chris@16: split(s, sep, date_string, tod_string); Chris@16: //call parse_date with first string Chris@16: date_type d = parse_date(date_string); Chris@16: //call parse_time_duration with remaining string Chris@16: time_duration td = parse_delimited_time_duration(tod_string); Chris@16: //construct a time Chris@16: return time_type(d, td); Chris@16: Chris@16: } Chris@16: Chris@16: //! Parse time duration part of an iso time of form: [-]hhmmss[.fff...] (eg: 120259.123 is 12 hours, 2 min, 59 seconds, 123000 microseconds) Chris@16: template Chris@16: inline Chris@16: time_duration Chris@16: parse_undelimited_time_duration(const std::string& s) Chris@16: { Chris@16: int precision = 0; Chris@16: { Chris@16: // msvc wouldn't compile 'time_duration::num_fractional_digits()' Chris@16: // (required template argument list) as a workaround, a temp Chris@16: // time_duration object was used Chris@16: time_duration tmp(0,0,0,1); Chris@16: precision = tmp.num_fractional_digits(); Chris@16: } Chris@16: // 'precision+1' is so we grab all digits, plus the decimal Chris@16: int offsets[] = {2,2,2, precision+1}; Chris@16: int pos = 0, sign = 0; Chris@16: int hours = 0; Chris@16: short min=0, sec=0; Chris@16: boost::int64_t fs=0; Chris@16: // increment one position if the string was "signed" Chris@16: if(s.at(sign) == '-') Chris@16: { Chris@16: ++sign; Chris@16: } Chris@16: // stlport choked when passing s.substr() to tokenizer Chris@16: // using a new string fixed the error Chris@16: std::string remain = s.substr(sign); Chris@16: /* We do not want the offset_separator to wrap the offsets, we Chris@16: * will never want to process more than: Chris@16: * 2 char, 2 char, 2 char, frac_sec length. Chris@16: * We *do* want the offset_separator to give us a partial for the Chris@16: * last characters if there were not enough provided in the input string. */ Chris@16: bool wrap_off = false; Chris@16: bool ret_part = true; Chris@16: boost::offset_separator osf(offsets, offsets+4, wrap_off, ret_part); Chris@16: typedef boost::tokenizer::const_iterator, Chris@16: std::basic_string > tokenizer; Chris@16: typedef boost::tokenizer::const_iterator, Chris@16: std::basic_string >::iterator tokenizer_iterator; Chris@16: tokenizer tok(remain, osf); Chris@16: for(tokenizer_iterator ti=tok.begin(); ti!=tok.end();++ti){ Chris@16: switch(pos) { Chris@16: case 0: Chris@16: { Chris@16: hours = boost::lexical_cast(*ti); Chris@16: break; Chris@16: } Chris@16: case 1: Chris@16: { Chris@16: min = boost::lexical_cast(*ti); Chris@16: break; Chris@16: } Chris@16: case 2: Chris@16: { Chris@16: sec = boost::lexical_cast(*ti); Chris@16: break; Chris@16: } Chris@16: case 3: Chris@16: { Chris@16: std::string char_digits(ti->substr(1)); // digits w/no decimal Chris@16: int digits = static_cast(char_digits.length()); Chris@16: Chris@16: //Works around a bug in MSVC 6 library that does not support Chris@16: //operator>> thus meaning lexical_cast will fail to compile. Chris@16: #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0 Chris@16: // _atoi64 is an MS specific function Chris@16: if(digits >= precision) { Chris@16: // drop excess digits Chris@16: fs = _atoi64(char_digits.substr(0, precision).c_str()); Chris@16: } Chris@16: else if(digits == 0) { Chris@16: fs = 0; // just in case _atoi64 doesn't like an empty string Chris@16: } Chris@16: else { Chris@16: fs = _atoi64(char_digits.c_str()); Chris@16: } Chris@16: #else Chris@16: if(digits >= precision) { Chris@16: // drop excess digits Chris@16: fs = boost::lexical_cast(char_digits.substr(0, precision)); Chris@16: } Chris@16: else if(digits == 0) { Chris@16: fs = 0; // lexical_cast doesn't like empty strings Chris@16: } Chris@16: else { Chris@16: fs = boost::lexical_cast(char_digits); Chris@16: } Chris@16: #endif Chris@16: if(digits < precision){ Chris@16: // trailing zeros get dropped from the string, Chris@16: // "1:01:01.1" would yield .000001 instead of .100000 Chris@16: // the power() compensates for the missing decimal places Chris@16: fs *= power(10, precision - digits); Chris@16: } Chris@16: Chris@16: break; Chris@16: } Chris@16: default: break; Chris@16: }; Chris@16: pos++; Chris@16: } Chris@16: if(sign) { Chris@16: return -time_duration(hours, min, sec, fs); Chris@16: } Chris@16: else { Chris@16: return time_duration(hours, min, sec, fs); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Parse time string of form YYYYMMDDThhmmss where T is delimeter between date and time Chris@16: template Chris@16: inline Chris@16: time_type Chris@16: parse_iso_time(const std::string& s, char sep) Chris@16: { Chris@16: typedef typename time_type::time_duration_type time_duration; Chris@16: typedef typename time_type::date_type date_type; Chris@16: Chris@16: //split date/time on a unique delimiter char such as ' ' or 'T' Chris@16: std::string date_string, tod_string; Chris@16: split(s, sep, date_string, tod_string); Chris@16: //call parse_date with first string Chris@16: date_type d = parse_undelimited_date(date_string); Chris@16: //call parse_time_duration with remaining string Chris@16: time_duration td = parse_undelimited_time_duration(tod_string); Chris@16: //construct a time Chris@16: return time_type(d, td); Chris@16: } Chris@16: Chris@16: Chris@16: Chris@16: } }//namespace date_time Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: #endif