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
28
32inline std::string
33ConvertSecondsToDHMS(const double totalSeconds, const unsigned int precision = 0)
34{
36 const std::size_t secondsPerMinute = 60;
37 const std::size_t secondsPerHour = 60 * secondsPerMinute;
38 const std::size_t secondsPerDay = 24 * secondsPerHour;
39
41 std::size_t iSeconds = static_cast<std::size_t>(totalSeconds);
42 const std::size_t days = iSeconds / secondsPerDay;
43
44 iSeconds %= secondsPerDay;
45 const std::size_t hours = iSeconds / secondsPerHour;
46
47 iSeconds %= secondsPerHour;
48 const std::size_t minutes = iSeconds / secondsPerMinute;
49
50 // iSeconds %= secondsPerMinute;
51 // const std::size_t seconds = iSeconds;
52 const double dSeconds = std::fmod(totalSeconds, 60.0);
53
55 bool nonzero = false;
56 std::ostringstream make_string;
57 if (days != 0)
58 {
59 make_string << days << "d";
60 nonzero = true;
61 }
62 if (hours != 0 || nonzero)
63 {
64 make_string << hours << "h";
65 nonzero = true;
66 }
67 if (minutes != 0 || nonzero)
68 {
69 make_string << minutes << "m";
70 }
71 make_string << std::showpoint << std::fixed << std::setprecision(precision);
72 make_string << dSeconds << "s";
73
75 return make_string.str();
76
77} // end ConvertSecondsToDHMS()
78
79
81inline std::string
83{
84 // Obtain current time
85 const std::time_t rawtime{ std::time(nullptr) };
86
87 // Convert to local time
88 // Note: std::localtime is not threadsafe!
89 const std::tm * const localTimePtr{ std::localtime(&rawtime) };
90
91 if (localTimePtr == nullptr)
92 {
93 assert(!"std::localtime should not return null!");
94 return {};
95 }
96
97 // Make a copy of the internal object from std::localtime, to reduce the
98 // risk of a race condition.
99 const std::tm localTimeValue(*localTimePtr);
100
101 constexpr std::size_t maxNumberOfChars{ 32 };
102 char timeAsString[maxNumberOfChars]{};
103 static_assert(maxNumberOfChars > sizeof("Thu Aug 23 14:55:02 2001"),
104 "timeAsString should be large enough to hold a typical example date and time");
105
106 if (std::strftime(timeAsString, maxNumberOfChars, "%c", &localTimeValue) == 0)
107 {
108 assert(!"std::strftime has failed!");
109 return {};
110 }
111
112 return timeAsString;
113} // end GetCurrentDateAndTime()
114
115
116#endif
std::string ConvertSecondsToDHMS(const double totalSeconds, const unsigned int precision=0)
Definition elastix.h:33
std::string GetCurrentDateAndTime()
Definition elastix.h:82


Generated on 2024-07-17 for elastix by doxygen 1.11.0 (9b424b03c9833626cd435af22a444888fbbb192d) elastix logo