+#endif
+
+ earlier += delay;
+ if( cached_time < earlier )
+ cached_time = earlier;
+}
+
+/*
+ * Date management (internal and external)
+ */
+
+/**
+ * Initialize a date_t.
+ *
+ * \param date to initialize
+ * \param divider (sample rate) numerator
+ * \param divider (sample rate) denominator
+ */
+
+void date_Init( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
+{
+ p_date->date = 0;
+ p_date->i_divider_num = i_divider_n;
+ p_date->i_divider_den = i_divider_d;
+ p_date->i_remainder = 0;
+}
+
+/**
+ * Change a date_t.
+ *
+ * \param date to change
+ * \param divider (sample rate) numerator
+ * \param divider (sample rate) denominator
+ */
+
+void date_Change( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
+{
+ p_date->i_divider_num = i_divider_n;
+ p_date->i_divider_den = i_divider_d;
+}
+
+/**
+ * Set the date value of a date_t.
+ *
+ * \param date to set
+ * \param date value
+ */
+void date_Set( date_t *p_date, mtime_t i_new_date )
+{
+ p_date->date = i_new_date;
+ p_date->i_remainder = 0;
+}
+
+/**
+ * Get the date of a date_t
+ *
+ * \param date to get
+ * \return date value
+ */
+mtime_t date_Get( const date_t *p_date )
+{
+ return p_date->date;
+}
+
+/**
+ * Move forwards or backwards the date of a date_t.
+ *
+ * \param date to move
+ * \param difference value
+ */
+void date_Move( date_t *p_date, mtime_t i_difference )
+{
+ p_date->date += i_difference;
+}
+
+/**
+ * Increment the date and return the result, taking into account
+ * rounding errors.
+ *
+ * \param date to increment
+ * \param incrementation in number of samples
+ * \return date value
+ */
+mtime_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
+{
+ mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
+ p_date->date += i_dividend / p_date->i_divider_num * p_date->i_divider_den;
+ p_date->i_remainder += (int)(i_dividend % p_date->i_divider_num);
+
+ if( p_date->i_remainder >= p_date->i_divider_num )
+ {
+ /* This is Bresenham algorithm. */
+ p_date->date += p_date->i_divider_den;
+ p_date->i_remainder -= p_date->i_divider_num;
+ }
+
+ 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);
+}