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