su_time.h 3.52 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#ifndef SU_TIME_H /** Defined when su_time.h has been included. */
#define SU_TIME_H "$Id: su_time.h,v 1.1.1.1 2005/07/20 20:35:59 kaiv Exp $"
/**@ingroup su_time
 * @file su_time.h 
 * @brief Time types and functions.
 * 
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @date Created: Thu Mar 18 19:40:51 1999 pessi
 *
 * $Date: 2005/07/20 20:35:59 $
 */

#ifndef SU_TYPES_H
#include "su_types.h"
#endif

/** Time in seconds and microsecondcs. 
 *
 * The structure su_time_t contains time in seconds and microseconds since
 * epoch (January 1, 1900).
 */
struct su_time_s { 
  unsigned long tv_sec;		/**< Seconds */
  unsigned long tv_usec;	/**< Microseconds  */
};
/** Time in seconds and microsecondcs. */
typedef struct su_time_s su_time_t;

/** Time difference in microseconds. 
 * 
 * The type su_duration_t is used to present small time differences (24
 * days), usually calculated between two su_time_t timestamps.  Note that
 * the su_duration_t is signed.
 */
typedef long su_duration_t;

enum { 
  /** Maximum duration in milliseconds. */
  SU_DURATION_MAX = 0x7fffffffL 
};
#define SU_DURATION_MAX SU_DURATION_MAX

/** NTP timestamp. 
 * 
 * NTP timestamp is defined as microseconds since epoch (1-Jan-1900) 
 * with 64-bit resolution.
 */
typedef uint64_t su_ntp_t;

#define SU_TIME_CMP(t1, t2) \
 (long)((t1.tv_sec - t2.tv_sec != 0) ? \
   (t1.tv_sec - t2.tv_sec) : \
     (t1.tv_usec - t2.tv_usec))

su_time_t su_now(void);
void su_time(su_time_t *tv);
long su_time_cmp(su_time_t const t1, su_time_t const t2);
double su_time_diff(su_time_t const t1, su_time_t const t2);
su_duration_t su_duration(su_time_t const t1, su_time_t const t2);

su_time_t su_time_add(su_time_t t, su_duration_t dur);
su_time_t su_time_dadd(su_time_t t, double dur);

int su_time_print(char *s, int n, su_time_t const *tv);

#define SU_SEC_TO_DURATION(sec) ((su_duration_t)(1000 * (sec)))

su_ntp_t su_ntp_now(void);
uint32_t su_ntp_sec(void);
uint32_t su_ntp_hi(su_ntp_t);
uint32_t su_ntp_lo(su_ntp_t);
uint32_t su_ntp_mw(su_ntp_t ntp);

#if !SU_HAVE_INLINE
uint32_t su_ntp_fraq(su_time_t t);
uint32_t su_time_ms(su_time_t t);
#else
static SU_INLINE
/** Middle 32 bit of NTP timestamp. */
uint32_t su_ntp_fraq(su_time_t t)
{
  /*
   * Multiply usec by 0.065536 (ie. 2**16 / 1E6)
   * 
   * Utilize fact that 0.065536 == 1024 / 15625
   */
  return (t.tv_sec << 16) + (1024 * t.tv_usec + 7812) / 15625;
}

static SU_INLINE
/** Time as milliseconds. */
uint32_t su_time_ms(su_time_t t)
{
  return t.tv_sec * 1000 + (t.tv_usec + 500) / 1000;
}
#endif

su_ntp_t su_ntp_hilo(uint32_t hi, uint32_t lo);

uint64_t su_counter(void);

uint64_t su_nanocounter(void);

uint32_t su_random();

#endif /* !defined(SU_TIME_H) */