+
+/**
+ * Decrement the date and return the result, taking into account
+ * rounding errors.
+ *
+ * \param date to decrement
+ * \param decrementation in number of samples
+ * \return date value
+ */
+mtime_t date_Decrement( date_t *p_date, uint32_t i_nb_samples )
+{
+ mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000 * p_date->i_divider_den;
+ p_date->date -= i_dividend / p_date->i_divider_num;
+ unsigned i_rem_adjust = i_dividend % p_date->i_divider_num;
+
+ if( p_date->i_remainder < i_rem_adjust )
+ {
+ /* This is Bresenham algorithm. */
+ assert( p_date->i_remainder > -p_date->i_divider_num);
+ p_date->date -= 1;
+ p_date->i_remainder += p_date->i_divider_num;
+ }
+
+ p_date->i_remainder -= i_rem_adjust;
+
+ return p_date->date;
+}
+
+#ifndef HAVE_GETTIMEOFDAY
+
+#ifdef WIN32
+
+/*
+ * Number of micro-seconds between the beginning of the Windows epoch
+ * (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970).
+ *
+ * This assumes all Win32 compilers have 64-bit support.
+ */
+#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__WATCOMC__)
+# define DELTA_EPOCH_IN_USEC 11644473600000000Ui64
+#else
+# define DELTA_EPOCH_IN_USEC 11644473600000000ULL
+#endif
+
+static uint64_t filetime_to_unix_epoch (const FILETIME *ft)
+{
+ uint64_t res = (uint64_t) ft->dwHighDateTime << 32;
+
+ res |= ft->dwLowDateTime;
+ res /= 10; /* from 100 nano-sec periods to usec */
+ res -= DELTA_EPOCH_IN_USEC; /* from Win epoch to Unix epoch */
+ return (res);
+}
+
+static int gettimeofday (struct timeval *tv, void *tz )
+{
+ FILETIME ft;
+ uint64_t tim;
+
+ if (!tv) {
+ return VLC_EGENERIC;
+ }
+ GetSystemTimeAsFileTime (&ft);
+ tim = filetime_to_unix_epoch (&ft);
+ tv->tv_sec = (long) (tim / 1000000L);
+ tv->tv_usec = (long) (tim % 1000000L);
+ return (0);
+}
+
+#endif
+
+#endif
+
+/**
+ * @return NTP 64-bits timestamp in host byte order.
+ */
+uint64_t NTPtime64 (void)
+{
+ struct timespec ts;
+#if defined (CLOCK_REALTIME)
+ clock_gettime (CLOCK_REALTIME, &ts);
+#else
+ {
+ struct timeval tv;
+ gettimeofday (&tv, NULL);
+ ts.tv_sec = tv.tv_sec;
+ ts.tv_nsec = tv.tv_usec * 1000;
+ }
+#endif
+
+ /* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
+ uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
+ t /= 1000000000;
+
+
+ /* There is 70 years (incl. 17 leap ones) offset to the Unix Epoch.
+ * No leap seconds during that period since they were not invented yet.
+ */
+ assert (t < 0x100000000);
+ t |= ((70LL * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
+ return t;
+}
+