]> git.sesse.net Git - vlc/blobdiff - src/misc/mtime.c
NTPtime64() returns an NTP timestamp
[vlc] / src / misc / mtime.c
index b67f3f7d13a358f627026dc62596258cbd2a96f8..1c543325e2456413332d0eb45c975ae4873b1fff 100644 (file)
@@ -1,8 +1,9 @@
 /*****************************************************************************
  * mtime.c: high resolution time management functions
- * Functions are prototyped in mtime.h.
+ * Functions are prototyped in vlc_mtime.h.
  *****************************************************************************
  * Copyright (C) 1998-2004 the VideoLAN team
+ * Copyright © 2006-2007 Rémi Denis-Courmont
  * $Id$
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
@@ -32,6 +33,7 @@
 #include <stdio.h>                                              /* sprintf() */
 #include <time.h>                      /* clock_gettime(), clock_nanosleep() */
 #include <stdlib.h>                                               /* lldiv() */
+#include <assert.h>
 
 
 #if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
@@ -134,7 +136,6 @@ mtime_t mdate( void )
 #elif defined( WIN32 ) || defined( UNDER_CE )
     /* We don't need the real date, just the value of a high precision timer */
     static mtime_t freq = I64C(-1);
-    mtime_t usec_time;
 
     if( freq == I64C(-1) )
     {
@@ -159,15 +160,21 @@ mtime_t mdate( void )
         LARGE_INTEGER buf;
 
         freq = ( QueryPerformanceFrequency( &buf ) &&
-                 (freq == I64C(1193182) || freq == I64C(3579545) ) )
+                 (buf.QuadPart == I64C(1193182) || buf.QuadPart == I64C(3579545) ) )
                ? buf.QuadPart : 0;
     }
 
     if( freq != 0 )
     {
-        /* Microsecond resolution */
-        QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
-        return ( usec_time * 1000000 ) / freq;
+        LARGE_INTEGER counter;
+        QueryPerformanceCounter (&counter);
+
+        /* Convert to from (1/freq) to microsecond resolution */
+        /* We need to split the division to avoid 63-bits overflow */
+        lldiv_t d = lldiv (counter.QuadPart, freq);
+
+        return (d.quot * 1000000)
+             + ((d.rem * 1000000) / freq);
     }
     else
     {
@@ -179,6 +186,7 @@ mtime_t mdate( void )
         static CRITICAL_SECTION date_lock;
         static mtime_t i_previous_time = I64C(-1);
         static int i_wrap_counts = -1;
+        mtime_t usec_time;
 
         if( i_wrap_counts == -1 )
         {
@@ -195,7 +203,7 @@ mtime_t mdate( void )
         {
             /* Counter wrapped */
             i_wrap_counts++;
-            usec_time += I64C(0x100000000000);
+            usec_time += I64C(0x100000000) * 1000;
         }
         i_previous_time = usec_time;
         LeaveCriticalSection( &date_lock );
@@ -231,7 +239,7 @@ void mwait( mtime_t date )
         clock_nanosleep( CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL );
 #else
 
-    mtime_t delay = mdate() - date;
+    mtime_t delay = date - mdate();
     if( delay > 0 )
         msleep( delay );
 
@@ -380,3 +388,34 @@ mtime_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
 
     return p_date->date;
 }
+
+/**
+ * @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;
+}
+