fw4spl
fromIsoExtendedString.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwTools/fromIsoExtendedString.hpp"
8 
9 #include <sstream>
10 
11 namespace fwTools
12 {
13 
14 //------------------------------------------------------------------------------
15 
16 std::string getDateInXsdFormat( const boost::posix_time::ptime& dateAndTime )
17 {
18  std::string dateAndTimeStr = boost::posix_time::to_iso_extended_string(dateAndTime);
19  return dateAndTimeStr.substr(0, 10);
20 }
21 
22 //------------------------------------------------------------------------------
23 
24 ::boost::posix_time::ptime fromIsoExtendedString(const std::string& time_string)
25 {
26  std::string tmp(time_string);
27  std::string::size_type i = 0;
28  while ((i = tmp.find('T', i)) != (std::string::size_type)(-1))
29  {
30  tmp.replace(i++, 1, " ");
31  }
32  ::boost::posix_time::ptime t = boost::posix_time::time_from_string(tmp);
33  return t;
34 }
35 
36 //------------------------------------------------------------------------------
37 
38 ::boost::posix_time::ptime fromDateInXsdFormat(const std::string& date_string)
39 {
40  std::stringstream ss;
41  ss << date_string << " 00:00:00";
42 
43  ::boost::posix_time::ptime t = boost::posix_time::time_from_string(ss.str());
44  return t;
45 }
46 
47 //------------------------------------------------------------------------------
48 
49 std::string toIsoExtendedString(boost::posix_time::ptime ptime)
50 {
51  return boost::posix_time::to_iso_extended_string(ptime);
52 }
53 
54 }//end namespace
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
FWTOOLS_API boost::posix_time::ptime fromIsoExtendedString(const std::string &time_string)
Construct a boost ptime from a string in iso extended format (YYYY-MM-DDTHH:MM:SS) ...
FWTOOLS_API::boost::posix_time::ptime fromDateInXsdFormat(const std::string &date_string)
Construct a boost ptime from a string in xsd format (YYYY-MM-DD)
FWTOOLS_API std::string getDateInXsdFormat(const boost::posix_time::ptime &dateAndTime)
Construct a string in the format YYYY-MM-DD from a boost ptime.
FWTOOLS_API std::string toIsoExtendedString(boost::posix_time::ptime ptime)
Construct a string in iso extended format from a boost ptime.