00001 // -*- C++ -*- 00002 00003 // $Id: ntp-time.h 71526 2006-03-14 06:14:35Z jtc $ 00004 /* 00005 * Copyright (c) 1995 The Regents of the University of California. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 1. Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 3. All advertising materials mentioning features or use of this software 00017 * must display the following acknowledgement: 00018 * This product includes software developed by the Network Research 00019 * Group at Lawrence Berkeley National Laboratory. 00020 * 4. Neither the name of the University nor of the Laboratory may be used 00021 * to endorse or promote products derived from this software without 00022 * specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00025 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00027 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00030 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00031 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00032 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00033 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00034 * SUCH DAMAGE. 00035 */ 00036 00037 #ifndef TAO_AV_NTP_TIME_H 00038 #define TAO_AV_NTP_TIME_H 00039 #include /**/ "ace/pre.h" 00040 00041 #include "orbsvcs/AV/RTCP.h" 00042 00043 /* 00044 * convert microseconds to fraction of second * 2^32 (i.e., the lsw of 00045 * a 64-bit ntp timestamp). This routine uses the factorization 00046 * 2^32/10^6 = 4096 + 256 - 1825/32 which results in a max conversion 00047 * error of 3 * 10^-7 and an average error of half that. 00048 */ 00049 ACE_INLINE u_int usec2ntp(u_int usec) 00050 { 00051 u_int const t = (usec * 1825) >> 5; 00052 return ((usec << 12) + (usec << 8) - t); 00053 } 00054 00055 /* 00056 * Number of seconds between 1-Jan-1900 and 1-Jan-1970 00057 */ 00058 const ACE_UINT32 GETTIMEOFDAY_TO_NTP_OFFSET = 2208988800U; 00059 00060 /* 00061 * Return a 64-bit ntp timestamp (UTC time relative to Jan 1, 1970). 00062 * gettimeofday conveniently gives us the correct reference -- we just 00063 * need to convert sec+usec to a 64-bit fixed point (with binary point 00064 * at bit 32). 00065 */ 00066 ACE_INLINE TAO_AV_RTCP::ntp64 00067 ntp64time (timeval tv) 00068 { 00069 TAO_AV_RTCP::ntp64 n; 00070 n.upper = (u_int)tv.tv_sec + GETTIMEOFDAY_TO_NTP_OFFSET; 00071 n.lower = usec2ntp((u_int)tv.tv_usec); 00072 return (n); 00073 } 00074 00075 ACE_INLINE ACE_UINT32 00076 ntptime (timeval t) 00077 { 00078 u_int s = (u_int)t.tv_sec + GETTIMEOFDAY_TO_NTP_OFFSET; 00079 return (s << 16 | usec2ntp((u_int)t.tv_usec) >> 16); 00080 } 00081 00082 ACE_INLINE ACE_UINT32 00083 ntptime() 00084 { 00085 // 00086 ACE_Time_Value tim_v = ACE_OS::gettimeofday(); 00087 //struct timeval tv = (timeval) tim_v; 00088 return (ntptime ((timeval) tim_v)); 00089 } 00090 00091 ACE_INLINE timeval unixtime() 00092 { 00093 ACE_Time_Value tv = ACE_OS::gettimeofday(); 00094 return ((timeval) tv); 00095 } 00096 #include /**/ "ace/post.h" 00097 #endif /* TAO_AV_NTP_TIME_H */