From: Rémi Denis-Courmont Date: Fri, 9 Feb 2007 18:24:06 +0000 (+0000) Subject: NTPtime64() returns an NTP timestamp X-Git-Tag: 0.9.0-test0~8683 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=55cfe8ef3e1fc528dd698e15b138714d502d34d9;p=vlc NTPtime64() returns an NTP timestamp --- diff --git a/include/vlc_mtime.h b/include/vlc_mtime.h index 30460698c6..add6941638 100644 --- a/include/vlc_mtime.h +++ b/include/vlc_mtime.h @@ -86,3 +86,4 @@ VLC_EXPORT( void, date_Set, ( date_t *, mtime_t ) ); VLC_EXPORT( mtime_t, date_Get, ( const date_t * ) ); VLC_EXPORT( void, date_Move, ( date_t *, mtime_t ) ); VLC_EXPORT( mtime_t, date_Increment, ( date_t *, uint32_t ) ); +VLC_EXPORT( uint64_t, NTPtime64, ( void ) ); diff --git a/src/misc/mtime.c b/src/misc/mtime.c index 8579dfccda..1c543325e2 100644 --- a/src/misc/mtime.c +++ b/src/misc/mtime.c @@ -3,6 +3,7 @@ * 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 @@ -32,6 +33,7 @@ #include /* sprintf() */ #include /* clock_gettime(), clock_nanosleep() */ #include /* lldiv() */ +#include #if defined( PTH_INIT_IN_PTH_H ) /* GNU Pth */ @@ -386,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; +} +