1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
9 /* A function for returning the time of day in various formats */
14 /* #define TESTING_LOG_DATESTAMP */
17 static uschar timebuf[sizeof("www, dd-mmm-yyyy hh:mm:ss.ddd +zzzz")];
20 /*************************************************
22 *************************************************/
24 /* The log timestamp format is dd-mmm-yy so as to be non-confusing on both
25 sides of the Atlantic. We calculate an explicit numerical offset from GMT for
26 the full datestamp and BSD inbox datestamp. Note that on some systems
27 localtime() and gmtime() re-use the same store, so we must save the local time
28 values before calling gmtime(). If timestamps_utc is set, don't use
29 localtime(); all times are then in UTC (with offset +0000).
31 There are also some contortions to get the day of the month without
32 a leading zero for the full stamp, since Ustrftime() doesn't provide this
35 Argument: type of timestamp required:
36 tod_bsdin BSD inbox format
37 tod_epoch Unix epoch format
38 tod_epochl Unix epoch/usec format
39 tod_full full date and time
40 tod_log log file data line format,
41 with zone if log_timezone is TRUE
42 tod_log_bare always without zone
43 tod_log_datestamp_daily for log file names when datestamped daily
44 tod_log_datestamp_monthly for log file names when datestamped monthly
45 tod_log_zone always with zone
46 tod_mbx MBX inbox format
47 tod_zone just the timezone offset
48 tod_zulu time in 8601 zulu format
50 Returns: pointer to fixed buffer containing the timestamp
59 gettimeofday(&now, NULL);
61 /* Styles that don't need local time */
66 (void) snprintf(CS timebuf, sizeof(timebuf), TIME_T_FMT, now.tv_sec); /* Unix epoch format */
67 return timebuf; /* NB the above will be wrong if time_t is FP */
70 /* Unix epoch/usec format */
71 (void) snprintf(CS timebuf, sizeof(timebuf), TIME_T_FMT "%06ld", now.tv_sec, (long) now.tv_usec );
75 t = gmtime(&now.tv_sec);
76 (void) snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u%02u%02u%02uZ",
77 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, (uint)t->tm_hour, (uint)t->tm_min,
82 /* Vary log type according to timezone requirement */
84 if (type == tod_log) type = log_timezone ? tod_log_zone : tod_log_bare;
86 /* Convert to local time or UTC */
88 t = f.timestamps_utc ? gmtime(&now.tv_sec) : localtime(&now.tv_sec);
92 case tod_log_bare: /* Format used in logging without timezone */
93 #ifndef COMPILE_UTILITY
94 if (LOGGING(millisec))
95 snprintf(CS timebuf, sizeof(timebuf), "%04u-%02u-%02u %02u:%02u:%02u.%03u",
96 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday,
97 (uint)t->tm_hour, (uint)t->tm_min, (uint)t->tm_sec,
98 (uint)(now.tv_usec/1000));
101 snprintf(CS timebuf, sizeof(timebuf), "%04u-%02u-%02u %02u:%02u:%02u",
102 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday,
103 (uint)t->tm_hour, (uint)t->tm_min, (uint)t->tm_sec);
107 /* Format used as suffix of log file name when 'log_datestamp' is active. For
108 testing purposes, it changes the file every second. */
110 #ifdef TESTING_LOG_DATESTAMP
111 case tod_log_datestamp_daily:
112 case tod_log_datestamp_monthly:
113 snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u%02u%02u",
114 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday,
115 (uint)t->tm_hour, (uint)t->tm_min);
119 case tod_log_datestamp_daily:
120 snprintf(CS timebuf, sizeof(timebuf), "%04u%02u%02u",
121 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday);
124 case tod_log_datestamp_monthly:
125 #ifndef COMPILE_UTILITY
126 snprintf(CS timebuf, sizeof(timebuf), "%04u%02u",
127 1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon);
132 /* Format used in BSD inbox separator lines. Sort-of documented in RFC 976
133 ("UUCP Mail Interchange Format Standard") but only by example, not by
134 explicit definition. The examples show no timezone offsets, and some MUAs
135 appear to be sensitive to this, so Exim has been changed to remove the
136 timezone offsets that originally appeared. */
140 int len = Ustrftime(timebuf, sizeof(timebuf), "%a %b %d %H:%M:%S", t);
141 Ustrftime(timebuf + len, sizeof(timebuf) - len, " %Y", t);
145 /* Other types require the GMT offset to be calculated, or just set up in the
146 case of UTC timestamping. We need to take a copy of the local time first. */
150 int diff_hour, diff_min;
152 struct tm * lp = &local;
153 memcpy(lp, t, sizeof(struct tm));
155 if (f.timestamps_utc)
156 diff_hour = diff_min = 0;
159 struct tm * gmt = gmtime(&now.tv_sec);
161 if (local.tm_sec == gmt->tm_sec) /* usual case */
163 diff_min = 60 * (local.tm_hour - gmt->tm_hour)
164 + local.tm_min - gmt->tm_min;
165 if (local.tm_year != gmt->tm_year)
166 diff_min += (local.tm_year > gmt->tm_year) ? 1440 : -1440;
167 else if (local.tm_yday != gmt->tm_yday)
168 diff_min += (local.tm_yday > gmt->tm_yday) ? 1440 : -1440;
169 diff_hour = diff_min/60;
170 diff_min = abs(diff_min - diff_hour*60);
172 else /* subminute offset, eg. TAI */
174 lp = gmt; /* pretend we're in UTC */
175 diff_min = diff_hour = 0;
181 case tod_log_zone: /* Format used in logging with timezone */
182 #ifndef COMPILE_UTILITY
183 if (LOGGING(millisec))
184 (void) snprintf(CS timebuf, sizeof(timebuf),
185 "%04u-%02u-%02u %02u:%02u:%02u.%03u %+03d%02d",
186 1900 + (uint)lp->tm_year, 1 + (uint)lp->tm_mon, (uint)lp->tm_mday,
187 (uint)lp->tm_hour, (uint)lp->tm_min, (uint)lp->tm_sec, (uint)(now.tv_usec/1000),
188 diff_hour, diff_min);
191 (void) snprintf(CS timebuf, sizeof(timebuf),
192 "%04u-%02u-%02u %02u:%02u:%02u %+03d%02d",
193 1900 + (uint)lp->tm_year, 1 + (uint)lp->tm_mon, (uint)lp->tm_mday,
194 (uint)lp->tm_hour, (uint)lp->tm_min, (uint)lp->tm_sec,
195 diff_hour, diff_min);
198 case tod_zone: /* Just the timezone offset */
199 (void) snprintf(CS timebuf, sizeof(timebuf), "%+03d%02d", diff_hour, diff_min);
202 /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */
207 int len = snprintf(CS timebuf, sizeof(timebuf),
208 "%02u-", (uint)lp->tm_mday);
209 len += Ustrftime(timebuf + len, sizeof(timebuf) - len,
210 "%b-%Y %H:%M:%S", lp);
211 (void) snprintf(CS timebuf + len, sizeof(timebuf)-len,
212 " %+03d%02d", diff_hour, diff_min);
217 /* tod_full: format used in Received: headers (use as default just in case
218 called with a junk type value) */
222 int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", lp);
223 len += snprintf(CS timebuf + len, sizeof(timebuf)-len,
224 "%02u ", (uint)lp->tm_mday);
225 len += Ustrftime(timebuf + len, sizeof(timebuf) - len,
226 "%b %Y %H:%M:%S", lp);
227 (void) snprintf(CS timebuf + len, sizeof(timebuf)-len,
228 " %+03d%02d", diff_hour, diff_min);