DMARC documentation and license
[exim.git] / src / src / tod.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
0a49a7a4 5/* Copyright (c) University of Cambridge 1995 - 2009 */
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
16static uschar timebuf[sizeof("www, dd-mmm-yyyy hh:mm:ss +zzzz")];
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{
f5787926 55time_t now;
059ec3d9
PH
56struct tm *t;
57
f5787926
JH
58if (type == tod_epoch_l)
59 {
60 struct timeval tv;
61 gettimeofday(&tv, NULL);
62 (void) sprintf(CS timebuf, "%ld%06ld", tv.tv_sec, tv.tv_usec ); /* Unix epoch/usec format */
63 return timebuf;
64 }
65
66now = time(NULL);
67
059ec3d9
PH
68/* Vary log type according to timezone requirement */
69
70if (type == tod_log) type = log_timezone? tod_log_zone : tod_log_bare;
71
72/* Styles that don't need local time */
73
74else if (type == tod_epoch)
75 {
76 (void) sprintf(CS timebuf, "%d", (int)now); /* Unix epoch format */
77 return timebuf;
78 }
79
80else if (type == tod_zulu)
81 {
82 t = gmtime(&now);
83 (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d%02dZ",
84 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min,
85 t->tm_sec);
86 return timebuf;
87 }
88
89/* Convert to local time or UTC */
90
91t = timestamps_utc? gmtime(&now) : localtime(&now);
92
93switch(type)
94 {
95 case tod_log_bare: /* Format used in logging without timezone */
96 (void) sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d",
97 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday,
98 t->tm_hour, t->tm_min, t->tm_sec);
99 break;
100
101 /* Format used as suffix of log file name when 'log_datestamp' is active. For
102 testing purposes, it changes the file every second. */
103
059ec3d9 104 #ifdef TESTING_LOG_DATESTAMP
f1e5fef5
PP
105 case tod_log_datestamp_daily:
106 case tod_log_datestamp_monthly:
059ec3d9
PH
107 (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d",
108 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min);
f1e5fef5
PP
109 break;
110
059ec3d9 111 #else
f1e5fef5 112 case tod_log_datestamp_daily:
059ec3d9
PH
113 (void) sprintf(CS timebuf, "%04d%02d%02d",
114 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday);
059ec3d9
PH
115 break;
116
f1e5fef5
PP
117 case tod_log_datestamp_monthly:
118 (void) sprintf(CS timebuf, "%04d%02d",
119 1900 + t->tm_year, 1 + t->tm_mon);
120 break;
121 #endif
122
059ec3d9
PH
123 /* Format used in BSD inbox separator lines. Sort-of documented in RFC 976
124 ("UUCP Mail Interchange Format Standard") but only by example, not by
125 explicit definition. The examples show no timezone offsets, and some MUAs
126 appear to be sensitive to this, so Exim has been changed to remove the
127 timezone offsets that originally appeared. */
128
129 case tod_bsdin:
130 {
131 int len = Ustrftime(timebuf, sizeof(timebuf), "%a %b %d %H:%M:%S", t);
132 Ustrftime(timebuf + len, sizeof(timebuf) - len, " %Y", t);
133 }
134 break;
135
136 /* Other types require the GMT offset to be calculated, or just set up in the
137 case of UTC timestamping. We need to take a copy of the local time first. */
138
139 default:
140 {
141 int diff_hour, diff_min;
142 struct tm local;
143 memcpy(&local, t, sizeof(struct tm));
144
145 if (timestamps_utc)
146 {
147 diff_hour = diff_min = 0;
148 }
149 else
150 {
151 struct tm *gmt = gmtime(&now);
152 diff_min = 60*(local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min;
153 if (local.tm_year != gmt->tm_year)
154 diff_min += (local.tm_year > gmt->tm_year)? 1440 : -1440;
155 else if (local.tm_yday != gmt->tm_yday)
156 diff_min += (local.tm_yday > gmt->tm_yday)? 1440 : -1440;
157 diff_hour = diff_min/60;
158 diff_min = abs(diff_min - diff_hour*60);
159 }
160
161 switch(type)
162 {
163 case tod_log_zone: /* Format used in logging with timezone */
164 (void) sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+03d%02d",
165 1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday,
166 local.tm_hour, local.tm_min, local.tm_sec,
167 diff_hour, diff_min);
168 break;
169
170 case tod_zone: /* Just the timezone offset */
171 (void) sprintf(CS timebuf, "%+03d%02d", diff_hour, diff_min);
172 break;
173
174 /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */
175
176 #ifdef SUPPORT_MBX
177 case tod_mbx:
178 {
179 int len;
180 (void) sprintf(CS timebuf, "%02d-", local.tm_mday);
181 len = Ustrlen(timebuf);
182 len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b-%Y %H:%M:%S",
183 &local);
184 (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min);
185 }
186 break;
187 #endif
188
189 /* tod_full: format used in Received: headers (use as default just in case
190 called with a junk type value) */
191
192 default:
193 {
194 int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", &local);
195 (void) sprintf(CS timebuf + len, "%02d ", local.tm_mday);
196 len += Ustrlen(timebuf + len);
197 len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b %Y %H:%M:%S",
198 &local);
199 (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min);
200 }
201 break;
202 }
203 }
204 break;
205 }
206
207return timebuf;
208}
209
210/* End of tod.c */