fw4spl
dateAndTime.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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/dateAndTime.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <boost/lexical_cast.hpp>
12 #include <boost/regex.hpp>
13 
14 namespace fwTools
15 {
16 
17 //------------------------------------------------------------------------------
18 
19 ::boost::gregorian::date strToBoostDate( const std::string& dateStr )
20 {
21  ::boost::gregorian::date resDate( ::boost::gregorian::from_undelimited_string( "19000101" ) );
22  if ( dateStr.size() < 8 )
23  {
24  OSLM_WARN(
25  "The string length is too short (<8) : " << dateStr <<
26  ". The string is initialized with \"19000101\".");
27  }
28  else if ( dateStr.size() > 8 )
29  {
30  OSLM_WARN("The string length is too long (>8) : " << dateStr << ".. The string is trunked to 8 characters.");
31  resDate = strToBoostDate( dateStr.substr(0, 8) );
32  }
33  else
34  {
35  ::boost::regex isNumber("[0-9]+");
36 
37  if( ::boost::regex_match(dateStr, isNumber) )
38  {
39  resDate = ::boost::gregorian::date( ::boost::gregorian::from_undelimited_string( dateStr ) );
40  }
41  else
42  {
43  OSLM_WARN(
44  "The string not contains 8 numbers : " << dateStr <<
45  ". The string is initialized with \"19000101\".");
46  }
47  }
48  return resDate;
49 }
50 
51 //------------------------------------------------------------------------------
52 
53 ::boost::posix_time::time_duration strToBoostTime( const std::string& timeStr )
54 {
55  using ::boost::posix_time::time_duration;
56  using ::boost::posix_time::hours;
57  using ::boost::posix_time::minutes;
58  using ::boost::posix_time::seconds;
59 
60  time_duration td;
61  if ( timeStr.size() < 6 )
62  {
63  OSLM_WARN("The string length is too short (<6) : " << timeStr <<
64  ". The string is initialized with \"000000\".");
65  td = hours(0) + minutes(0) + seconds(0);
66  }
67  else if ( timeStr.size() > 6 )
68  {
69  OSLM_WARN("The string length is too short (>6) : " << timeStr << ". This string is trunked.");
70  td = strToBoostTime( timeStr.substr(0, 6) );
71  }
72  else
73  {
74  ::boost::regex isNumber("[0-9]+");
75  if( ::boost::regex_match(timeStr, isNumber) )
76  {
77  std::uint16_t h = ::boost::lexical_cast< std::uint16_t > ( timeStr.substr(0, 2) );
78  std::uint16_t m = ::boost::lexical_cast< std::uint16_t > ( timeStr.substr(2, 2) );
79  std::uint16_t s = ::boost::lexical_cast< std::uint16_t > ( timeStr.substr(4, 2) );
80  td = hours(h) + minutes(m) + seconds(s);
81  }
82  else
83  {
84  OSLM_WARN("The string not contains 6 numbers : " << timeStr <<
85  ". The string is initialized with \"000000\".");
86  td = hours(0) + minutes(0) + seconds(0);
87  }
88  }
89  return td;
90 }
91 
92 //------------------------------------------------------------------------------
93 
94 ::boost::posix_time::ptime strToBoostDateAndTime( const std::string& dateStr, const std::string& timeStr)
95 {
96  return ::boost::posix_time::ptime(strToBoostDate(dateStr), strToBoostTime(timeStr));
97 }
98 
99 //------------------------------------------------------------------------------
100 
101 std::string getDate( const ::boost::posix_time::ptime& dateAndTime )
102 {
103  std::string dateAndTimeStr = ::boost::posix_time::to_iso_string(dateAndTime);
104  return dateAndTimeStr.substr(0, 8);
105 }
106 
107 //------------------------------------------------------------------------------
108 
109 std::string getTime( const ::boost::posix_time::ptime& dateAndTime )
110 {
111  std::string dateAndTimeStr = ::boost::posix_time::to_iso_string(dateAndTime);
112  return dateAndTimeStr.substr(9, 6);
113 }
114 
115 //------------------------------------------------------------------------------
116 
117 std::string getCurrentTime()
118 {
119  ::boost::posix_time::ptime now = ::boost::posix_time::second_clock::local_time();
120  std::string nowStr = ::boost::posix_time::to_simple_string(now);
121  return nowStr.substr(0, 21);
122 }
123 
124 }
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
FWTOOLS_API std::string getCurrentTime()
Return the current clock.
FWTOOLS_API::boost::gregorian::date strToBoostDate(const std::string &dateStr)
Convert a string date to a boost date.
Definition: dateAndTime.cpp:19
#define OSLM_WARN(message)
Definition: spyLog.hpp:263
FWTOOLS_API::boost::posix_time::time_duration strToBoostTime(const std::string &timeStr)
Convert a string time to a boost time.
Definition: dateAndTime.cpp:53
FWTOOLS_API std::string getDate(const ::boost::posix_time::ptime &dateAndTime)
Convert a boost time to a string date.
FWTOOLS_API std::string getTime(const ::boost::posix_time::ptime &dateAndTime)
Convert a boost time to a string time.
FWTOOLS_API::boost::posix_time::ptime strToBoostDateAndTime(const std::string &dateStr, const std::string &timeStr="000000")
Convert string data time to a boost time.
Definition: dateAndTime.cpp:94