/var/lib/pcp/testsuite/src/rtimetest.c is in pcp-testsuite 4.0.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 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 182 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | /*
* Copyright (c) 2013-2015 Red Hat.
*
* 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.
*/
#include <pcp/pmapi.h>
#include "libpcp.h"
void
set_tm(struct timeval *ntv, struct tm *ntm, struct tm *btm, int mon,
int mday, int hour, int min)
{
memcpy(ntm, btm, sizeof(struct tm));
if (mon > 0)
ntm->tm_mon = mon;
if (mday > 0)
ntm->tm_mday = mday;
if (hour > 0)
ntm->tm_hour = hour;
if (min > 0)
ntm->tm_min = min;
if (ntv != NULL) {
ntv->tv_sec = __pmMktime(ntm);
ntv->tv_usec = 0;
}
}
void
dump_dt(char *str, struct tm *atm)
{
int pfx;
printf("\"%s\"%n", str, &pfx);
printf("%*s", 33 - pfx, " ");
printf("%d-%.2d-%.2d %.2d:%.2d:%.2d\n",
atm->tm_year + 1900,
atm->tm_mon + 1,
atm->tm_mday, atm->tm_hour, atm->tm_min, atm->tm_sec);
}
int
main(int argc, char *argv[])
{
struct timeval tvstart; // .tv_sec .tv_usec
struct timeval tvend;
struct timeval tvrslt;
struct tm tmstart; // .tm_sec .tm_min .tm_hour .tm_mday
// .tm_mon .tm_year .tm_wday .tm_yday
struct tm tmend;
struct tm tmrslt;
struct tm tmtmp;
time_t ttstart;
char buffer[256];
char *errmsg;
char *tmtmp_str;
char *tz;
int errflag = 0;
int c;
int sts;
pmSetProgname(argv[0]);
while ((c = getopt(argc, argv, "D:?")) != EOF) {
switch (c) {
case 'D': /* debug options */
sts = pmSetDebug(optarg);
if (sts < 0) {
fprintf(stderr, "%s: unrecognized debug options specification (%s)\n",
pmGetProgname(), optarg);
errflag++;
}
break;
case '?':
default:
errflag++;
break;
}
}
if (errflag) {
fprintf(stderr, "Usage: %s [-D debug] [strftime_fmt ...]\n", pmGetProgname());
exit(1);
}
ttstart = 1392649730; // time(&ttstart) => time_t
ttstart = 1390057730;
tvstart.tv_sec = ttstart;
tvstart.tv_usec = 0;
localtime_r(&ttstart, &tmstart); // time_t => tm
set_tm(&tvend, &tmend, &tmstart, 0, 27, 11, 28);
printf(" ");
dump_dt("start ", &tmstart);
printf(" ");
dump_dt("end ", &tmend);
tz = getenv("TZ");
if (tz != NULL) {
pmNewZone(tz);
}
printf("These time terms are relative to the start/end time.\n"
"#1 __pmParseTime #2 pmParseTimeWindow/Start #3 pmParseTimeWindow/End.\n");
set_tm(NULL, &tmtmp, &tmstart, 0, 19, 11, 45);
tmtmp_str = asctime(&tmtmp);
char *tmtmp_c = strchr(tmtmp_str, '\n');
if (tmtmp_c)
*tmtmp_c = ' ';
if (__pmParseTime(tmtmp_str, &tvstart, &tvend, &tvrslt, &errmsg) != 0) {
printf ("%s: %s\n", errmsg, tmtmp_str);
}
localtime_r(&tvrslt.tv_sec, &tmrslt); // time_t => tm
printf(" ");
dump_dt(tmtmp_str, &tmrslt);
// See strftime for a description of the % formats
char *strftime_fmt[] = {
"+1minute",
"-1 minute",
"-1minute",
"%F",
"%D",
"%D %r",
"%D %r -1month",
"%D %r tomorrow",
"%D %r yesterday",
"%D %R",
"%D %T",
"%d %b %Y %X",
"1 day ago",
"1 week ago",
"@%F",
"@%D",
"@%D %r",
"@%D %R",
"@%D %T",
"@%D %T GMT",
"@%d %b %Y %X",
"@next day",
"@1 day ago",
"1 day",
"5 minutes 5 seconds",
"last week",
"last day",
"next day",
// relative to current time terms begin here
"now",
"today",
"@yesterday",
"yesterday",
"tomorrow",
"sunday",
"first sunday",
"this sunday",
"next sunday",
"last sunday",
"monday",
"first monday",
"this monday",
"next monday",
"last monday",
"tuesday",
"first tuesday",
"this tuesday",
"next tuesday",
"last tuesday",
"wednesday",
"first wednesday",
"this wednesday",
"next wednesday",
"last wednesday",
"thursday",
"first thursday",
"this thursday",
"next thursday",
"last thursday",
"friday",
"first friday",
"this friday",
"next friday",
"last friday",
"saturday",
"first saturday",
"this saturday",
"next saturday",
"last saturday",
};
int sfx;
for (sfx = 0; sfx < (sizeof(strftime_fmt) / sizeof(void *)); sfx++) {
char *fmt = strftime_fmt[sfx];
int len;
/* non-flag args are argv[optind] ... argv[argc-1] */
if (optind < argc) {
/* over-ride from command line */
if (sfx+optind >= argc) return 0;
fmt = argv[sfx+optind];
}
if (pmDebugOptions.appl0)
fprintf(stderr, "tmtmp: sec=%d min=%d hour=%d mday=%d mon=%d year=%d wday=%d yday=%d isdst=%d\n",
tmtmp.tm_sec, tmtmp.tm_min, tmtmp.tm_hour, tmtmp.tm_mday,
tmtmp.tm_mon, tmtmp.tm_year, tmtmp.tm_wday, tmtmp.tm_yday,
tmtmp.tm_isdst);
len = strftime(buffer, sizeof(buffer), fmt, &tmtmp);
if (len != 0) {
struct timeval rsltStart;
struct timeval rsltEnd;
struct timeval rsltOffset;
if (strcmp(fmt, "now") == 0)
printf
("These time terms for a specific day are relative to the current time.\n");
if (__pmParseTime(buffer, &tvstart, &tvend, &tvrslt, &errmsg) != 0) {
printf ("%s: %s\n", errmsg, tmtmp_str);
}
localtime_r(&tvrslt.tv_sec, &tmrslt); // time_t => tm
if (pmDebugOptions.appl0)
fprintf(stderr, "tmrslt: sec=%d min=%d hour=%d mday=%d mon=%d year=%d wday=%d yday=%d isdst=%d\n",
tmrslt.tm_sec, tmrslt.tm_min, tmrslt.tm_hour, tmrslt.tm_mday,
tmrslt.tm_mon, tmrslt.tm_year, tmrslt.tm_wday, tmrslt.tm_yday,
tmrslt.tm_isdst);
printf("#1 ");
dump_dt(buffer, &tmrslt);
if (pmParseTimeWindow(buffer, NULL, NULL, NULL, &tvstart, &tvend, &rsltStart, &rsltEnd, &rsltOffset, &errmsg) < 0) {
printf ("%s: %s\n", errmsg, tmtmp_str);
}
localtime_r(&rsltStart.tv_sec, &tmrslt); // time_t => tm
if (pmDebugOptions.appl0)
fprintf(stderr, "tmrslt: sec=%d min=%d hour=%d mday=%d mon=%d year=%d wday=%d yday=%d isdst=%d\n",
tmrslt.tm_sec, tmrslt.tm_min, tmrslt.tm_hour, tmrslt.tm_mday,
tmrslt.tm_mon, tmrslt.tm_year, tmrslt.tm_wday, tmrslt.tm_yday,
tmrslt.tm_isdst);
printf("#2 ");
dump_dt(buffer, &tmrslt);
localtime_r(&rsltEnd.tv_sec, &tmrslt); // time_t => tm
if (pmDebugOptions.appl0)
fprintf(stderr, "tmrslt: sec=%d min=%d hour=%d mday=%d mon=%d year=%d wday=%d yday=%d isdst=%d\n",
tmrslt.tm_sec, tmrslt.tm_min, tmrslt.tm_hour, tmrslt.tm_mday,
tmrslt.tm_mon, tmrslt.tm_year, tmrslt.tm_wday, tmrslt.tm_yday,
tmrslt.tm_isdst);
printf("#3 ");
dump_dt(buffer, &tmrslt);
}
else
printf("strftime format \"%s\" not recognized\n", fmt);
}
return 0;
}
|