go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
elastix.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright UMC Utrecht and contributors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef __elastix_h
19#define __elastix_h
20
21#include <cassert>
22#include <ctime>
23#include <cmath> // For fmod.
24#include <iomanip> // std::setprecision
25#include <sstream>
26#include <string>
27
38void PrintHelp( void );
39
43inline
44std::string
45ConvertSecondsToDHMS( const double totalSeconds, const unsigned int precision = 0 )
46{
48 const std::size_t secondsPerMinute = 60;
49 const std::size_t secondsPerHour = 60 * secondsPerMinute;
50 const std::size_t secondsPerDay = 24 * secondsPerHour;
51
53 std::size_t iSeconds = static_cast< std::size_t >( totalSeconds );
54 const std::size_t days = iSeconds / secondsPerDay;
55
56 iSeconds %= secondsPerDay;
57 const std::size_t hours = iSeconds / secondsPerHour;
58
59 iSeconds %= secondsPerHour;
60 const std::size_t minutes = iSeconds / secondsPerMinute;
61
62 //iSeconds %= secondsPerMinute;
63 //const std::size_t seconds = iSeconds;
64 const double dSeconds = std::fmod( totalSeconds, 60.0 );
65
67 bool nonzero = false;
68 std::ostringstream make_string( "" );
69 if( days != 0 ) { make_string << days << "d"; nonzero = true; }
70 if( hours != 0 || nonzero ) { make_string << hours << "h"; nonzero = true; }
71 if( minutes != 0 || nonzero ) { make_string << minutes << "m"; nonzero = true; }
72 make_string << std::showpoint << std::fixed << std::setprecision( precision );
73 make_string << dSeconds << "s";
74
76 return make_string.str();
77
78} // end ConvertSecondsToDHMS()
79
80
82inline
83std::string
85{
86 // Obtain current time
87 const std::time_t rawtime{ std::time( nullptr ) };
88
89 // Convert to local time
90 // Note: std::localtime is not threadsafe!
91 const std::tm* const localTimePtr{ std::localtime( &rawtime ) };
92
93 if (localTimePtr == nullptr)
94 {
95 assert(!"std::localtime should not return null!");
96 return {};
97 }
98
99 // Make a copy of the internal object from std::localtime, to reduce the
100 // risk of a race condition.
101 const std::tm localTimeValue( *localTimePtr );
102
103 constexpr std::size_t maxNumberOfChars{ 32 };
104 char timeAsString[maxNumberOfChars]{};
105 static_assert(maxNumberOfChars > sizeof("Thu Aug 23 14:55:02 2001"),
106 "timeAsString should be large enough to hold a typical example date and time");
107
108 if (std::strftime(timeAsString, maxNumberOfChars, "%c", &localTimeValue) == 0)
109 {
110 assert(!"std::strftime has failed!");
111 return {};
112 }
113
114 return timeAsString;
115} // end GetCurrentDateAndTime()
116
117
118#endif
void PrintHelp(void)
std::string ConvertSecondsToDHMS(const double totalSeconds, const unsigned int precision=0)
Definition: elastix.h:45
std::string GetCurrentDateAndTime(void)
Definition: elastix.h:84


Generated on 1667476801 for elastix by doxygen 1.9.4 elastix logo