Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Sharybin <sergey.vfx@gmail.com>2019-03-19 17:19:22 +0300
committerBrecht Van Lommel <brechtvanlommel@gmail.com>2019-03-19 19:38:43 +0300
commit5b7b7101c81ca104111e0df76cccf5b1c88bd3f6 (patch)
treed26d5295a226e14c904e3bb904cb51a1292a3b3a /intern/cycles/util
parent01df4818a6e1d3b93517e48a617310481abd9339 (diff)
Cycles: Implement function to format and parse human readable time
Gives value in seconds for a string which is encoded in format HH:MM:SS.hh.
Diffstat (limited to 'intern/cycles/util')
-rw-r--r--intern/cycles/util/util_time.cpp88
-rw-r--r--intern/cycles/util/util_time.h11
2 files changed, 84 insertions, 15 deletions
diff --git a/intern/cycles/util/util_time.cpp b/intern/cycles/util/util_time.cpp
index bc6ac4e2099..9983fdd1df3 100644
--- a/intern/cycles/util/util_time.cpp
+++ b/intern/cycles/util/util_time.cpp
@@ -14,15 +14,22 @@
* limitations under the License.
*/
+#include "util/util_time.h"
+
#include <stdlib.h>
-#include "util/util_time.h"
-#include "util/util_windows.h"
+#if !defined(_WIN32)
+# include <sys/time.h>
+# include <unistd.h>
+#endif
-#ifdef _WIN32
+#include "util/util_math.h"
+#include "util/util_string.h"
+#include "util/util_windows.h"
CCL_NAMESPACE_BEGIN
+#ifdef _WIN32
double time_dt()
{
__int64 frequency, counter;
@@ -37,16 +44,7 @@ void time_sleep(double t)
{
Sleep((int)(t*1000));
}
-
-CCL_NAMESPACE_END
-
#else
-
-#include <sys/time.h>
-#include <unistd.h>
-
-CCL_NAMESPACE_BEGIN
-
double time_dt()
{
struct timeval now;
@@ -73,7 +71,69 @@ void time_sleep(double t)
if(us > 0)
usleep(us);
}
+#endif
-CCL_NAMESPACE_END
+/* Time in format "hours:minutes:seconds.hundreds" */
-#endif
+string time_human_readable_from_seconds(const double seconds)
+{
+ const int h = (((int)seconds) / (60 * 60));
+ const int m = (((int)seconds) / 60) % 60;
+ const int s = (((int)seconds) % 60);
+ const int r = (((int)(seconds * 100)) % 100);
+
+ if(h > 0) {
+ return string_printf("%.2d:%.2d:%.2d.%.2d", h, m, s, r);
+ }
+ else {
+ return string_printf("%.2d:%.2d.%.2d", m, s, r);
+ }
+}
+
+double time_human_readable_to_seconds(const string& time_string)
+{
+ /* Those are multiplies of a corresponding token surrounded by : in the
+ * time string, which denotes how to convert value to seconds.
+ * Effectively: seconds, minutes, hours, days in seconds. */
+ const int multipliers[] = {1, 60, 60*60, 24*60*60};
+ const int num_multiplies = sizeof(multipliers) / sizeof(*multipliers);
+ if(time_string.empty()) {
+ return 0.0;
+ }
+ double result = 0.0;
+ /* Split fractions of a second from the encoded time. */
+ vector<string> fraction_tokens;
+ string_split(fraction_tokens, time_string, ".", false);
+ const int num_fraction_tokens = fraction_tokens.size();
+ if(num_fraction_tokens == 0) {
+ /* Time string is malformed. */
+ return 0.0;
+ }
+ else if(fraction_tokens.size() == 1) {
+ /* There is no fraction of a second specified, the rest of the code
+ * handles this normally. */
+ }
+ else if(fraction_tokens.size() == 2) {
+ result = atof(fraction_tokens[1].c_str());
+ result *= pow(0.1, fraction_tokens[1].length());
+ }
+ else {
+ /* This is not a valid string, the result can not be reliable. */
+ return 0.0;
+ }
+ /* Split hours, minutes and seconds.
+ * Hours part is optional. */
+ vector<string> tokens;
+ string_split(tokens, fraction_tokens[0], ":", false);
+ const int num_tokens = tokens.size();
+ if(num_tokens > num_multiplies) {
+ /* Can not reliably represent the value. */
+ return 0.0;
+ }
+ for(int i = 0; i < num_tokens; ++i) {
+ result += atoi(tokens[num_tokens - i - 1].c_str()) * multipliers[i];
+ }
+ return result;
+}
+
+CCL_NAMESPACE_END
diff --git a/intern/cycles/util/util_time.h b/intern/cycles/util/util_time.h
index 13281bf188b..ed4dd5154d7 100644
--- a/intern/cycles/util/util_time.h
+++ b/intern/cycles/util/util_time.h
@@ -17,16 +17,20 @@
#ifndef __UTIL_TIME_H__
#define __UTIL_TIME_H__
+#include "util/util_string.h"
+
CCL_NAMESPACE_BEGIN
/* Give current time in seconds in double precision, with good accuracy. */
double time_dt();
-/* Sleep for the specified number of seconds */
+/* Sleep for the specified number of seconds. */
void time_sleep(double t);
+/* Scoped timer. */
+
class scoped_timer {
public:
explicit scoped_timer(double *value = NULL) : value_(value)
@@ -56,6 +60,11 @@ protected:
double time_start_;
};
+/* Make human readable string from time, compatible with Blender metadata. */
+
+string time_human_readable_from_seconds(const double seconds);
+double time_human_readable_to_seconds(const string& str);
+
CCL_NAMESPACE_END
#endif