00001 /* 00002 * This file is part of the Sofia-SIP package 00003 * 00004 * Copyright (C) 2005 Nokia Corporation. 00005 * 00006 * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden> 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public License 00010 * as published by the Free Software Foundation; either version 2.1 of 00011 * the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, but 00014 * WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 00021 * 02110-1301 USA 00022 * 00023 */ 00024 00025 #ifndef SU_TIME_H 00026 00027 #define SU_TIME_H 00028 00037 #ifndef SU_TYPES_H 00038 #include "sofia-sip/su_types.h" 00039 #endif 00040 00041 SOFIA_BEGIN_DECLS 00042 00048 struct su_time_s { 00049 unsigned long tv_sec; 00050 unsigned long tv_usec; 00051 }; 00053 typedef struct su_time_s su_time_t; 00054 00061 typedef long su_duration_t; 00062 00063 enum { 00065 SU_DURATION_MAX = 0x7fffffffL 00066 }; 00067 #define SU_DURATION_MAX SU_DURATION_MAX 00068 00074 typedef uint64_t su_ntp_t; 00075 00077 #define SU_NTP_C(x) SU_U64_C(x) 00078 00079 #define SU_TIME_CMP(t1, t2) \ 00080 (long)((t1.tv_sec - t2.tv_sec != 0) ? \ 00081 (t1.tv_sec - t2.tv_sec) : \ 00082 (t1.tv_usec - t2.tv_usec)) 00083 00084 SOFIAPUBFUN su_time_t su_now(void); 00085 SOFIAPUBFUN void su_time(su_time_t *tv); 00086 SOFIAPUBFUN long su_time_cmp(su_time_t const t1, su_time_t const t2); 00087 SOFIAPUBFUN double su_time_diff(su_time_t const t1, su_time_t const t2); 00088 SOFIAPUBFUN su_duration_t su_duration(su_time_t const t1, su_time_t const t2); 00089 00090 SOFIAPUBFUN su_time_t su_time_add(su_time_t t, su_duration_t dur); 00091 SOFIAPUBFUN su_time_t su_time_dadd(su_time_t t, double dur); 00092 00093 SOFIAPUBFUN int su_time_print(char *s, int n, su_time_t const *tv); 00094 00095 #define SU_SEC_TO_DURATION(sec) ((su_duration_t)(1000 * (sec))) 00096 00097 SOFIAPUBFUN su_ntp_t su_ntp_now(void); 00098 SOFIAPUBFUN uint32_t su_ntp_sec(void); 00099 SOFIAPUBFUN uint32_t su_ntp_hi(su_ntp_t); 00100 SOFIAPUBFUN uint32_t su_ntp_lo(su_ntp_t); 00101 SOFIAPUBFUN uint32_t su_ntp_mw(su_ntp_t ntp); 00102 00103 #if !SU_HAVE_INLINE 00104 SOFIAPUBFUN uint32_t su_ntp_fraq(su_time_t t); 00105 SOFIAPUBFUN uint32_t su_time_ms(su_time_t t); 00106 #else 00107 static SU_INLINE 00109 uint32_t su_ntp_fraq(su_time_t t) 00110 { 00111 /* 00112 * Multiply usec by 0.065536 (ie. 2**16 / 1E6) 00113 * 00114 * Utilize fact that 0.065536 == 1024 / 15625 00115 */ 00116 return (t.tv_sec << 16) + (1024 * t.tv_usec + 7812) / 15625; 00117 } 00118 00119 static SU_INLINE 00121 uint32_t su_time_ms(su_time_t t) 00122 { 00123 return t.tv_sec * 1000 + (t.tv_usec + 500) / 1000; 00124 } 00125 #endif 00126 00127 SOFIAPUBFUN su_ntp_t su_ntp_hilo(uint32_t hi, uint32_t lo); 00128 00129 SOFIAPUBFUN uint64_t su_counter(void); 00130 00131 SOFIAPUBFUN uint64_t su_nanocounter(void); 00132 00133 SOFIAPUBFUN uint32_t su_random(); 00134 00135 SOFIA_END_DECLS 00136 00137 #endif /* !defined(SU_TIME_H) */