6#ifndef file_fileTimes_hpp
7#define file_fileTimes_hpp
14#include <mx/mxlib.hpp>
16#include "../common/defaults.hpp"
18#include "../common/exceptions.hpp"
25#ifdef XWCTEST_NAMESPACE
43template <
class verboseT = XWC_DEFAULT_VERBOSITY>
53 #ifdef XWCTEST_TIMESTAMP_THROW_BAD_ALLOC
54 throw std::bad_alloc();
59 #ifdef XWCTEST_TIMESTAMP_THROW_FORMAT_ERROR
60 throw std::format_error(
"testing format_error");
65 #ifdef XWCTEST_TIMESTAMP_THROW_EXCEPTION
66 throw std::exception();
70 tstamp = std::format(
"{:04}{:02}{:02}{:02}{:02}{:02}{:09}",
71 uttime.tm_year + 1900,
79 catch(
const std::bad_alloc &e )
81 std::throw_with_nested( mx::exception<verboseT>(mx::error_t::std_bad_alloc,
"from std::format") );
83 catch(
const std::format_error &e )
85 return mx::error_report<verboseT>( mx::error_t::std_format_error,
86 std::string(
"from std::format: " ) + e.what() );
88 catch(
const std::exception &e )
90 return mx::error_report<verboseT>( mx::error_t::std_exception, std::string(
"from std::format: " ) + e.what() );
93 return mx::error_t::noerror;
114template <
typename verboseT = XWC_DEFAULT_VERBOSITY>
121 memset( &uttime, 0,
sizeof( uttime ) );
124 if( gmtime_r( &ts_sec, &uttime ) == 0 )
127 #ifdef XWCTEST_TIMESTAMP_GMTIME_OTHER
134 return mx::error_report<verboseT>( mx::errno2error_t( errno ),
135 "error getting UT time (gmtime_r returned 0)" );
139 return mx::error_report<verboseT>( mx::error_t::error,
"error getting UT time (gmtime_r returned 0)" );
145 mx_error_return( timestamp<verboseT>( tstamp, uttime, ts_nsec ) );
149 std::throw_with_nested( mx::exception<verboseT>(mx::error_t::exception) );
171template <
class verboseT = XWC_DEFAULT_VERBOSITY>
178 memset( &uttime, 0,
sizeof( uttime ) );
182 mx_error_return( timestamp<verboseT>( tstamp, uttime, ts_sec, ts_nsec ) );
186 std::throw_with_nested( mx::exception<verboseT>( mx::error_t::exception));
208template <
class verboseT = XWC_DEFAULT_VERBOSITY>
210 std::string &relPath,
215 if(ts_sec == 0 && ts_nsec == 0)
217 return mx::error_report<verboseT>( mx::error_t::invalidarg,
"all 0 time");
221 memset( &uttime, 0,
sizeof( uttime ) );
226 #ifdef XWCTEST_FILETIMERELPATH_THROW_BAD_ALLOC
227 throw std::bad_alloc();
232 #ifdef XWCTEST_FILETIMERELPATH_THROW_FORMAT_ERROR
233 throw std::format_error(
"testing format_error");
238 #ifdef XWCTEST_FILETIMERELPATH_THROW_EXCEPTION
239 throw std::exception();
243 mx_error_check( timestamp<verboseT>( tstamp, uttime, ts_sec, ts_nsec ) );
245 relPath = std::format(
"{:04}_{:02}_{:02}", uttime.tm_year + 1900, uttime.tm_mon + 1, uttime.tm_mday );
247 return mx::error_t::noerror;
249 catch(
const std::bad_alloc &e )
251 std::throw_with_nested( mx::exception<verboseT>(mx::error_t::std_bad_alloc,
"getting relPath") );
253 catch(
const mx::exception<verboseT> & e )
255 std::throw_with_nested( mx::exception<verboseT>(e.code(),
"getting relPath") );
257 catch(
const std::format_error &e )
259 return mx::error_report<verboseT>( mx::error_t::std_format_error,
260 std::string(
"from std::format: " ) + e.what() );
262 catch(
const std::exception &e )
264 return mx::error_report<verboseT>( mx::error_t::std_exception, e.what() );
286template <
class verboseT = XWC_DEFAULT_VERBOSITY>
288 std::string &relPath,
289 const std::string &devName,
290 const std::string &ext,
295 std::string tstamp, tmprelpath;
299 mx_error_check( fileTimeRelPath<verboseT>( tstamp, tmprelpath, ts_sec, ts_nsec ) );
303 std::throw_with_nested( mx::exception<verboseT>(mx::error_t::exception));
309 #ifdef XWCTEST_FILETIMERELPATHSTRING_THROW_BAD_ALLOC
310 throw std::bad_alloc();
315 #ifdef XWCTEST_FILETIMERELPATHSTRING_THROW_EXCEPTION
316 throw std::exception();
320 relPath = devName +
'/' + tmprelpath;
321 fileName = devName +
'_' + tstamp +
'.' + ext;
323 return mx::error_t::noerror;
325 catch(
const std::bad_alloc &e )
327 std::throw_with_nested( mx::exception<verboseT>(mx::error_t::std_bad_alloc,
"std::string assembling paths" ) );
329 catch(
const std::exception &e )
331 return mx::error_report<verboseT>( mx::error_t::std_exception,
332 std::string(
"std::string assembling paths" ) + e.what() );
351template <
class verboseT = XWC_DEFAULT_VERBOSITY>
359 const std::string &tstamp
365 #ifdef XWCTEST_PARSETIMESTAMP_THROW_BAD_ALLOC
366 throw std::bad_alloc();
371 #ifdef XWCTEST_PARSETIMESTAMP_THROW_OUT_OF_RANGE
372 throw std::out_of_range(
"testing out of range");
377 #ifdef XWCTEST_PARSETIMESTAMP_THROW_EXCEPTION
378 throw std::exception();
382 if( tstamp.length() != 23 )
384 return mx::error_report<verboseT>( mx::error_t::invalidarg,
"timestamp does not have 23 characters" );
387 YYYY = tstamp.substr( 0, 4 );
388 MM = tstamp.substr( 4, 2 );
389 DD = tstamp.substr( 6, 2 );
390 hh = tstamp.substr( 8, 2 );
391 mm = tstamp.substr( 10, 2 );
392 ss = tstamp.substr( 12, 2 );
393 nn = tstamp.substr( 14, 9 );
395 return mx::error_t::noerror;
397 catch(
const std::bad_alloc &e )
399 std::throw_with_nested( mx::exception<verboseT>(mx::error_t::std_bad_alloc));
401 catch(
const std::out_of_range &e )
403 return mx::error_report<verboseT>( mx::error_t::std_out_of_range,
404 std::string(
"parsing timestamp" ) + e.what() );
406 catch(
const std::exception &e )
408 return mx::error_report<verboseT>( mx::error_t::std_exception, std::string(
"parsing timestamp" ) + e.what() );
429template <
class verboseT = XWC_DEFAULT_VERBOSITY>
438 const std::string &fname
444 #ifdef XWCTEST_PARSEFILEPATH_THROW_BAD_ALLOC
445 throw std::bad_alloc();
450 #ifdef XWCTEST_PARSEFILEPATH_THROW_EXCEPTION
451 throw std::exception();
455 size_t est = fname.rfind(
'.' );
456 if( est == std::string::npos )
464 dend = fname.rfind(
'_', est );
466 if( dend == std::string::npos )
469 dst = fname.rfind(
'/', est );
471 if( dst == std::string::npos )
487 dst = fname.rfind(
'/', dend );
489 if( dst == std::string::npos )
506 #ifdef XWCTEST_PARSEFILEPATH_THROW_OUT_OF_RANGE
507 throw std::out_of_range(
"testing out of range");
511 devName = fname.substr( dst, dend - dst );
519 if( dend - dst != 23 )
521 return mx::error_report<verboseT>( mx::error_t::invalidarg,
"timestamp does not have 23 characters" );
525 #ifdef XWCTEST_PARSEFILEPATH_TOO_SHORT
531 mx_error_return( parseTimestamp<verboseT>( YYYY, MM, DD, hh, mm, ss, nn, fname.substr( dst, dend - dst ) ) );
533 catch(
const std::bad_alloc &e )
535 std::throw_with_nested( mx::exception<verboseT>(mx::error_t::std_bad_alloc,
"parsing filepath") );
537 catch(
const mx::exception<verboseT> &e )
539 std::throw_with_nested( mx::exception<verboseT>(e.code(),
"parsing filepath") );
541 catch(
const std::out_of_range &e )
543 return mx::error_report<verboseT>( mx::error_t::std_out_of_range, e.what() );
545 catch(
const std::exception &e )
547 return mx::error_report<verboseT>( mx::error_t::std_exception, e.what() );
552#ifdef XWCTEST_NAMESPACE
#define XWCTEST_NAMESPACE
mx::error_t parseTimestamp(std::string &YYYY, std::string &MM, std::string &DD, std::string &hh, std::string &mm, std::string &ss, std::string &nn, const std::string &tstamp)
Parse a standard XWCTk timestamp string.
mx::error_t timestamp(std::string &tstamp, const tm &uttime, long ts_nsec)
Get the filename timestamp from the breakdown for a time.
mx::error_t parseFilePath(std::string &devName, std::string &YYYY, std::string &MM, std::string &DD, std::string &hh, std::string &mm, std::string &ss, std::string &nn, const std::string &fname)
Parse a standard XWCTk timestamp filepath.
mx::error_t fileTimeRelPath(std::string &tstamp, std::string &relPath, time_t ts_sec, long ts_nsec)
Get the timestamp and the relative path based on a time.