Browse Source

faux.time: Use CLOCK_REALTIME for faux_timespec_now(). Add faux_timespec_now_monotonic()

Serj Kalichev 3 years ago
parent
commit
f84cb0d213
2 changed files with 22 additions and 3 deletions
  1. 1 2
      faux/time.h
  2. 21 1
      faux/time/time.c

+ 1 - 2
faux/time.h

@@ -11,8 +11,6 @@
 
 #include <faux/faux.h>
 
-#define FAUX_CLOCK_SOURCE CLOCK_MONOTONIC
-
 C_DECL_BEGIN
 
 // Operations for struct timespec
@@ -22,6 +20,7 @@ int faux_timespec_diff(struct timespec *res,
 int faux_timespec_sum(struct timespec *res,
 	const struct timespec *val1, const struct timespec *val2);
 int faux_timespec_now(struct timespec *now);
+int faux_timespec_now_monotonic(struct timespec *now);
 bool_t faux_timespec_before_now(const struct timespec *ts);
 
 // Convertions of struct timespec

+ 21 - 1
faux/time/time.c

@@ -149,7 +149,27 @@ int faux_timespec_now(struct timespec *now)
 	if (!now)
 		return -1;
 
-	clock_gettime(FAUX_CLOCK_SOURCE, now);
+	clock_gettime(CLOCK_REALTIME, now);
+
+	return 0;
+}
+
+
+/** @brief Returns current time using CLOCK_MONOTONIC (now).
+ *
+ * CLOCK_MONOTONIC is not ajustable by NTP and etc. But it represents
+ * time from system up but not since Epoch.
+ *
+ * @param [out] now The struct timespec to save current time.
+ * @return 0 - success, < 0 on error.
+ */
+int faux_timespec_now_monotonic(struct timespec *now)
+{
+	assert(now);
+	if (!now)
+		return -1;
+
+	clock_gettime(CLOCK_MONOTONIC, now);
 
 	return 0;
 }