[Bf-blender-cvs] [c7e222c5ee5] experimental-build: Add initial `BLI_math_time` with a 'seconds explode' function.

Bastien Montagne noreply at git.blender.org
Fri Jun 11 13:01:57 CEST 2021


Commit: c7e222c5ee5df7dd518d9c1b5d60c2bd70db2d64
Author: Bastien Montagne
Date:   Fri Jun 11 13:00:36 2021 +0200
Branches: experimental-build
https://developer.blender.org/rBc7e222c5ee5df7dd518d9c1b5d60c2bd70db2d64

Add initial `BLI_math_time` with a 'seconds explode' function.

Allows to explode a given amount of seconds into a random set of
days/hours/minutes/seconds/milliseconds.

Also add matching test.

===================================================================

M	source/blender/blenlib/BLI_math.h
A	source/blender/blenlib/BLI_math_time.h
M	source/blender/blenlib/CMakeLists.txt
A	source/blender/blenlib/intern/math_time_inline.c
A	source/blender/blenlib/tests/BLI_math_time_test.cc

===================================================================

diff --git a/source/blender/blenlib/BLI_math.h b/source/blender/blenlib/BLI_math.h
index f6075367ac5..3b61c0feb51 100644
--- a/source/blender/blenlib/BLI_math.h
+++ b/source/blender/blenlib/BLI_math.h
@@ -70,4 +70,5 @@
 #include "BLI_math_rotation.h"
 #include "BLI_math_solvers.h"
 #include "BLI_math_statistics.h"
+#include "BLI_math_time.h"
 #include "BLI_math_vector.h"
diff --git a/source/blender/blenlib/BLI_math_time.h b/source/blender/blenlib/BLI_math_time.h
new file mode 100644
index 00000000000..40ea6f716df
--- /dev/null
+++ b/source/blender/blenlib/BLI_math_time.h
@@ -0,0 +1,64 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2021 by Blender Foundation.
+ * All rights reserved.
+ */
+
+#pragma once
+
+/** \file
+ * \ingroup bli
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef BLI_MATH_GCC_WARN_PRAGMA
+#  pragma GCC diagnostic push
+#  pragma GCC diagnostic ignored "-Wredundant-decls"
+#endif
+
+/************************ Time constants definitions***************************/
+#define SECONDS_IN_MILLISECONDS 0.001
+#define SECONDS_IN_MINUTE 60.0
+#define MINUTES_IN_HOUR 60.0
+#define HOURS_IN_DAY 24.0
+
+#define MINUTES_IN_DAY (MINUTES_IN_HOUR * HOURS_IN_DAY)
+#define SECONDS_IN_DAY (MINUTES_IN_DAY * SECONDS_IN_MINUTE)
+#define SECONDS_IN_HOUR (MINUTES_IN_HOUR * SECONDS_IN_MINUTE)
+
+MINLINE void BLI_math_time_seconds_explode(double seconds,
+                                           double *r_days,
+                                           double *r_hours,
+                                           double *r_minutes,
+                                           double *r_seconds,
+                                           double *r_milliseconds);
+
+/**************************** Inline Definitions ******************************/
+
+#if BLI_MATH_DO_INLINE
+#  include "intern/math_time_inline.c"
+#endif
+
+#ifdef BLI_MATH_GCC_WARN_PRAGMA
+#  pragma GCC diagnostic pop
+#endif
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index e04f3c1b19d..2d45eee13d8 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -103,6 +103,7 @@ set(SRC
   intern/math_rotation.c
   intern/math_solvers.c
   intern/math_statistics.c
+  intern/math_time_inline.c
   intern/math_vec.cc
   intern/math_vector.c
   intern/math_vector_inline.c
@@ -241,6 +242,7 @@ set(SRC
   BLI_math_rotation.h
   BLI_math_solvers.h
   BLI_math_statistics.h
+  BLI_math_time.h
   BLI_math_vector.h
   BLI_memarena.h
   BLI_memblock.h
@@ -419,6 +421,7 @@ if(WITH_GTESTS)
     tests/BLI_math_matrix_test.cc
     tests/BLI_math_rotation_test.cc
     tests/BLI_math_solvers_test.cc
+    tests/BLI_math_time_test.cc
     tests/BLI_math_vector_test.cc
     tests/BLI_memiter_test.cc
     tests/BLI_memory_utils_test.cc
diff --git a/source/blender/blenlib/intern/math_time_inline.c b/source/blender/blenlib/intern/math_time_inline.c
new file mode 100644
index 00000000000..f0b71c5c40e
--- /dev/null
+++ b/source/blender/blenlib/intern/math_time_inline.c
@@ -0,0 +1,74 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2021 by Blender Foundation.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup bli
+ */
+
+#ifndef __MATH_TIME_INLINE_C__
+#define __MATH_TIME_INLINE_C__
+
+#include "BLI_math.h"
+
+/** Explode given time value expressed in seconds, into a set of days, hours, minutes, seconds
+ * and/or milliseconds (depending on which return parameters are not NULL).
+ *
+ * \note: The smallest given return parameter will get the potential fractional remaining time
+ * value.
+ */
+MINLINE void BLI_math_time_seconds_explode(double seconds,
+                                           double *r_days,
+                                           double *r_hours,
+                                           double *r_minutes,
+                                           double *r_seconds,
+                                           double *r_milliseconds)
+{
+  BLI_assert(r_days != NULL || r_hours != NULL || r_minutes != NULL || r_seconds != NULL ||
+             r_milliseconds != NULL);
+
+  if (r_days != NULL) {
+    seconds = modf(seconds / SECONDS_IN_DAY, r_days) * SECONDS_IN_DAY;
+  }
+  if (r_hours != NULL) {
+    seconds = modf(seconds / SECONDS_IN_HOUR, r_hours) * SECONDS_IN_HOUR;
+  }
+  if (r_minutes != NULL) {
+    seconds = modf(seconds / SECONDS_IN_MINUTE, r_minutes) * SECONDS_IN_MINUTE;
+  }
+  if (r_seconds != NULL) {
+    seconds = modf(seconds, r_seconds);
+  }
+  if (r_milliseconds != NULL) {
+    *r_milliseconds = seconds / SECONDS_IN_MILLISECONDS;
+  }
+  else if (r_seconds != NULL) {
+    *r_seconds += seconds;
+  }
+  else if (r_minutes != NULL) {
+    *r_minutes += seconds / SECONDS_IN_MINUTE;
+  }
+  else if (r_hours != NULL) {
+    *r_hours += seconds / SECONDS_IN_HOUR;
+  }
+  else if (r_days != NULL) {
+    *r_days = seconds / SECONDS_IN_DAY;
+  }
+}
+
+#endif
diff --git a/source/blender/blenlib/tests/BLI_math_time_test.cc b/source/blender/blenlib/tests/BLI_math_time_test.cc
new file mode 100644
index 00000000000..14a1250c307
--- /dev/null
+++ b/source/blender/blenlib/tests/BLI_math_time_test.cc
@@ -0,0 +1,35 @@
+/* Apache License, Version 2.0 */
+
+#include "testing/testing.h"
+
+#include "BLI_math.h"
+
+TEST(math_time, SecondsExplode)
+{
+  const double seconds = 2.0 * SECONDS_IN_DAY + 13.0 * SECONDS_IN_HOUR + 33.0 * SECONDS_IN_MINUTE +
+                         9.0 + 369.0 * SECONDS_IN_MILLISECONDS;
+  const double epsilon = 1e-8;
+
+  double r_days, r_hours, r_minutes, r_seconds, r_milliseconds;
+
+  BLI_math_time_seconds_explode(
+      seconds, &r_days, &r_hours, &r_minutes, &r_seconds, &r_milliseconds);
+  EXPECT_NEAR(2.0, r_days, epsilon);
+  EXPECT_NEAR(13.0, r_hours, epsilon);
+  EXPECT_NEAR(33.0, r_minutes, epsilon);
+  EXPECT_NEAR(9.0, r_seconds, epsilon);
+  EXPECT_NEAR(369.0, r_milliseconds, epsilon);
+
+  BLI_math_time_seconds_explode(seconds, NULL, &r_hours, &r_minutes, &r_seconds, NULL);
+  EXPECT_NEAR(61.0, r_hours, epsilon);
+  EXPECT_NEAR(33.0, r_minutes, epsilon);
+  EXPECT_NEAR(9.369, r_seconds, epsilon);
+
+  BLI_math_time_seconds_explode(seconds, NULL, NULL, NULL, &r_seconds, NULL);
+  EXPECT_NEAR(seconds, r_seconds, epsilon);
+
+  BLI_math_time_seconds_explode(seconds, &r_days, NULL, &r_minutes, NULL, &r_milliseconds);
+  EXPECT_NEAR(2.0, r_days, epsilon);
+  EXPECT_NEAR(813.0, r_minutes, epsilon);
+  EXPECT_NEAR(9369.0, r_milliseconds, epsilon);
+}



More information about the Bf-blender-cvs mailing list