Blender V4.3
time.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "util/time.h"
6
7#include <stdlib.h>
8
9#if !defined(_WIN32)
10# include <sys/time.h>
11# include <unistd.h>
12#endif
13
14#include "util/math.h"
15#include "util/string.h"
16#include "util/windows.h"
17
19
20#ifdef _WIN32
21double time_dt()
22{
23 __int64 frequency, counter;
24
25 QueryPerformanceFrequency((LARGE_INTEGER *)&frequency);
26 QueryPerformanceCounter((LARGE_INTEGER *)&counter);
27
28 return (double)counter / (double)frequency;
29}
30
31void time_sleep(double t)
32{
33 Sleep((int)(t * 1000));
34}
35#else
36double time_dt()
37{
38 struct timeval now;
39 gettimeofday(&now, NULL);
40
41 return now.tv_sec + now.tv_usec * 1e-6;
42}
43
44/* sleep t seconds */
45void time_sleep(double t)
46{
47 /* get whole seconds */
48 int s = (int)t;
49
50 if (s >= 1) {
51 sleep(s);
52
53 /* adjust parameter to remove whole seconds */
54 t -= s;
55 }
56
57 /* get microseconds */
58 int us = (int)(t * 1e6);
59 if (us > 0) {
60 usleep(us);
61 }
62}
63#endif
64
65/* Time in format "hours:minutes:seconds.hundreds" */
66
67string time_human_readable_from_seconds(const double seconds)
68{
69 const int h = (((int)seconds) / (60 * 60));
70 const int m = (((int)seconds) / 60) % 60;
71 const int s = (((int)seconds) % 60);
72 const int r = (((int)(seconds * 100)) % 100);
73
74 if (h > 0) {
75 return string_printf("%.2d:%.2d:%.2d.%.2d", h, m, s, r);
76 }
77 else {
78 return string_printf("%.2d:%.2d.%.2d", m, s, r);
79 }
80}
81
82double time_human_readable_to_seconds(const string &time_string)
83{
84 /* Those are multiplies of a corresponding token surrounded by : in the
85 * time string, which denotes how to convert value to seconds.
86 * Effectively: seconds, minutes, hours, days in seconds. */
87 const int multipliers[] = {1, 60, 60 * 60, 24 * 60 * 60};
88 const int num_multiplies = sizeof(multipliers) / sizeof(*multipliers);
89 if (time_string.empty()) {
90 return 0.0;
91 }
92 double result = 0.0;
93 /* Split fractions of a second from the encoded time. */
94 vector<string> fraction_tokens;
95 string_split(fraction_tokens, time_string, ".", false);
96 const int num_fraction_tokens = fraction_tokens.size();
97 if (num_fraction_tokens == 0) {
98 /* Time string is malformed. */
99 return 0.0;
100 }
101 else if (fraction_tokens.size() == 1) {
102 /* There is no fraction of a second specified, the rest of the code
103 * handles this normally. */
104 }
105 else if (fraction_tokens.size() == 2) {
106 result = atof(fraction_tokens[1].c_str());
107 result *= ::pow(0.1, fraction_tokens[1].length());
108 }
109 else {
110 /* This is not a valid string, the result can not be reliable. */
111 return 0.0;
112 }
113 /* Split hours, minutes and seconds.
114 * Hours part is optional. */
115 vector<string> tokens;
116 string_split(tokens, fraction_tokens[0], ":", false);
117 const int num_tokens = tokens.size();
118 if (num_tokens > num_multiplies) {
119 /* Can not reliably represent the value. */
120 return 0.0;
121 }
122 for (int i = 0; i < num_tokens; ++i) {
123 result += atoi(tokens[num_tokens - i - 1].c_str()) * multipliers[i];
124 }
125 return result;
126}
127
typedef double(DMatrix)[4][4]
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
pow(value.r - subtrahend, 2.0)") .do_static_compilation(true)
#define CCL_NAMESPACE_END
#define NULL
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:23
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
Definition string.cpp:70
void time_sleep(double t)
Definition time.cpp:45
double time_human_readable_to_seconds(const string &time_string)
Definition time.cpp:82
string time_human_readable_from_seconds(const double seconds)
Definition time.cpp:67
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:36