Testsuite: OpenSSL version output variances
[exim.git] / src / src / tod.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
8d6d5106 5/* Copyright (c) University of Cambridge 1995 - 2014 */
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 16static 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
24sides of the Atlantic. We calculate an explicit numerical offset from GMT for
25the full datestamp and BSD inbox datestamp. Note that on some systems
26localtime() and gmtime() re-use the same store, so we must save the local time
27values before calling gmtime(). If timestamps_utc is set, don't use
28localtime(); all times are then in UTC (with offset +0000).
29
30There are also some contortions to get the day of the month without
31a leading zero for the full stamp, since Ustrftime() doesn't provide this
32option.
33
34Argument: 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
49Returns: pointer to fixed buffer containing the timestamp
50*/
51
52uschar *
53tod_stamp(int type)
54{
571b2715
JH
55struct timeval now;
56struct tm * t;
57int off = 0;
059ec3d9 58
571b2715 59gettimeofday(&now, NULL);
059ec3d9
PH
60
61/* Styles that don't need local time */
62
571b2715 63switch(type)
059ec3d9 64 {
571b2715
JH
65 case tod_epoch:
66 (void) sprintf(CS timebuf, TIME_T_FMT, now.tv_sec); /* Unix epoch format */
67 return timebuf; /* NB the above will be wrong if time_t is FP */
68
69 case tod_epoch_l:
70 /* Unix epoch/usec format */
71 (void) sprintf(CS timebuf, TIME_T_FMT "%06ld", now.tv_sec, (long) now.tv_usec );
72 return timebuf;
73
74 case tod_zulu:
75 t = gmtime(&now.tv_sec);
76 (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d%02dZ",
77 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min,
78 t->tm_sec);
79 return timebuf;
059ec3d9
PH
80 }
81
571b2715
JH
82/* Vary log type according to timezone requirement */
83
84if (type == tod_log) type = log_timezone ? tod_log_zone : tod_log_bare;
059ec3d9
PH
85
86/* Convert to local time or UTC */
87
571b2715 88t = timestamps_utc ? gmtime(&now.tv_sec) : localtime(&now.tv_sec);
059ec3d9
PH
89
90switch(type)
91 {
92 case tod_log_bare: /* Format used in logging without timezone */
571b2715
JH
93 off = sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d",
94 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday,
95 t->tm_hour, t->tm_min, t->tm_sec);
96 break;
059ec3d9 97
571b2715
JH
98 /* Format used as suffix of log file name when 'log_datestamp' is active. For
99 testing purposes, it changes the file every second. */
059ec3d9 100
571b2715 101#ifdef TESTING_LOG_DATESTAMP
f1e5fef5
PP
102 case tod_log_datestamp_daily:
103 case tod_log_datestamp_monthly:
571b2715
JH
104 off = sprintf(CS timebuf, "%04d%02d%02d%02d%02d",
105 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min);
106 break;
f1e5fef5 107
571b2715 108#else
f1e5fef5 109 case tod_log_datestamp_daily:
571b2715
JH
110 off = sprintf(CS timebuf, "%04d%02d%02d",
111 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday);
112 break;
059ec3d9 113
f1e5fef5 114 case tod_log_datestamp_monthly:
cab0c277 115#ifndef COMPILE_UTILITY
571b2715
JH
116 off = sprintf(CS timebuf, "%04d%02d",
117 1900 + t->tm_year, 1 + t->tm_mon);
cab0c277 118#endif
571b2715
JH
119 break;
120#endif
f1e5fef5 121
571b2715
JH
122 /* Format used in BSD inbox separator lines. Sort-of documented in RFC 976
123 ("UUCP Mail Interchange Format Standard") but only by example, not by
124 explicit definition. The examples show no timezone offsets, and some MUAs
125 appear to be sensitive to this, so Exim has been changed to remove the
126 timezone offsets that originally appeared. */
059ec3d9
PH
127
128 case tod_bsdin:
059ec3d9 129 {
571b2715
JH
130 int len = Ustrftime(timebuf, sizeof(timebuf), "%a %b %d %H:%M:%S", t);
131 Ustrftime(timebuf + len, sizeof(timebuf) - len, " %Y", t);
059ec3d9 132 }
571b2715
JH
133 break;
134
135 /* Other types require the GMT offset to be calculated, or just set up in the
136 case of UTC timestamping. We need to take a copy of the local time first. */
059ec3d9 137
571b2715 138 default:
059ec3d9 139 {
571b2715
JH
140 int diff_hour, diff_min;
141 struct tm local;
142 memcpy(&local, t, sizeof(struct tm));
143
144 if (timestamps_utc)
145 diff_hour = diff_min = 0;
146 else
147 {
148 struct tm * gmt = gmtime(&now.tv_sec);
149
150 diff_min = 60*(local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min;
151 if (local.tm_year != gmt->tm_year)
152 diff_min += (local.tm_year > gmt->tm_year)? 1440 : -1440;
153 else if (local.tm_yday != gmt->tm_yday)
154 diff_min += (local.tm_yday > gmt->tm_yday)? 1440 : -1440;
155 diff_hour = diff_min/60;
156 diff_min = abs(diff_min - diff_hour*60);
157 }
158
159 switch(type)
160 {
161 case tod_log_zone: /* Format used in logging with timezone */
162#ifndef COMPILE_UTILITY
163 if (LOGGING(millisec))
164 (void) sprintf(CS timebuf,
165 "%04d-%02d-%02d %02d:%02d:%02d.%03d %+03d%02d",
166 1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday,
cab0c277 167 local.tm_hour, local.tm_min, local.tm_sec, (int)(now.tv_usec/1000),
571b2715
JH
168 diff_hour, diff_min);
169 else
170#endif
171 (void) sprintf(CS timebuf,
172 "%04d-%02d-%02d %02d:%02d:%02d %+03d%02d",
173 1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday,
174 local.tm_hour, local.tm_min, local.tm_sec,
175 diff_hour, diff_min);
176 break;
177
178 case tod_zone: /* Just the timezone offset */
179 (void) sprintf(CS timebuf, "%+03d%02d", diff_hour, diff_min);
180 break;
181
182 /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */
183
184 #ifdef SUPPORT_MBX
185 case tod_mbx:
186 {
187 int len;
188 (void) sprintf(CS timebuf, "%02d-", local.tm_mday);
189 len = Ustrlen(timebuf);
190 len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b-%Y %H:%M:%S",
191 &local);
192 (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min);
193 }
194 break;
195 #endif
196
197 /* tod_full: format used in Received: headers (use as default just in case
198 called with a junk type value) */
199
200 default:
201 {
202 int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", &local);
203 (void) sprintf(CS timebuf + len, "%02d ", local.tm_mday);
204 len += Ustrlen(timebuf + len);
205 len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b %Y %H:%M:%S",
206 &local);
207 (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min);
208 }
209 break;
210 }
059ec3d9 211 }
571b2715 212 break;
059ec3d9
PH
213 }
214
571b2715
JH
215#ifndef COMPILE_UTILITY
216if (LOGGING(millisec) && off > 0)
cab0c277
JH
217 (void) sprintf(CS timebuf + off, ".%03d", (int)(now.tv_usec/1000));
218#else
219off = off; /* Compiler quietening */
571b2715
JH
220#endif
221
059ec3d9
PH
222return timebuf;
223}
224
225/* End of tod.c */