A library for working with phylogenetic and population genetic data.
v0.32.0
date_time.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_DATE_TIME_H_
2 #define GENESIS_UTILS_TOOLS_DATE_TIME_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <ctime>
35 #include <string>
36 #include <vector>
37 
38 namespace genesis {
39 namespace utils {
40 
41 // =================================================================================================
42 // Convenience Functions
43 // =================================================================================================
44 
48 std::string current_date();
49 
53 std::string current_time();
54 
61 std::time_t tm_to_time( std::tm time, bool use_local_time = false );
62 
69 std::vector<std::time_t> tm_to_time( std::vector<std::tm> const& times, bool use_local_time = false );
70 
79 template<typename ForwardIterator>
80 std::vector<std::time_t> tm_to_time(
81  ForwardIterator first,
82  ForwardIterator last,
83  bool use_local_time = false,
84  size_t size = 0
85 ) {
86  // Prep.
87  std::vector<std::time_t> ret;
88  ret.reserve( size );
89 
90  // Add all values. Throws on error.
91  while( first != last ) {
92  ret.push_back( tm_to_time( *first, use_local_time ));
93  ++first;
94  }
95  return ret;
96 }
97 
104 std::tm time_to_tm( std::time_t const& time, bool use_local_time = false );
105 
112 std::vector<std::tm> time_to_tm( std::vector<std::time_t> const& times, bool use_local_time = false );
113 
122 template<typename ForwardIterator>
123 std::vector<std::tm> time_to_tm(
124  ForwardIterator first,
125  ForwardIterator last,
126  bool use_local_time = false,
127  size_t size = 0
128 ) {
129  // Prep.
130  std::vector<std::tm> ret;
131  ret.reserve( size );
132 
133  // Add all values. Throws on error.
134  while( first != last ) {
135  ret.push_back( time_to_tm( *first, use_local_time ));
136  ++first;
137  }
138  return ret;
139 }
140 
141 // =================================================================================================
142 // Date/Time Conversion from std::tm
143 // =================================================================================================
144 
154 std::string tm_to_string( std::tm const& time, std::string const& format, std::string const& locale );
155 
161 std::string tm_to_string( std::tm const& time, std::string const& format );
162 
169 std::string tm_to_string( std::tm const& time );
170 
178 std::string tm_date_to_string( std::tm const& time );
179 
187 std::string tm_time_to_string( std::tm const& time );
188 
189 // =================================================================================================
190 // Date/Time Conversion to std::tm
191 // =================================================================================================
192 
202 std::tm convert_to_tm( std::string const& str, std::string const& format, std::string const& locale );
203 
215 std::tm convert_to_tm( std::string const& str, std::string const& format );
216 
250 std::tm convert_to_tm( std::string const& str );
251 
259 template<typename ForwardIterator>
260 std::vector<std::tm> convert_to_tm(
261  ForwardIterator first,
262  ForwardIterator last,
263  std::string const& format,
264  std::string const& locale,
265  size_t size = 0
266 ) {
267  // Prep.
268  std::vector<std::tm> ret;
269  ret.reserve( size );
270 
271  // Add all values. Throws on error.
272  while( first != last ) {
273  ret.push_back( convert_to_tm( *first, format, locale ));
274  ++first;
275  }
276  return ret;
277 }
278 
286 template<typename ForwardIterator>
287 std::vector<std::tm> convert_to_tm(
288  ForwardIterator first,
289  ForwardIterator last,
290  std::string const& format,
291  size_t size = 0
292 ) {
293  // Prep.
294  std::vector<std::tm> ret;
295  ret.reserve( size );
296 
297  // Add all values. Throws on error.
298  while( first != last ) {
299  ret.push_back( convert_to_tm( *first, format ));
300  ++first;
301  }
302  return ret;
303 }
304 
313 template<typename ForwardIterator>
314 std::vector<std::tm> convert_to_tm(
315  ForwardIterator first,
316  ForwardIterator last,
317  size_t size = 0
318 ) {
319  // Prep.
320  std::vector<std::tm> ret;
321  ret.reserve( size );
322 
323  // Add all values. Throws on error.
324  while( first != last ) {
325  ret.push_back( convert_to_tm( *first ));
326  ++first;
327  }
328  return ret;
329 }
330 
337 bool is_convertible_to_tm( std::string const& str, std::string const& format, std::string const& locale );
338 
345 bool is_convertible_to_tm( std::string const& str, std::string const& format );
346 
353 bool is_convertible_to_tm( std::string const& str );
354 
360 template<typename ForwardIterator>
362  ForwardIterator first,
363  ForwardIterator last,
364  std::string const& format,
365  std::string const& locale
366 ) {
367  // If one of the conversions fails, the list is not convertible.
368  while( first != last ) {
369  if( ! is_convertible_to_tm( *first, format, locale )) {
370  return false;
371  }
372  ++first;
373  }
374  return true;
375 }
376 
382 template<typename ForwardIterator>
384  ForwardIterator first,
385  ForwardIterator last,
386  std::string const& format
387 ) {
388  // If one of the conversions fails, the list is not convertible.
389  while( first != last ) {
390  if( ! is_convertible_to_tm( *first, format )) {
391  return false;
392  }
393  ++first;
394  }
395  return true;
396 }
397 
404 template<typename ForwardIterator>
406  ForwardIterator first,
407  ForwardIterator last
408 ) {
409  // If one of the conversions fails, the list is not convertible.
410  while( first != last ) {
411  if( ! is_convertible_to_tm( *first )) {
412  return false;
413  }
414  ++first;
415  }
416  return true;
417 }
418 
419 } // namespace utils
420 } // namespace genesis
421 
422 #endif // include guard
genesis::utils::convert_to_tm
std::tm convert_to_tm(std::string const &str, std::string const &format, std::string const &locale)
Convert a std::string to a std::tm date/time object, if possible. Throw otherwise.
Definition: date_time.cpp:314
genesis::utils::current_date
std::string current_date()
Returns the current date as a string in the format "2014-12-31".
Definition: date_time.cpp:68
genesis::utils::current_time
std::string current_time()
Returns the current time as a string in the format "13:37:42".
Definition: date_time.cpp:88
genesis::utils::tm_date_to_string
std::string tm_date_to_string(std::tm const &time)
Print the given std::tm object as a std::string containing only the date, using the ISO 8601 extended...
Definition: date_time.cpp:213
genesis::utils::time_to_tm
std::tm time_to_tm(std::time_t const &time, bool use_local_time)
Convert std::time_t object to a std::tm object.
Definition: date_time.cpp:147
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::tm_to_time
std::time_t tm_to_time(std::tm time, bool use_local_time)
Convert std::tm object to a std::time_t object.
Definition: date_time.cpp:108
genesis::utils::tm_time_to_string
std::string tm_time_to_string(std::tm const &time)
Print the given std::tm object as a std::string containing only the time, using the ISO 8601 extended...
Definition: date_time.cpp:218
genesis::utils::tm_to_string
std::string tm_to_string(std::tm const &time, std::string const &format, std::string const &locale)
Print the given std::tm object as a std::string, using the format and locale.
Definition: date_time.cpp:179
genesis::utils::is_convertible_to_tm
bool is_convertible_to_tm(std::string const &str, std::string const &format, std::string const &locale)
Return whether a std::string is convertible to a std::tm date/time object, that is,...
Definition: date_time.cpp:361