-/*****************************************************************************
- * mdate: return high precision date
- *****************************************************************************
- * Uses the gettimeofday() function when possible (1 MHz resolution) or the
- * ftime() function (1 kHz resolution).
- *****************************************************************************/
+/**
+ * Convert seconds to a time in the format h:mm:ss.
+ *
+ * This function is provided for any interface function which need to print a
+ * time string in the format h:mm:ss
+ * date.
+ * \param secs the date to be converted
+ * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
+ * \return psz_buffer is returned so this can be used as printf parameter.
+ */
+char *secstotimestr( char *psz_buffer, int32_t i_seconds )
+{
+ if( unlikely(i_seconds < 0) )
+ {
+ secstotimestr( psz_buffer + 1, -i_seconds );
+ *psz_buffer = '-';
+ return psz_buffer;
+ }
+
+ div_t d;
+
+ d = div( i_seconds, 60 );
+ i_seconds = d.rem;
+ d = div( d.quot, 60 );
+
+ if( d.quot )
+ snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%u:%02u:%02u",
+ d.quot, d.rem, i_seconds );
+ else
+ snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%02u:%02u",
+ d.rem, i_seconds );
+ return psz_buffer;
+}
+
+#if defined (HAVE_CLOCK_NANOSLEEP)
+static unsigned prec = 0;
+
+static void mprec_once( void )
+{
+ struct timespec ts;
+ if( clock_getres( CLOCK_MONOTONIC, &ts ))
+ clock_getres( CLOCK_REALTIME, &ts );
+
+ prec = ts.tv_nsec / 1000;
+}
+#endif
+
+/**
+ * Return a value that is no bigger than the clock precision
+ * (possibly zero).
+ */
+static inline unsigned mprec( void )
+{
+#if defined (HAVE_CLOCK_NANOSLEEP)
+ static pthread_once_t once = PTHREAD_ONCE_INIT;
+ pthread_once( &once, mprec_once );
+ return prec;
+#else
+ return 0;
+#endif
+}
+
+#ifdef USE_APPLE_MACH
+static mach_timebase_info_data_t mtime_timebase_info;
+static pthread_once_t mtime_timebase_info_once = PTHREAD_ONCE_INIT;
+static void mtime_init_timebase(void)
+{
+ mach_timebase_info(&mtime_timebase_info);
+}
+#endif
+
+/**
+ * Return high precision date
+ *
+ * Use a 1 MHz clock when possible, or 1 kHz
+ *
+ * Beware ! It doesn't reflect the actual date (since epoch), but can be the machine's uptime or anything (when monotonic clock is used)
+ */