util_time.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "util/util_time.h"
  17. #include <stdlib.h>
  18. #if !defined(_WIN32)
  19. # include <sys/time.h>
  20. # include <unistd.h>
  21. #endif
  22. #include "util/util_math.h"
  23. #include "util/util_string.h"
  24. #include "util/util_windows.h"
  25. CCL_NAMESPACE_BEGIN
  26. #ifdef _WIN32
  27. double time_dt()
  28. {
  29. __int64 frequency, counter;
  30. QueryPerformanceFrequency((LARGE_INTEGER *)&frequency);
  31. QueryPerformanceCounter((LARGE_INTEGER *)&counter);
  32. return (double)counter / (double)frequency;
  33. }
  34. void time_sleep(double t)
  35. {
  36. Sleep((int)(t * 1000));
  37. }
  38. #else
  39. double time_dt()
  40. {
  41. struct timeval now;
  42. gettimeofday(&now, NULL);
  43. return now.tv_sec + now.tv_usec * 1e-6;
  44. }
  45. /* sleep t seconds */
  46. void time_sleep(double t)
  47. {
  48. /* get whole seconds */
  49. int s = (int)t;
  50. if (s >= 1) {
  51. sleep(s);
  52. /* adjust parameter to remove whole seconds */
  53. t -= s;
  54. }
  55. /* get microseconds */
  56. int us = (int)(t * 1e6);
  57. if (us > 0)
  58. usleep(us);
  59. }
  60. #endif
  61. /* Time in format "hours:minutes:seconds.hundreds" */
  62. string time_human_readable_from_seconds(const double seconds)
  63. {
  64. const int h = (((int)seconds) / (60 * 60));
  65. const int m = (((int)seconds) / 60) % 60;
  66. const int s = (((int)seconds) % 60);
  67. const int r = (((int)(seconds * 100)) % 100);
  68. if (h > 0) {
  69. return string_printf("%.2d:%.2d:%.2d.%.2d", h, m, s, r);
  70. }
  71. else {
  72. return string_printf("%.2d:%.2d.%.2d", m, s, r);
  73. }
  74. }
  75. double time_human_readable_to_seconds(const string &time_string)
  76. {
  77. /* Those are multiplies of a corresponding token surrounded by : in the
  78. * time string, which denotes how to convert value to seconds.
  79. * Effectively: seconds, minutes, hours, days in seconds. */
  80. const int multipliers[] = {1, 60, 60 * 60, 24 * 60 * 60};
  81. const int num_multiplies = sizeof(multipliers) / sizeof(*multipliers);
  82. if (time_string.empty()) {
  83. return 0.0;
  84. }
  85. double result = 0.0;
  86. /* Split fractions of a second from the encoded time. */
  87. vector<string> fraction_tokens;
  88. string_split(fraction_tokens, time_string, ".", false);
  89. const int num_fraction_tokens = fraction_tokens.size();
  90. if (num_fraction_tokens == 0) {
  91. /* Time string is malformed. */
  92. return 0.0;
  93. }
  94. else if (fraction_tokens.size() == 1) {
  95. /* There is no fraction of a second specified, the rest of the code
  96. * handles this normally. */
  97. }
  98. else if (fraction_tokens.size() == 2) {
  99. result = atof(fraction_tokens[1].c_str());
  100. result *= pow(0.1, fraction_tokens[1].length());
  101. }
  102. else {
  103. /* This is not a valid string, the result can not be reliable. */
  104. return 0.0;
  105. }
  106. /* Split hours, minutes and seconds.
  107. * Hours part is optional. */
  108. vector<string> tokens;
  109. string_split(tokens, fraction_tokens[0], ":", false);
  110. const int num_tokens = tokens.size();
  111. if (num_tokens > num_multiplies) {
  112. /* Can not reliably represent the value. */
  113. return 0.0;
  114. }
  115. for (int i = 0; i < num_tokens; ++i) {
  116. result += atoi(tokens[num_tokens - i - 1].c_str()) * multipliers[i];
  117. }
  118. return result;
  119. }
  120. CCL_NAMESPACE_END