| 1 |
/* ctime.stp - Convert seconds to human readable date string. |
| 2 |
* |
| 3 |
* Copyright (C) 2012-2016 Red Hat, Inc. |
| 4 |
*/ |
| 5 |
|
| 6 |
/* NB: functionally equivalent to linux/ctime.stp |
| 7 |
* This means unlike normal userspace ctime(), this version |
| 8 |
* returns time in UTC, without a final newline. |
| 9 |
*/ |
| 10 |
|
| 11 |
%{ |
| 12 |
#include <string.h> |
| 13 |
#include <time.h> |
| 14 |
%} |
| 15 |
|
| 16 |
function ctime:string(epochsecs:long) |
| 17 |
%{ /* pure */ |
| 18 |
struct tm gmt; |
| 19 |
char buf[26]; |
| 20 |
|
| 21 |
const time_t t = STAP_ARG_epochsecs; |
| 22 |
|
| 23 |
if (gmtime_r(&t, &gmt) && asctime_r(&gmt, buf)) { |
| 24 |
char* newline = strchr(buf, '\n'); |
| 25 |
if (newline) |
| 26 |
*newline = '\0'; |
| 27 |
strlcpy (STAP_RETVALUE, buf, MAXSTRINGLEN); |
| 28 |
} else { |
| 29 |
#if STAP_COMPAT_VERSION <= STAP_VERSION(2,2) |
| 30 |
strlcpy(STAP_RETVALUE, "<invalid time>", MAXSTRINGLEN); |
| 31 |
#else |
| 32 |
CONTEXT->last_error = "Time cannot be converted"; |
| 33 |
#endif |
| 34 |
} |
| 35 |
%} |
| 36 |
|
| 37 |
function ctime:string() |
| 38 |
{ |
| 39 |
return ctime(gettimeofday_s()) |
| 40 |
} |