• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

ink_time.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /****************************************************************************
00025   ink_time.c
00026 
00027   Timing routines for libts
00028 
00029  ****************************************************************************/
00030 
00031 #include "ink_platform.h"
00032 #include "ink_defs.h"
00033 #include "ink_time.h"
00034 #include "ink_assert.h"
00035 #include "ink_string.h"
00036 
00037 #include <locale.h>
00038 #include <sys/resource.h>
00039 
00040 /*===========================================================================*
00041 
00042                                   Timers
00043 
00044  *===========================================================================*/
00045 
00046 /*---------------------------------------------------------------------------*
00047   uint64_t microseconds(which)
00048 
00049   returns microsecond-resolution clock info
00050  *---------------------------------------------------------------------------*/
00051 
00052 uint64_t
00053 ink_microseconds(int which)
00054 {
00055   struct timeval tp;
00056   struct rusage ru;
00057 
00058   switch (which) {
00059   case MICRO_REAL:
00060     gettimeofday(&tp, NULL);
00061     break;
00062   case MICRO_USER:
00063     getrusage(RUSAGE_SELF, &ru);
00064     tp = ru.ru_utime;
00065     break;
00066   case MICRO_SYS:
00067     getrusage(RUSAGE_SELF, &ru);
00068     tp = ru.ru_stime;
00069     break;
00070   default:
00071     return 0;
00072   }
00073 
00074   return tp.tv_sec * 1000000 + tp.tv_usec;
00075 }
00076 
00077 /*---------------------------------------------------------------------------*
00078 
00079   double ink_time_wall_seconds()
00080 
00081   This routine returns a double precision number of wall clock seconds
00082   elapsed since some fixed time in the past.
00083 
00084  *---------------------------------------------------------------------------*/
00085 double
00086 ink_time_wall_seconds()
00087 {
00088 
00089   struct timeval s_val;
00090 
00091   gettimeofday(&s_val, 0);
00092   return ((double) s_val.tv_sec + 0.000001 * s_val.tv_usec);
00093 }                               /* End ink_time_wall_seconds */
00094 
00095 
00096 struct dtconv
00097 {
00098   char *abbrev_month_names[12];
00099   char *month_names[12];
00100   char *abbrev_weekday_names[7];
00101   char *weekday_names[7];
00102   char *time_format;
00103   char *sdate_format;
00104   char *dtime_format;
00105   char *am_string;
00106   char *pm_string;
00107   char *ldate_format;
00108 };
00109 
00110 
00111 
00112 /*
00113  * The man page for cftime lies. It claims that it is thread safe.
00114  * Instead, it silently trashes the heap (by freeing things more than
00115  * once) when used in a mulithreaded program. Gack!
00116  */
00117 int
00118 cftime_replacement(char *s, int maxsize, const char *format, const time_t * clock)
00119 {
00120   struct tm tm;
00121 
00122   ink_assert(ink_localtime_r(clock, &tm) != NULL);
00123 
00124   return strftime(s, maxsize, format, &tm);
00125 }
00126 
00127 #undef cftime
00128 /* Throw an error if they ever call plain-old cftime. */
00129 int
00130 cftime(char *s, char *format, const time_t * clock)
00131 {
00132   (void) s;
00133   (void) format;
00134   (void) clock;
00135   printf("ERROR cftime is not thread safe -- call cftime_replacement\n");
00136   ink_assert(!"cftime");
00137   return 0;
00138 }
00139 
00140 #define DAYS_OFFSET  25508
00141 
00142 ink_time_t
00143 convert_tm(const struct tm * tp)
00144 {
00145   static const int days[12] = {
00146     305, 336, -1, 30, 60, 91, 121, 152, 183, 213, 244, 274
00147   };
00148 
00149   ink_time_t t;
00150   int year;
00151   int month;
00152   int mday;
00153 
00154   year = tp->tm_year;
00155   month = tp->tm_mon;
00156   mday = tp->tm_mday;
00157 
00158   /* what should we do? */
00159   if ((year<70) || (year> 137))
00160     return (ink_time_t) UNDEFINED_TIME;
00161 
00162   mday += days[month];
00163   /* month base == march */
00164   if (month < 2)
00165     year -= 1;
00166   mday += (year * 365) + (year / 4) - (year / 100) + (year / 100 + 3) / 4;
00167   mday -= DAYS_OFFSET;
00168 
00169   t = ((mday * 24 + tp->tm_hour) * 60 + tp->tm_min) * 60 + tp->tm_sec;
00170 
00171   return t;
00172 }
00173 
00174 char *
00175 ink_ctime_r(const ink_time_t * clock, char *buf)
00176 {
00177   return ctime_r(clock, buf);
00178 }
00179 
00180 struct tm *
00181 ink_localtime_r(const ink_time_t * clock, struct tm *res)
00182 {
00183   return localtime_r(clock, res);
00184 }

Generated by  doxygen 1.7.1