Commit | Line | Data |
---|---|---|
059ec3d9 PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
f9ba5e22 | 5 | /* Copyright (c) University of Cambridge 1995 - 2018 */ |
059ec3d9 PH |
6 | /* See the file NOTICE for conditions of use and distribution. */ |
7 | ||
8 | /* A function for returning the time of day in various formats */ | |
9 | ||
10 | ||
11 | #include "exim.h" | |
12 | ||
13 | /* #define TESTING_LOG_DATESTAMP */ | |
14 | ||
15 | ||
571b2715 | 16 | static uschar timebuf[sizeof("www, dd-mmm-yyyy hh:mm:ss.ddd +zzzz")]; |
059ec3d9 PH |
17 | |
18 | ||
19 | /************************************************* | |
20 | * Return timestamp * | |
21 | *************************************************/ | |
22 | ||
23 | /* The log timestamp format is dd-mmm-yy so as to be non-confusing on both | |
24 | sides of the Atlantic. We calculate an explicit numerical offset from GMT for | |
25 | the full datestamp and BSD inbox datestamp. Note that on some systems | |
26 | localtime() and gmtime() re-use the same store, so we must save the local time | |
27 | values before calling gmtime(). If timestamps_utc is set, don't use | |
28 | localtime(); all times are then in UTC (with offset +0000). | |
29 | ||
30 | There are also some contortions to get the day of the month without | |
31 | a leading zero for the full stamp, since Ustrftime() doesn't provide this | |
32 | option. | |
33 | ||
34 | Argument: type of timestamp required: | |
f1e5fef5 PP |
35 | tod_bsdin BSD inbox format |
36 | tod_epoch Unix epoch format | |
f5787926 | 37 | tod_epochl Unix epoch/usec format |
f1e5fef5 PP |
38 | tod_full full date and time |
39 | tod_log log file data line format, | |
40 | with zone if log_timezone is TRUE | |
41 | tod_log_bare always without zone | |
42 | tod_log_datestamp_daily for log file names when datestamped daily | |
43 | tod_log_datestamp_monthly for log file names when datestamped monthly | |
44 | tod_log_zone always with zone | |
45 | tod_mbx MBX inbox format | |
46 | tod_zone just the timezone offset | |
47 | tod_zulu time in 8601 zulu format | |
059ec3d9 PH |
48 | |
49 | Returns: pointer to fixed buffer containing the timestamp | |
50 | */ | |
51 | ||
52 | uschar * | |
53 | tod_stamp(int type) | |
54 | { | |
571b2715 JH |
55 | struct timeval now; |
56 | struct tm * t; | |
059ec3d9 | 57 | |
571b2715 | 58 | gettimeofday(&now, NULL); |
059ec3d9 PH |
59 | |
60 | /* Styles that don't need local time */ | |
61 | ||
571b2715 | 62 | switch(type) |
059ec3d9 | 63 | { |
571b2715 | 64 | case tod_epoch: |
b09c5f34 | 65 | (void) snprintf(CS timebuf, sizeof(timebuf), TIME_T_FMT, now.tv_sec); /* Unix epoch format */ |
571b2715 JH |
66 | return timebuf; /* NB the above will be wrong if time_t is FP */ |
67 | ||
68 | case tod_epoch_l: | |
69 | /* Unix epoch/usec format */ | |
b09c5f34 | 70 | (void) snprintf(CS timebuf, sizeof(timebuf), TIME_T_FMT "%06ld", now.tv_sec, (long) now.tv_usec ); |
571b2715 JH |
71 | return timebuf; |
72 | ||
73 | case tod_zulu: | |
74 | t = gmtime(&now.tv_sec); | |
b09c5f34 | 75 | (void) snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u%02u%02u%02uZ", |
13c7874e JH |
76 | 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, (uint)t->tm_hour, (uint)t->tm_min, |
77 | (uint)t->tm_sec); | |
571b2715 | 78 | return timebuf; |
059ec3d9 PH |
79 | } |
80 | ||
571b2715 JH |
81 | /* Vary log type according to timezone requirement */ |
82 | ||
83 | if (type == tod_log) type = log_timezone ? tod_log_zone : tod_log_bare; | |
059ec3d9 PH |
84 | |
85 | /* Convert to local time or UTC */ | |
86 | ||
8768d548 | 87 | t = f.timestamps_utc ? gmtime(&now.tv_sec) : localtime(&now.tv_sec); |
059ec3d9 PH |
88 | |
89 | switch(type) | |
90 | { | |
91 | case tod_log_bare: /* Format used in logging without timezone */ | |
753b36c5 HSHR |
92 | #ifndef COMPILE_UTILITY |
93 | if (LOGGING(millisec)) | |
b09c5f34 | 94 | snprintf(CS timebuf, sizeof(timebuf), "%04u-%02u-%02u %02u:%02u:%02u.%03u", |
13c7874e JH |
95 | 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, |
96 | (uint)t->tm_hour, (uint)t->tm_min, (uint)t->tm_sec, | |
97 | (uint)(now.tv_usec/1000)); | |
753b36c5 HSHR |
98 | else |
99 | #endif | |
b09c5f34 | 100 | snprintf(CS timebuf, sizeof(timebuf), "%04u-%02u-%02u %02u:%02u:%02u", |
13c7874e JH |
101 | 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, |
102 | (uint)t->tm_hour, (uint)t->tm_min, (uint)t->tm_sec); | |
753b36c5 | 103 | |
571b2715 | 104 | break; |
059ec3d9 | 105 | |
571b2715 JH |
106 | /* Format used as suffix of log file name when 'log_datestamp' is active. For |
107 | testing purposes, it changes the file every second. */ | |
059ec3d9 | 108 | |
571b2715 | 109 | #ifdef TESTING_LOG_DATESTAMP |
f1e5fef5 PP |
110 | case tod_log_datestamp_daily: |
111 | case tod_log_datestamp_monthly: | |
b09c5f34 | 112 | snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u%02u%02u", |
13c7874e JH |
113 | 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, |
114 | (uint)t->tm_hour, (uint)t->tm_min); | |
571b2715 | 115 | break; |
f1e5fef5 | 116 | |
571b2715 | 117 | #else |
f1e5fef5 | 118 | case tod_log_datestamp_daily: |
b09c5f34 | 119 | snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u", |
13c7874e | 120 | 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday); |
571b2715 | 121 | break; |
059ec3d9 | 122 | |
f1e5fef5 | 123 | case tod_log_datestamp_monthly: |
cab0c277 | 124 | #ifndef COMPILE_UTILITY |
b09c5f34 | 125 | snprintf(CS timebuf, sizeof(timebuf), "%04u%02u", |
13c7874e | 126 | 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon); |
cab0c277 | 127 | #endif |
571b2715 JH |
128 | break; |
129 | #endif | |
f1e5fef5 | 130 | |
571b2715 JH |
131 | /* Format used in BSD inbox separator lines. Sort-of documented in RFC 976 |
132 | ("UUCP Mail Interchange Format Standard") but only by example, not by | |
133 | explicit definition. The examples show no timezone offsets, and some MUAs | |
134 | appear to be sensitive to this, so Exim has been changed to remove the | |
135 | timezone offsets that originally appeared. */ | |
059ec3d9 PH |
136 | |
137 | case tod_bsdin: | |
059ec3d9 | 138 | { |
571b2715 JH |
139 | int len = Ustrftime(timebuf, sizeof(timebuf), "%a %b %d %H:%M:%S", t); |
140 | Ustrftime(timebuf + len, sizeof(timebuf) - len, " %Y", t); | |
059ec3d9 | 141 | } |
571b2715 JH |
142 | break; |
143 | ||
144 | /* Other types require the GMT offset to be calculated, or just set up in the | |
145 | case of UTC timestamping. We need to take a copy of the local time first. */ | |
059ec3d9 | 146 | |
571b2715 | 147 | default: |
059ec3d9 | 148 | { |
571b2715 JH |
149 | int diff_hour, diff_min; |
150 | struct tm local; | |
158d7137 AW |
151 | struct tm * lp = &local; |
152 | memcpy(lp, t, sizeof(struct tm)); | |
571b2715 | 153 | |
8768d548 | 154 | if (f.timestamps_utc) |
571b2715 JH |
155 | diff_hour = diff_min = 0; |
156 | else | |
157 | { | |
158 | struct tm * gmt = gmtime(&now.tv_sec); | |
159 | ||
158d7137 AW |
160 | if (local.tm_sec == gmt->tm_sec) /* usual case */ |
161 | { | |
162 | diff_min = 60 * (local.tm_hour - gmt->tm_hour) | |
163 | + local.tm_min - gmt->tm_min; | |
164 | if (local.tm_year != gmt->tm_year) | |
165 | diff_min += (local.tm_year > gmt->tm_year) ? 1440 : -1440; | |
166 | else if (local.tm_yday != gmt->tm_yday) | |
167 | diff_min += (local.tm_yday > gmt->tm_yday) ? 1440 : -1440; | |
168 | diff_hour = diff_min/60; | |
169 | diff_min = abs(diff_min - diff_hour*60); | |
170 | } | |
171 | else /* subminute offset, eg. TAI */ | |
172 | { | |
173 | lp = gmt; /* pretend we're in UTC */ | |
174 | diff_min = diff_hour = 0; | |
175 | } | |
571b2715 JH |
176 | } |
177 | ||
178 | switch(type) | |
179 | { | |
180 | case tod_log_zone: /* Format used in logging with timezone */ | |
181 | #ifndef COMPILE_UTILITY | |
182 | if (LOGGING(millisec)) | |
b09c5f34 | 183 | (void) snprintf(CS timebuf, sizeof(timebuf), |
13c7874e | 184 | "%04u-%02u-%02u %02u:%02u:%02u.%03u %+03d%02d", |
158d7137 AW |
185 | 1900 + (uint)lp->tm_year, 1 + (uint)lp->tm_mon, (uint)lp->tm_mday, |
186 | (uint)lp->tm_hour, (uint)lp->tm_min, (uint)lp->tm_sec, (uint)(now.tv_usec/1000), | |
571b2715 JH |
187 | diff_hour, diff_min); |
188 | else | |
189 | #endif | |
b09c5f34 | 190 | (void) snprintf(CS timebuf, sizeof(timebuf), |
13c7874e | 191 | "%04u-%02u-%02u %02u:%02u:%02u %+03d%02d", |
158d7137 AW |
192 | 1900 + (uint)lp->tm_year, 1 + (uint)lp->tm_mon, (uint)lp->tm_mday, |
193 | (uint)lp->tm_hour, (uint)lp->tm_min, (uint)lp->tm_sec, | |
571b2715 JH |
194 | diff_hour, diff_min); |
195 | break; | |
196 | ||
197 | case tod_zone: /* Just the timezone offset */ | |
b09c5f34 | 198 | (void) snprintf(CS timebuf, sizeof(timebuf), "%+03d%02d", diff_hour, diff_min); |
571b2715 JH |
199 | break; |
200 | ||
201 | /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */ | |
202 | ||
158d7137 | 203 | #ifdef SUPPORT_MBX |
571b2715 | 204 | case tod_mbx: |
158d7137 AW |
205 | { |
206 | int len = snprintf(CS timebuf, sizeof(timebuf), | |
207 | "%02u-", (uint)lp->tm_mday); | |
208 | len += Ustrftime(timebuf + len, sizeof(timebuf) - len, | |
209 | "%b-%Y %H:%M:%S", lp); | |
210 | (void) snprintf(CS timebuf + len, sizeof(timebuf)-len, | |
211 | " %+03d%02d", diff_hour, diff_min); | |
212 | } | |
571b2715 | 213 | break; |
158d7137 | 214 | #endif |
571b2715 JH |
215 | |
216 | /* tod_full: format used in Received: headers (use as default just in case | |
217 | called with a junk type value) */ | |
218 | ||
219 | default: | |
158d7137 AW |
220 | { |
221 | int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", lp); | |
222 | len += snprintf(CS timebuf + len, sizeof(timebuf)-len, | |
223 | "%02u ", (uint)lp->tm_mday); | |
224 | len += Ustrftime(timebuf + len, sizeof(timebuf) - len, | |
225 | "%b %Y %H:%M:%S", lp); | |
226 | (void) snprintf(CS timebuf + len, sizeof(timebuf)-len, | |
227 | " %+03d%02d", diff_hour, diff_min); | |
228 | } | |
571b2715 JH |
229 | break; |
230 | } | |
059ec3d9 | 231 | } |
571b2715 | 232 | break; |
059ec3d9 PH |
233 | } |
234 | ||
235 | return timebuf; | |
236 | } | |
237 | ||
238 | /* End of tod.c */ |