Debian Codegraph @

tapset/linux/ctime.stp

raw
1 /* ctime.stp - Convert seconds to human readable date string.
2 *
3 * This code was adapted from the newlib mktm_r() and asctime_r()
4 * functions. In newlib, mktm_r.c states that it was adapted from
5 * tzcode maintained by Arthur David Olson. In newlib, asctime_r.c
6 * doesn't have any author/copyright information.
7 *
8 * Changes copyright (C) 2006-2016 Red Hat Inc.
9 */
10
11 /**
12 * sfunction ctime - Convert seconds since epoch into human readable date/time string
13 *
14 * @epochsecs: Number of seconds since epoch (as returned by gettimeofday_s())
15 *
16 * Description: Takes an argument of seconds since the epoch as returned by
17 * gettimeofday_s(). Returns a string of the form
18 *
19 * "Wed Jun 30 21:49:08 1993"
20 *
21 * The string will always be exactly 24 characters. If the time would
22 * be unreasonable far in the past (before what can be represented
23 * with a 32 bit offset in seconds from the epoch) an error will occur
24 * (which can be avoided with try/catch). If the time would be
25 * unreasonable far in the future, an error will also occur.
26 *
27 * Note that the epoch (zero) corresponds to
28 *
29 * "Thu Jan 1 00:00:00 1970"
30 *
31 * The earliest full date given by ctime, corresponding to epochsecs
32 * -2147483648 is "Fri Dec 13 20:45:52 1901". The latest full date
33 * given by ctime, corresponding to epochsecs 2147483647 is
34 * "Tue Jan 19 03:14:07 2038".
35 *
36 * The abbreviations for the days of the week are ‘Sun’, ‘Mon’, ‘Tue’,
37 * ‘Wed’, ‘Thu’, ‘Fri’, and ‘Sat’. The abbreviations for the months
38 * are ‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’, ‘Aug’, ‘Sep’,
39 * ‘Oct’, ‘Nov’, and ‘Dec’.
40 *
41 * Note that the real C library ctime() function puts a newline ('\n')
42 * character at the end of the string that this function does not.
43 * Also note that since the kernel has no concept of timezones, the
44 * returned time is always in GMT.
45 */
46 function ctime:string(epochsecs:long)
47 %{ /* pure */
48
49 #define SECSPERMIN 60L
50 #define MINSPERHOUR 60L
51 #define HOURSPERDAY 24L
52 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
53 #define SECSPERDAY (SECSPERHOUR * HOURSPERDAY)
54 #define DAYSPERWEEK 7
55 #define MONSPERYEAR 12
56
57 #define EPOCH_YEAR 1970
58 #define EPOCH_WDAY 4
59
60 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
61
62 static const int mon_lengths[2][MONSPERYEAR] = {
63 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
64 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
65 } ;
66
67 static const int year_lengths[2] = {
68 365,
69 366
70 } ;
71
72 static const char day_name[7][4] = {
73 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
74 };
75 static const char mon_name[12][4] = {
76 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
77 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
78 };
79
80 long days, rem;
81 #if defined(STAPCONF_TIME32_OLD_H)
82 old_time32_t lcltime;
83 #else
84 time_t lcltime;
85 #endif
86 int yleap;
87 const int *ip;
88 int tm_sec; /* seconds */
89 int tm_min; /* minutes */
90 int tm_hour; /* hours */
91 int tm_mday; /* day of the month */
92 int tm_mon; /* month */
93 int tm_year; /* year */
94 int tm_wday; /* day of the week */
95
96 // Check that the numer of seconds is "reasonable".
97 // Otherwise (especially on 64bit machines) we will be spending
98 // way too much time calculating the correct year, month and
99 // day. Also we would like the returned string to always be 24 chars.
100 // So cap to what can be represented normally on a 32bit machine.
101 int64_t MAX_POS_SECS = 2147483647LL;
102 int64_t MIN_NEG_SECS = -2147483648LL;
103
104 if (STAP_ARG_epochsecs > MAX_POS_SECS)
105 {
106 #if STAP_COMPAT_VERSION <= STAP_VERSION(2,2)
107 strlcpy(STAP_RETVALUE, "far far in the future...", MAXSTRINGLEN);
108 #else
109 CONTEXT->last_error = "Time too far in the future to be converted";
110 #endif
111 return;
112 }
113 if (STAP_ARG_epochsecs < MIN_NEG_SECS)
114 {
115 #if STAP_COMPAT_VERSION <= STAP_VERSION(2,2)
116 strlcpy(STAP_RETVALUE, "a long, long time ago...", MAXSTRINGLEN);
117 #else
118 CONTEXT->last_error = "Time too far in the past to be converted";
119 #endif
120 return;
121 }
122
123 lcltime = STAP_ARG_epochsecs;
124
125 days = ((long)lcltime) / SECSPERDAY;
126 rem = ((long)lcltime) % SECSPERDAY;
127 while (rem < 0)
128 {
129 rem += SECSPERDAY;
130 --days;
131 }
132 while (rem >= SECSPERDAY)
133 {
134 rem -= SECSPERDAY;
135 ++days;
136 }
137
138 /* compute hour, min, and sec */
139 tm_hour = (int) (rem / SECSPERHOUR);
140 rem %= SECSPERHOUR;
141 tm_min = (int) (rem / SECSPERMIN);
142 tm_sec = (int) (rem % SECSPERMIN);
143
144 /* compute day of week */
145 if ((tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
146 tm_wday += DAYSPERWEEK;
147
148 /* compute year & day of year */
149 tm_year = EPOCH_YEAR;
150 if (days >= 0)
151 {
152 for (;;)
153 {
154 yleap = isleap(tm_year);
155 if (days < year_lengths[yleap])
156 break;
157 tm_year++;
158 days -= year_lengths[yleap];
159 }
160 }
161 else
162 {
163 do
164 {
165 --tm_year;
166 yleap = isleap(tm_year);
167 days += year_lengths[yleap];
168 } while (days < 0);
169 }
170
171 ip = mon_lengths[yleap];
172 for (tm_mon = 0; days >= ip[tm_mon]; ++tm_mon)
173 days -= ip[tm_mon];
174 tm_mday = days + 1;
175
176 /*
177 * At this point we have all our information. Now we need to
178 * convert it to an ascii representation.
179 */
180
181 snprintf (STAP_RETVALUE, MAXSTRINGLEN, "%.3s %.3s%3d %.2d:%.2d:%.2d %d",
182 day_name[tm_wday], mon_name[tm_mon],
183 tm_mday, tm_hour, tm_min,
184 tm_sec, tm_year);
185 %}
186
187
188 /**
189 * sfunction ctime - Convert seconds since epoch into human readable date/time string
190 *
191 * Without an argument, does the same as ctime(gettimeofday_s()).
192 *
193 */
194 function ctime:string()
195 {
196 return ctime(gettimeofday_s())
197 }