Thu May 24 14:31:54 2007

Asterisk developer's documentation


time.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief Time-related functions and macros
00021  */
00022 
00023 #ifndef _ASTERISK_TIME_H
00024 #define _ASTERISK_TIME_H
00025 
00026 #include <sys/time.h>
00027 
00028 #include "asterisk/inline_api.h"
00029 
00030 /* We have to let the compiler learn what types to use for the elements of a
00031    struct timeval since on linux, it's time_t and suseconds_t, but on *BSD,
00032    they are just a long. */
00033 extern struct timeval tv;
00034 typedef typeof(tv.tv_sec) ast_time_t;
00035 typedef typeof(tv.tv_usec) ast_suseconds_t;
00036 
00037 /*!
00038  * \brief Computes the difference (in milliseconds) between two \c struct \c timeval instances.
00039  * \param end the beginning of the time period
00040  * \param start the end of the time period
00041  * \return the difference in milliseconds
00042  */
00043 AST_INLINE_API(
00044 int ast_tvdiff_ms(struct timeval end, struct timeval start),
00045 {
00046    /* the offset by 1,000,000 below is intentional...
00047       it avoids differences in the way that division
00048       is handled for positive and negative numbers, by ensuring
00049       that the divisor is always positive
00050    */
00051    return  ((end.tv_sec - start.tv_sec) * 1000) +
00052       (((1000000 + end.tv_usec - start.tv_usec) / 1000) - 1000);
00053 }
00054 )
00055 
00056 /*!
00057  * \brief Returns true if the argument is 0,0
00058  */
00059 AST_INLINE_API(
00060 int ast_tvzero(const struct timeval t),
00061 {
00062    return (t.tv_sec == 0 && t.tv_usec == 0);
00063 }
00064 )
00065 
00066 /*!
00067  * \brief Compres two \c struct \c timeval instances returning
00068  * -1, 0, 1 if the first arg is smaller, equal or greater to the second.
00069  */
00070 AST_INLINE_API(
00071 int ast_tvcmp(struct timeval _a, struct timeval _b),
00072 {
00073    if (_a.tv_sec < _b.tv_sec)
00074       return -1;
00075    if (_a.tv_sec > _b.tv_sec)
00076       return 1;
00077    /* now seconds are equal */
00078    if (_a.tv_usec < _b.tv_usec)
00079       return -1;
00080    if (_a.tv_usec > _b.tv_usec)
00081       return 1;
00082    return 0;
00083 }
00084 )
00085 
00086 /*!
00087  * \brief Returns true if the two \c struct \c timeval arguments are equal.
00088  */
00089 AST_INLINE_API(
00090 int ast_tveq(struct timeval _a, struct timeval _b),
00091 {
00092    return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
00093 }
00094 )
00095 
00096 /*!
00097  * \brief Returns current timeval. Meant to replace calls to gettimeofday().
00098  */
00099 AST_INLINE_API(
00100 struct timeval ast_tvnow(void),
00101 {
00102    struct timeval t;
00103    gettimeofday(&t, NULL);
00104    return t;
00105 }
00106 )
00107 
00108 /*!
00109  * \brief Returns the sum of two timevals a + b
00110  */
00111 struct timeval ast_tvadd(struct timeval a, struct timeval b);
00112 
00113 /*!
00114  * \brief Returns the difference of two timevals a - b
00115  */
00116 struct timeval ast_tvsub(struct timeval a, struct timeval b);
00117 
00118 /*!
00119  * \brief Returns a timeval from sec, usec
00120  */
00121 AST_INLINE_API(
00122 struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
00123 {
00124    struct timeval t;
00125    t.tv_sec = sec;
00126    t.tv_usec = usec;
00127    return t;
00128 }
00129 )
00130 
00131 /*!
00132  * \brief Returns a timeval corresponding to the duration of n samples at rate r.
00133  * Useful to convert samples to timevals, or even milliseconds to timevals
00134  * in the form ast_samp2tv(milliseconds, 1000)
00135  */
00136 AST_INLINE_API(
00137 struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
00138 {
00139    return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / _rate));
00140 }
00141 )
00142 
00143 #endif /* _ASTERISK_TIME_H */

Generated on Thu May 24 14:31:54 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1