su_time0.c 4.96 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
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
 * 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
 *
 */

/**@ingroup su_time
 * @CFILE su_time0.c
 * @brief su_time() implementation
 *
 * The file su_time0.c contains implementation of OS-independent wallclock
 * time with microsecond resolution.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Jari Selin <Jari.Selin@nokia.com>
 * 
 * @date Created: Fri May 10 18:13:19 2002 ppessi
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

43 44
#include "sofia-sip/su_types.h"
#include "sofia-sip/su_time.h"
Pekka Pessi's avatar
Pekka Pessi committed
45 46
#include "su_module_debug.h"

47 48
#include <time.h>

Pekka Pessi's avatar
Pekka Pessi committed
49 50 51 52
#if HAVE_SYS_TIME_H
#include <sys/time.h> /* Get struct timeval */
#endif

Martti Mela's avatar
Martti Mela committed
53 54 55 56 57 58 59
#if defined(__MINGW32__)
#define HAVE_FILETIME 1
#include <windows.h>
#endif

#if HAVE_FILETIME
#define HAVE_FILETIME 1
Pekka Pessi's avatar
Pekka Pessi committed
60 61 62 63 64
#include <windows.h>
#endif

/** Seconds from 1.1.1900 to 1.1.1970 */
#define NTP_EPOCH 2208988800UL 
65 66 67 68 69 70
#define E9 (1000000000U)
#define E7 (10000000U)

/* Hooks for testing timing and timers */
void (*_su_time)(su_time_t *tv);
uint64_t (*_su_nanotime)(uint64_t *);
Pekka Pessi's avatar
Pekka Pessi committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

/** Get current time.
 *
 * The function @c su_time() fills its argument with the current NTP
 * timestamp expressed as a su_time_t structure.
 *
 * @param tv pointer to the timeval object
 */
void su_time(su_time_t *tv)
{
#if HAVE_GETTIMEOFDAY
  if (tv) {
    gettimeofday((struct timeval *)tv, NULL);
    tv->tv_sec += NTP_EPOCH;
  }
#elif HAVE_FILETIME
  union {
    FILETIME       ft[1];
    ULARGE_INTEGER ull[1];
  } date;

  GetSystemTimeAsFileTime(date.ft);

94 95
  tv->tv_usec = (unsigned long) ((date.ull->QuadPart % E7) / 10);
  tv->tv_sec = (unsigned long) ((date.ull->QuadPart / E7) - 
Pekka Pessi's avatar
Pekka Pessi committed
96 97 98 99
    /* 1900-Jan-01 - 1601-Jan-01: 299 years, 72 leap years */
    (299 * 365 + 72) * 24 * 60 * (uint64_t)60);
#else
#error no su_time() implementation
100 101 102
#endif

  if (_su_time)
103
    _su_time(tv);
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}

/** Get current time as nanoseconds since epoch.
 *
 * Return the current NTP timestamp expressed as nanoseconds since epoch
 * (January 1st 1900).
 *
 * @param return_time optional pointer to current time to return
 *
 * @return Nanoseconds since epoch
 */
su_nanotime_t su_nanotime(su_nanotime_t *return_time)
{
  su_nanotime_t now;

  if (!return_time)
    return_time = &now;

#if HAVE_CLOCK_GETTIME
  {
    struct timespec tv;
  
    if (clock_gettime(CLOCK_REALTIME, &tv) == 0) {
      now = ((su_nanotime_t)tv.tv_sec + NTP_EPOCH) * E9 + tv.tv_nsec;
      *return_time = now;
      if (_su_nanotime)
	return _su_nanotime(return_time);
      return now;
    }
  }
#endif

#if HAVE_FILETIME
  {
    union {
      FILETIME       ft[1];
      ULARGE_INTEGER ull[1];
    } date;
    
    GetSystemTimeAsFileTime(date.ft);

    now = 100 * 
      (date.ull->QuadPart - 
       /* 1900-Jan-01 - 1601-Jan-01: 299 years, 72 leap years */
       ((su_nanotime_t)(299 * 365 + 72) * 24 * 60 * 60) * E7);
  }
#elif HAVE_GETTIMEOFDAY
  {
    struct timeval tv;

    gettimeofday(&tv, NULL);

    now = ((su_nanotime_t)tv.tv_sec + NTP_EPOCH) * E9 + tv.tv_usec * 1000;
  }
#else
  now = ((su_nanotime_t)time() + NTP_EPOCH) * E9;
#endif

  *return_time = now;

  if (_su_nanotime)
    return _su_nanotime(return_time);

  return now;
}

/** Get current time as nanoseconds.
 *
 * Return the current time expressed as nanoseconds. The time returned is
 * monotonic and never goes back - if the underlying system supports such a
 * clock.
 *
 * @param return_time optional pointer to return the current time 
 *
 * @return Current time as nanoseconds 
 */
su_nanotime_t su_monotime(su_nanotime_t *return_time)
{
182
#if HAVE_CLOCK_GETTIME && CLOCK_MONOTONIC
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  {
    struct timespec tv;
  
    if (clock_gettime(CLOCK_MONOTONIC, &tv) == 0) {
      su_nanotime_t now = (su_nanotime_t)tv.tv_sec * E9 + tv.tv_nsec;
      if (return_time)
	*return_time = now;
      return now;
    }
  }
#endif

#if HAVE_NANOUPTIME
  {
    struct timespec tv;
  
    nanouptime(&tv);
    now = (su_nanotime_t)tv.tv_sec * E9 + tv.tv_nsec;
    if (return_time)
      *return_time = now;
    return now;
  }
#elif HAVE_MICROUPTIME
  {
    struct timeval tv;

    microuptime(&tv);
    now = (su_nanotime_t)tv.tv_sec * E9 + tv.tv_usec * 1000;
    if (return_time)
      *return_time = now;
    return now;
  }
#else
  return su_nanotime(return_time);
Pekka Pessi's avatar
Pekka Pessi committed
217 218
#endif
}