Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00043
00044
00045
00046
00047
00048
00049
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
00080
00081
00082
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 }
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
00114
00115
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
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
00159 if ((year<70) || (year> 137))
00160 return (ink_time_t) UNDEFINED_TIME;
00161
00162 mday += days[month];
00163
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 }